2022-03-29 23:02:48 -07:00
|
|
|
import { Injectable } from '@angular/core'
|
|
|
|
|
import { HttpEventType } from '@angular/common/http'
|
|
|
|
|
import { FileSystemFileEntry, NgxFileDropEntry } from 'ngx-file-drop'
|
|
|
|
|
import {
|
|
|
|
|
ConsumerStatusService,
|
|
|
|
|
FileStatusPhase,
|
|
|
|
|
} from './consumer-status.service'
|
|
|
|
|
import { DocumentService } from './rest/document.service'
|
2022-03-10 20:59:09 -08:00
|
|
|
|
|
|
|
|
@Injectable({
|
2022-03-29 23:02:48 -07:00
|
|
|
providedIn: 'root',
|
2022-03-10 20:59:09 -08:00
|
|
|
})
|
|
|
|
|
export class UploadDocumentsService {
|
|
|
|
|
constructor(
|
|
|
|
|
private documentService: DocumentService,
|
|
|
|
|
private consumerStatusService: ConsumerStatusService
|
2022-03-29 23:02:48 -07:00
|
|
|
) {}
|
2022-03-10 20:59:09 -08:00
|
|
|
|
|
|
|
|
uploadFiles(files: NgxFileDropEntry[]) {
|
|
|
|
|
for (const droppedFile of files) {
|
|
|
|
|
if (droppedFile.fileEntry.isFile) {
|
2022-03-29 23:02:48 -07:00
|
|
|
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry
|
2022-03-10 20:59:09 -08:00
|
|
|
fileEntry.file((file: File) => {
|
|
|
|
|
let formData = new FormData()
|
|
|
|
|
formData.append('document', file, file.name)
|
|
|
|
|
let status = this.consumerStatusService.newFileUpload(file.name)
|
|
|
|
|
|
|
|
|
|
status.message = $localize`Connecting...`
|
|
|
|
|
|
2022-03-29 23:02:48 -07:00
|
|
|
this.documentService.uploadDocument(formData).subscribe({
|
|
|
|
|
next: (event) => {
|
|
|
|
|
if (event.type == HttpEventType.UploadProgress) {
|
|
|
|
|
status.updateProgress(
|
|
|
|
|
FileStatusPhase.UPLOADING,
|
|
|
|
|
event.loaded,
|
|
|
|
|
event.total
|
|
|
|
|
)
|
|
|
|
|
status.message = $localize`Uploading...`
|
|
|
|
|
} else if (event.type == HttpEventType.Response) {
|
|
|
|
|
status.taskId = event.body['task_id']
|
|
|
|
|
status.message = $localize`Upload complete, waiting...`
|
2022-03-10 20:59:09 -08:00
|
|
|
}
|
2022-03-29 23:02:48 -07:00
|
|
|
},
|
|
|
|
|
error: (error) => {
|
|
|
|
|
switch (error.status) {
|
|
|
|
|
case 400: {
|
|
|
|
|
this.consumerStatusService.fail(status, error.error.document)
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
default: {
|
|
|
|
|
this.consumerStatusService.fail(
|
|
|
|
|
status,
|
|
|
|
|
$localize`HTTP error: ${error.status} ${error.statusText}`
|
|
|
|
|
)
|
|
|
|
|
break
|
|
|
|
|
}
|
2022-03-10 20:59:09 -08:00
|
|
|
}
|
2022-03-29 23:02:48 -07:00
|
|
|
},
|
2022-03-10 20:59:09 -08:00
|
|
|
})
|
2022-03-29 23:02:48 -07:00
|
|
|
})
|
2022-03-10 20:59:09 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|