1 import {Component, Input, OnInit, ViewChild} from '@angular/core';
2 import {FormControl} from '@angular/forms';
3 import {takeLast, finalize} from 'rxjs/operators';
4 import {DialogComponent} from '@eg/share/dialog/dialog.component';
5 import {AuthService} from '@eg/core/auth.service';
6 import {NetService} from '@eg/core/net.service';
7 import {EventService} from '@eg/core/event.service';
8 import {ToastService} from '@eg/share/toast/toast.service';
9 import {NgbModal} from '@ng-bootstrap/ng-bootstrap';
10 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
11 import {StringComponent} from '@eg/share/string/string.component';
12 import {HttpClient, HttpResponse, HttpErrorResponse, HttpEventType} from '@angular/common/http';
15 selector: 'eg-upload-jacket-image-dialog',
16 templateUrl: './upload-jacket-image-dialog.component.html'
20 export class UploadJacketImageDialogComponent extends DialogComponent implements OnInit {
22 // ID of bib record for jacket image
23 @Input() recordId: number;
27 errorUploading: boolean;
28 errorAuthentication: boolean;
29 errorAuthorization: boolean;
30 errorCompressionConfig: boolean;
31 errorNotFound: boolean;
32 errorLocationConfig: boolean;
33 errorWritingFile: boolean;
35 errorParsing: boolean;
36 errorGeneric: boolean;
38 private fileEvent: any;
41 private modal: NgbModal,
42 private auth: AuthService,
43 private evt: EventService,
44 private net: NetService,
45 private toast: ToastService,
46 private http: HttpClient
52 this.errorAuthentication = false;
53 this.errorAuthorization = false;
54 this.errorCompressionConfig = false;
55 this.errorNotFound = false;
56 this.errorLocationConfig = false;
57 this.errorWritingFile = false;
58 this.errorSize = false;
59 this.errorParsing = false;
60 this.errorGeneric = false;
61 this.errorUploading = false;
65 this.uploading = false;
70 onFileSelected(event) {
71 console.debug('onFileSelected', event);
72 this.fileEvent = event;
73 const file: File = this.fileEvent.target.files[0];
82 const file: File = this.fileEvent.target.files[0];
84 this.uploading = true;
86 const formData = new FormData();
87 formData.append('jacket_upload', file);
88 formData.append('ses', this.auth.token());
89 formData.append('bib_record', this.recordId.toString());
91 const upload$ = this.http.post('/jacket-upload', formData, {
98 console.debug('Jacket upload: ' , x);
99 if (x instanceof HttpResponse) {
100 console.debug('yay', x.body);
101 if (x.body !== '1') {
102 this.uploading = false;
103 this.errorUploading = true;
106 case 'session not found': this.errorAuthentication = true; break;
107 case 'permission denied': this.errorAuthorization = true; break;
108 case 'invalid compression level': this.errorCompressionConfig = true; break;
109 case 'bib not found': this.errorNotFound = true; break;
110 case 'jacket location not configured': this.errorLocationConfig = true; break;
111 case 'unable to open file for writing': this.errorWritingFile = true; break;
112 case 'file too large': this.errorSize = true; break;
113 case 'parse error': this.errorParsing = true; break;
114 case 'upload error': this.errorGeneric = true; break;
115 default: this.errorGeneric = true; break;
120 this.uploading = false;
121 this.errorUploading = true;
122 this.errorGeneric = true;
123 console.error('jacket upload error: ' , err);
125 () => this.refreshPage()
131 if (this.errorUploading) {
132 console.debug('no refresh page due to error');
134 console.debug('refresh page');
135 location.href = location.href;