641 lines
20 KiB
TypeScript

/** @file
* @author Edouard DUPIN
* @copyright 2018, Edouard DUPIN, all right reserved
* @license PROPRIETARY (see license file)
*/
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { fadeInAnimation } from '../../_animations/index';
import { PopInService } from '../../service/popin';
import { TypeService } from '../../service/type';
import { UniverseService } from '../../service/universe';
import { SeriesService } from '../../service/series';
import { VideoService } from '../../service/video';
import { ArianeService } from '../../service/ariane';
import { UploadProgress } from '../../popin/upload-progress/upload-progress';
import { SeasonService } from '../../service/season';
export class ElementList {
constructor(
public value: number,
public label: string) {
// nothing to do.
}
}
export class FileParsedElement {
public isSended: boolean = false;
public nameDetected: boolean = false;
public episodeDetected: boolean = false;
constructor(
public file: File,
public universe: string,
public series: string,
public season: number,
public episode: number,
public title: string) {
// nothiing to do.
}
}
export class FileFailParsedElement {
constructor(
public file: File,
public reason: string) {
// nothiing to do.
}
}
@Component({
selector: 'app-video-edit',
templateUrl: './upload.html',
styleUrls: ['./upload.less'],
animations: [fadeInAnimation],
host: { '[@fadeInAnimation]': '' }
})
export class UploadScene implements OnInit {
parsedElement: FileParsedElement[] = [];
parsedFailedElement: FileFailParsedElement[] = [];
upload_file_value: string = ""
selectedFiles: FileList;
type_id: number = null
series_id: number = null
saison_id: number = null
need_send: boolean = false;
// list of all files already registered in the bdd to compare with the curent list of files.
listFileInBdd: any = null;
// section tha define the upload value to display in the pop-in of upload
public upload:UploadProgress = new UploadProgress();
listType: ElementList[] = [
{ value: null, label: '---' },
];
listUniverse: ElementList[] = [
{ value: null, label: '---' },
];
listSeries: ElementList[] = [
{ value: null, label: '---' },
];
listSeries2 = [{ id: null, description: '---' }];
/*
config = {
displayKey: "label", // if objects array passed which key to be displayed defaults to description
search: true,
limitTo: 3,
};
*/
config = {
displayKey: "description", //if objects array passed which key to be displayed defaults to description
search: true, //true/false for the search functionlity defaults to false,
height: 'auto', //height of the list so that if there are more no of items it can show a scroll defaults to auto. With auto height scroll will never appear
placeholder: 'Select', // text to be displayed when no item is selected defaults to Select,
customComparator: ()=>{}, // a custom function using which user wants to sort the items. default is undefined and Array.sort() will be used in that case,
limitTo: 10, // number thats limits the no of options displayed in the UI (if zero, options will not be limited)
moreText: 'more', // text to be displayed whenmore than one items are selected like Option 1 + 5 more
noResultsFound: 'No results found!', // text to be displayed when no items are found while searching
searchPlaceholder: 'Search', // label thats displayed in search input,
searchOnKey: 'description', // key on which search should be performed this will be selective search. if undefined this will be extensive search on all keys
}
listSeason: ElementList[] = [
{ value: null, label: '---' },
];
global_universe: string = "";
global_series: string = "";
global_season: number = null;
constructor(private route: ActivatedRoute,
private typeService: TypeService,
private universeService: UniverseService,
private seriesService: SeriesService,
private seasonService: SeasonService,
private videoService: VideoService,
private arianeService: ArianeService,
private popInService: PopInService) {
// nothing to do.
}
updateNeedSend (): boolean {
if (this.parsedElement.length === 0) {
this.need_send = false;
return;
}
this.need_send = true;
for (let iii = 0; iii< this.parsedElement.length; iii++) {
if (this.parsedElement[iii].title === undefined || this.parsedElement[iii].title === null || this.parsedElement[iii].title === "") {
this.need_send = false;
}
}
if (this.type_id === undefined || this.type_id === null) {
this.need_send = false;
}
return this.need_send;
}
ngOnInit () {
this.arianeService.updateManual(this.route.snapshot.paramMap);
let self = this;
this.listType = [{ value: null, label: '---' }];
this.listUniverse = [{ value: null, label: '---' }];
this.listSeries = [{ value: null, label: '---' }];
this.listSeason = [{ value: null, label: '---' }];
this.universeService.getData()
.then(function (response2) {
for (let iii = 0; iii < response2.length; iii++) {
self.listUniverse.push({ value: response2[iii].id, label: response2[iii].name });
}
}).catch(function (response2) {
console.log("get response22 : " + JSON.stringify(response2, null, 2));
});
this.typeService.getData()
.then(function (response2) {
for (let iii = 0; iii < response2.length; iii++) {
self.listType.push({ value: response2[iii].id, label: response2[iii].name });
}
}).catch(function (response2) {
console.log("get response22 : " + JSON.stringify(response2, null, 2));
});
console.log(" END INIT ");
}
onChangeType (_value: any): void {
this.series_id = null;
this.updateType (_value);
}
private updateType (_value: any): void {
console.log("Change requested of type ... " + _value);
if (this.type_id == _value) {
return;
}
this.type_id = _value;
//this.data.series_id = null;
//this.data.season_id = null;
this.listSeries = [{value: null, label: '---'}];
this.listSeason = [{value: null, label: '---'}];
let self = this;
this.updateNeedSend();
if (this.type_id != null) {
self.typeService.getSubSeries(this.type_id, ["id", "name"])
.then(function(response2) {
for(let iii= 0; iii < response2.length; iii++) {
self.listSeries.push({value: response2[iii].id, label: response2[iii].name});
}
}).catch(function(response2) {
console.log("get response22 : " + JSON.stringify(response2, null, 2));
});
}
}
onChangeSeries (_value: any): void {
this.series_id = _value;
if (_value === undefined || _value === null) {
} else {
for (let iii = 0 ; iii<this.listSeries.length ; iii++) {
if (this.listSeries[iii].value == _value) {
this.global_series = this.listSeries[iii].label;
break;
}
}
}
this.updateNeedSend();
this.updateListOfVideoToCheck();
}
onSeason (_value: any): void {
this.global_season = _value;
//console.log("change episode ID: " + _value + " ==> " + this.parse_season.toString());
this.updateNeedSend();
this.updateListOfVideoToCheck();
}
onTitle (data: FileParsedElement, _value: any): void {
data.title = _value;
this.updateNeedSend();
}
onUniverse (_value: any): void {
this.global_universe = _value;
this.updateNeedSend();
}
onEpisode (data: FileParsedElement, _value: any): void {
data.episode = _value;
//console.log("change episode ID: " + _value + " ==> " + this.parse_episode.toString());
this.updateNeedSend();
}
onSeries (_value: any): void {
this.global_series = _value;
let self = this;
if (this.global_series != "") {
this.seriesService.getLike(this.global_series)
.then(function(response: any[]) {
console.log("find element: " + response.length);
for (let iii = 0; iii<response.length; iii++) {
console.log(" - " + JSON.stringify(response[iii]));
}
if (response.length == 0) {
self.series_id = null;
} else if (response.length == 1) {
self.series_id = response[0].id;
}
this.updateListOfVideoToCheck();
}).catch(function(response) {
console.log("CAN NOT find element: " );
self.series_id = null;
});
}
this.updateNeedSend();
}
clearData() {
this.global_universe = "";
this.global_series = "";
this.global_season = null;
this.parsedElement = [];
this.parsedFailedElement = [];
this.listFileInBdd = null;
this.type_id = null;
this.series_id = null;
this.saison_id = null;
this.listSeries = [{value: null, label: '---'}];
this.listSeason = [{value: null, label: '---'}];
}
addFileWithMetaData(file: File) {
//parsedElement: FileParsedElement[] = [];
let universe: string = null;
let series: string = null;
let season: number | null = null;
let episode: number | null = null;
let title: string = "";
console.log("select file " + file.name);
let tmpName = file.name.replace(/[ \t]*-[ \t]*/g,'-');
tmpName = tmpName.replace(/[Ss]([0-9]+)[- \t]+[Ee]([0-9]+)/g,'-s$1-e$2-');
tmpName = tmpName.replace(/[Ee]([0-9]+)[- \t]+[Ss]([0-9]+)/g,'-s$2-e$1-');
tmpName = tmpName.replace(/_/g,'-');
tmpName = tmpName.replace(/--/g,'-');
console.log("select file " + tmpName);
const splitElement = tmpName.split('-');
if (splitElement.length == 1) {
title = splitElement[0];
} else {
if (splitElement.length>=2) {
series = splitElement[0];
}
splitElement.splice(0,1);
if (splitElement.length == 1) {
title = splitElement[0];
} else {
while (splitElement.length>0) {
let element = splitElement[0];
let find = false;
if (season === null) {
if (element.length >= 1 && (element[0] === 's' || element[0] === 'S') ) {
element = element.substring(1);
season = parseInt(element, 10);
find = true;
}
}
if (episode === null && find === false) {
if (element.length >= 1 && (element[0] === 'e' || element[0] === 'E') ) {
element = element.substring(1);
episode = parseInt(element, 10);
find = true;
}
}
if (find === false) {
if (season === null && episode === null) {
if (universe === "") {
universe = element;
} else {
universe = universe + "-" + element;
}
} else {
if (title === "") {
title = element;
} else {
title = title + "-" + element;
}
}
}
splitElement.splice(0,1);
}
}
}
if (isNaN(episode)) {
episode = null;
}
if (isNaN(season)) {
season = null;
}
// remove extention
title = title.replace(new RegExp("\.(mkv|MKV|Mkv|webm|WEBM|Webm|mp4)"),"");
let tmp = new FileParsedElement(file, universe, series, season, episode, title);
console.log("==>" + JSON.stringify(tmp));
// add it in the list.
this.parsedElement.push(tmp);
}
// At the file input element
// (change)="selectFile($event)"
onChangeFile (_value: any): void {
this.clearData();
for (let iii=0; iii<_value.files.length; iii++) {
this.addFileWithMetaData(_value.files[iii]);
}
// check if all global parameters are generic:
if (this.parsedElement.length == 0) {
this.updateNeedSend();
return;
}
// we verify fith the first value to remove all unknown ...
// clean different univers:
for (let iii=1; iii<this.parsedElement.length; iii++) {
console.log("check universe [" + (iii+1) + "/" + this.parsedElement.length + "] '" + this.parsedElement[0].universe + " !== " + this.parsedElement[iii].universe + "'");
if (this.parsedElement[0].universe !== this.parsedElement[iii].universe) {
this.parsedFailedElement.push(new FileFailParsedElement(this.parsedElement[iii].file, "Remove from list due to wrong universe value"));
console.log("Remove from list (!= universe) : [" + (iii+1) + "/" + this.parsedElement.length + "] '" + this.parsedElement[iii].file.name + "'");
this.parsedElement.splice(iii,1);
iii--;
}
}
// clean different series:
for (let iii=1; iii<this.parsedElement.length; iii++) {
console.log("check series [" + (iii+1) + "/" + this.parsedElement.length + "] '" + this.parsedElement[0].series + " !== " + this.parsedElement[iii].series + "'");
if (this.parsedElement[0].series !== this.parsedElement[iii].series) {
this.parsedFailedElement.push(new FileFailParsedElement(this.parsedElement[iii].file, "Remove from list due to wrong series value"));
console.log("Remove from list (!= series) : [" + (iii+1) + "/" + this.parsedElement.length + "] '" + this.parsedElement[iii].file.name + "'");
this.parsedElement.splice(iii,1);
iii--;
}
}
// clean different season:
for (let iii=1; iii<this.parsedElement.length; iii++) {
console.log("check season [" + (iii+1) + "/" + this.parsedElement.length + "] '" + this.parsedElement[0].season + " !== " + this.parsedElement[iii].season + "'");
if (this.parsedElement[0].season !== this.parsedElement[iii].season) {
this.parsedFailedElement.push(new FileFailParsedElement(this.parsedElement[iii].file, "Remove from list due to wrong season value"));
console.log("Remove from list (!= season) : [" + (iii+1) + "/" + this.parsedElement.length + "] '" + this.parsedElement[iii].file.name + "'");
this.parsedElement.splice(iii,1);
iii--;
}
}
this.global_universe = this.parsedElement[0].universe;
this.global_series = this.parsedElement[0].series;
this.global_season = this.parsedElement[0].season;
this.updateNeedSend();
this.series_id = null;
this.saison_id = null;
let self = this;
if (this.global_series != "") {
this.seriesService.getLike(this.global_series)
.then(function(response: any[]) {
console.log("find element: " + response.length);
for (let iii = 0; iii<response.length; iii++) {
console.log(" - " + JSON.stringify(response[iii]));
}
if (response.length == 0) {
self.series_id = null;
} else if (response.length == 1) {
let serie_elem = response[0];
self.series_id = serie_elem.id;
self.updateType(serie_elem.parent_id);
}
self.updateListOfVideoToCheck();
}).catch(function(response) {
console.log("CAN NOT find element: " );
});
}
}
sendFile(): void {
console.log("Send file requested ... " + this.parsedElement.length);
this.upload = new UploadProgress();
// display the upload pop-in
this.popInService.open("popin-upload-progress");
this.globalUpLoad(0, this.parsedElement.length);
}
globalUpLoad(id: number, total: number): void {
let self = this;
this.uploadFile(this.parsedElement[id], id, total, () => {
let id2 = id + 1;
if (id2 < total) {
self.globalUpLoad(id2, total);
} else {
self.upload.result = "Media creation done";
}
}, (value:string) => {
self.upload.error = "Error in the upload of the data..." + value;
});
}
uploadFile (eleemnent: FileParsedElement, id: number, total: number, _sendDone: any, _errorOccured: any): void {
let self = this;
self.upload.labelMediaTitle = '';
// add universe
if (self.global_universe !== null) {
self.upload.labelMediaTitle += self.global_universe;
}
// add series
if (self.global_series !== null) {
if (self.upload.labelMediaTitle.length != 0) {
self.upload.labelMediaTitle += ":";
}
self.upload.labelMediaTitle += self.global_series;
}
// add season
if (self.global_season !== null && self.global_season !== undefined && self.global_season.toString().length != 0) {
if (self.upload.labelMediaTitle.length != 0) {
self.upload.labelMediaTitle += "-";
}
self.upload.labelMediaTitle += "s" + self.global_season.toString();
}
// add episode ID
if (eleemnent.episode !== null && eleemnent.episode !== undefined && eleemnent.episode.toString().length != 0) {
if (self.upload.labelMediaTitle.length != 0) {
self.upload.labelMediaTitle += "-";
}
self.upload.labelMediaTitle += "e" + eleemnent.episode.toString();
}
// add title
if (self.upload.labelMediaTitle.length != 0) {
self.upload.labelMediaTitle += "-";
}
self.upload.labelMediaTitle = '[' + (id+1) + '/' + total + ']' + self.upload.labelMediaTitle + eleemnent.title;
self.videoService.uploadFile(eleemnent.file,
self.global_universe,
self.global_series,
self.series_id,
self.global_season,
eleemnent.episode,
eleemnent.title,
self.type_id,
function(count, total) {
//console.log("upload : " + count*100/total);
self.upload.mediaSendSize = count;
self.upload.mediaSize = total;
})
.then(function (response) {
console.log("get response of video : " + JSON.stringify(response, null, 2));
_sendDone();
}).catch(function (response) {
//self.error = "Can not get the data";
console.log("Can not add the data in the system...");
_errorOccured(JSON.stringify(response, null, 2));
});
}
public checkSimilarString(valueA:string, valueB:string): boolean {
let valueAL = valueA.toLowerCase();
let valueBL = valueB.toLowerCase();
valueAL = valueAL.replace(/[ \t\n\r-_#~@]/g, "");
valueBL = valueBL.replace(/[ \t\n\r-_#~@]/g, "");
if (valueAL == valueBL) {
return true;
}
if (valueAL.startsWith(valueBL)) {
return true;
}
if (valueBL.startsWith(valueAL)) {
return true;
}
return false;
}
checkConcordence():void {
if (this.parsedElement === null) {
return;
}
// ckear checker
for (let iii = 0; iii < this.parsedElement.length; iii++) {
this.parsedElement[iii].nameDetected = false;
this.parsedElement[iii].episodeDetected = false;
}
if (this.listFileInBdd === null) {
return;
}
for (let iii = 0; iii < this.listFileInBdd.length; iii++) {
this.listFileInBdd[iii].nameDetected = false;
this.listFileInBdd[iii].episodeDetected = false;
}
for (let iii = 0; iii < this.parsedElement.length; iii++) {
for (let jjj = 0; jjj < this.listFileInBdd.length; jjj++) {
if (this.checkSimilarString(this.parsedElement[iii].title, this.listFileInBdd[jjj].name)) {
this.parsedElement[iii].nameDetected = true;
this.listFileInBdd[jjj].nameDetected = true;
}
if (this.parsedElement[iii].episode === this.listFileInBdd[jjj].episode) {
this.parsedElement[iii].episodeDetected = true;
this.listFileInBdd[jjj].episodeDetected = true;
}
}
}
}
updateListOfVideoToCheck(): void {
// No series ID set ==> nothing to do.
if (this.series_id === null) {
this.listFileInBdd = null;
return;
}
let self = this;
// no season check only the series files.
if (this.global_season === null) {
self.seriesService.getVideo(self.series_id)
.then(function(response: any[]) {
self.listFileInBdd = response;
//console.log("find video: " + response.length);
//for (let iii = 0; iii<response.length; iii++) {
// console.log(" - " + JSON.stringify(response[iii]));
//}
self.checkConcordence();
}).catch(function(response) {
self.listFileInBdd = null;
})
return;
}
self.saison_id = null;
// set 1 find the ID of the season:
this.seriesService.getSeason(this.series_id, [ "id" , "name" ])
.then(function(response: any[]) {
//console.log("find season: " + response.length);
for (let iii = 0; iii<response.length; iii++) {
//console.log(" - " + JSON.stringify(response[iii]) + 'compare with : ' + JSON.stringify(self.global_season));
if (response[iii].name == ""+self.global_season) {
self.saison_id = response[iii].id;
break;
}
}
if (self.saison_id === null) {
return;
}
self.seasonService.getVideo(self.saison_id)
.then(function(response: any[]) {
self.listFileInBdd = response;
//console.log("find video: " + response.length);
//for (let iii = 0; iii<response.length; iii++) {
// console.log(" - " + JSON.stringify(response[iii]));
//}
self.checkConcordence();
}).catch(function(response) {
self.listFileInBdd = null;
})
}).catch(function(response) {
self.listFileInBdd = null;
});
}
eventPopUpSeason (_event: string): void {
console.log("GET event: " + _event);
this.popInService.close("popin-new-season");
}
eventPopUpSeries (_event: string): void {
console.log("GET event: " + _event);
this.popInService.close("popin-new-series");
}
eventPopUpType (_event: string): void {
console.log("GET event: " + _event);
this.popInService.close("popin-new-type");
}
eventPopUpUniverse (_event: string): void {
console.log("GET event: " + _event);
this.popInService.close("popin-new-universe");
}
newSeason (): void {
console.log("Request new Season...");
this.popInService.open("popin-new-season");
}
newSeries (): void {
console.log("Request new Series...");
this.popInService.open("popin-new-series");
}
newType (): void {
console.log("Request new Type...");
this.popInService.open("popin-create-type");
}
newUniverse () {
console.log("Request new Universe...");
this.popInService.open("popin-new-universe");
}
}