github.com/readium/readium-lcp-server@v0.0.0-20240101192032-6e95190e99f1/frontend/manage/app/publication/publication-form.component.ts (about) 1 import { Component, Input, OnInit, Directive } from '@angular/core'; 2 import { Router } from '@angular/router'; 3 import { 4 FormGroup, 5 FormControl, 6 Validators, 7 FormBuilder } from '@angular/forms'; 8 9 import { Publication } from './publication'; 10 import { PublicationService } from './publication.service'; 11 import { MasterFile } from './master-file'; 12 import { FileSelectDirective, FileDropDirective, FileUploader } from 'ng2-file-upload'; 13 14 declare var Config: any; 15 16 @Component({ 17 moduleId: module.id, 18 selector: 'lcp-publication-form', 19 templateUrl: 'publication-form.component.html' 20 }) 21 22 export class PublicationFormComponent implements OnInit { 23 @Input() 24 publication: Publication; 25 masterFiles: MasterFile[]; 26 baseUrl: string = Config.frontend.url; 27 28 hideFilename: boolean = false; 29 submitButtonLabel: string = "Add"; 30 form: FormGroup; 31 32 snackMessage: string = ""; 33 uploadConfirmation: boolean; 34 errorMessage: string = ""; 35 36 fileName: string; 37 38 public uploader:FileUploader; 39 public droppedItem:any; 40 public hasBaseDropZoneOver:boolean = false; 41 public notAPublication: boolean = false; 42 43 constructor( 44 private fb: FormBuilder, 45 private router: Router, 46 private publicationService: PublicationService) { 47 } 48 49 public fileOverBase(e:any):void { 50 this.hasBaseDropZoneOver = e; 51 } 52 53 refreshMasterFiles(): void { 54 this.publicationService.getMasterFiles().then( 55 masterFiles => { 56 this.masterFiles = masterFiles; 57 } 58 ); 59 } 60 61 // onItemAdded is executed when a file is added to the opload component 62 onItemAdded = function(fileItem: any) 63 { 64 this.split = fileItem.file.name.split('.'); 65 let extension = this.split[this.split.length-1]; 66 if (extension === "epub" || extension === "pdf" || extension === "lpf" || 67 extension === "webpub" || extension === "audiobook" || extension === "divina") 68 { 69 this.notAPublication = false; 70 } 71 else 72 { 73 this.notAPublication = true; 74 } 75 this.uploader.queue = [fileItem]; 76 this.droppedItem = fileItem; 77 } 78 79 ngOnInit(): void { 80 this.uploader = new FileUploader({url: this.baseUrl + "/publicationUpload"}); 81 this.refreshMasterFiles(); 82 83 // Events declarations 84 this.uploader.onAfterAddingFile = (fileItem: any) => {this.onItemAdded(fileItem); fileItem.withCredentials = false; } 85 this.uploader.onCompleteAll = () => {this.gotoList()} 86 87 // case = import of a new publication 88 if (this.publication == null) { 89 this.submitButtonLabel = "Add"; 90 this.form = this.fb.group({ 91 "title": ["", Validators.required], 92 "filename": ["", Validators.required], 93 "type": ["UPLOAD", Validators.nullValidator] 94 }); 95 // case = edition of an existing publication 96 } else { 97 this.hideFilename = true 98 this.submitButtonLabel = "Save"; 99 this.form = this.fb.group({ 100 "title": [this.publication.title, Validators.required] 101 }); 102 } 103 } 104 105 gotoList() { 106 this.router.navigate(['/publications']); 107 } 108 109 onCancel() { 110 this.gotoList(); 111 } 112 113 // onSubmit imports a publication into the frontend server, 114 // or updates information on an existing publication. 115 // confirm indicates if the user must provide a confirmation in case a publication 116 // already exists with the same title. 117 onSubmit(confirm: boolean) { 118 // case = edition of an existing publication 119 if (this.publication) { 120 this.publication.title = this.form.value['title']; 121 this.publicationService.update( 122 this.publication 123 ).then( 124 publication => { 125 this.gotoList(); 126 } 127 ); 128 // case = import of a new publication 129 } else { 130 // if the import into the frontend server needs confirmation (in case of a detected duplicate) 131 if (confirm) { 132 // check the title chosen for the publication 133 this.publicationService.checkByName(this.form.value['title']).then( 134 result => { 135 // if there is no duplicate 136 if (result === 0) { 137 // upload the publication 138 if (this.form.value["type"] === "UPLOAD") { 139 let options = {url: this.baseUrl + "/publicationUpload?title=" + this.form.value['title']}; 140 this.uploader.setOptions(options); 141 this.uploader.uploadItem(this.droppedItem); 142 // or request the import of a publication into the frontend server 143 } else { 144 let publication = new Publication(); 145 publication.title = this.form.value['title']; 146 publication.masterFilename = this.form.value['filename']; 147 this.publicationService.addPublication(publication) 148 .then( error => { 149 console.log(error); 150 this.uploadConfirmation = false; 151 if (error === 200) { 152 this.gotoList(); 153 } else if (error === 400) { 154 this.errorMessage = "The file must be a proper EPUB, PDF or LPF file." 155 this.showSnackBar(false); 156 } 157 } 158 ); 159 } 160 } else { 161 this.uploadConfirmation = true; 162 this.showSnackBar(true); 163 } 164 } 165 ); 166 // if the import into the frontend server doesn't need confirmation 167 } else { 168 // just treat the case of an update of the file via upload 169 if (this.form.value["type"] === "UPLOAD") { 170 let options = {url: this.baseUrl + "/publicationUpload?title=" + this.form.value['title']}; 171 this.uploader.setOptions(options); 172 this.uploader.uploadItem(this.droppedItem); 173 } 174 // the case where a new master file is selected for an existing title is not treated here 175 // I could be useful, still ... 176 this.gotoList(); 177 } 178 } 179 } 180 181 showSnackBar(stay: boolean) { 182 var snakeClass: string = "show stay"; 183 184 if (!stay) snakeClass = "show"; 185 var x = $("#snackbar"); 186 x.attr("class",snakeClass); 187 if (!stay) setTimeout(function(){$("#snackbar").attr("class","");}, 3000); 188 } 189 190 hideSnackBar() { 191 var x = $("#snackbar"); 192 x.attr("class",""); 193 } 194 }