paperless-ngx/src-ui/src/app/components/dashboard/widgets/upload-file-widget/upload-file-widget.component.ts

147 lines
4.4 KiB
TypeScript
Raw Normal View History

2020-12-08 00:45:11 +01:00
import { HttpEventType } from '@angular/common/http';
2020-11-22 22:35:39 +01:00
import { Component, OnInit } from '@angular/core';
import { FileSystemFileEntry, NgxFileDropEntry } from 'ngx-file-drop';
2021-01-26 00:51:45 +01:00
import { ConsumerStatusService, FileStatus, FileStatusPhase } from 'src/app/services/consumer-status.service';
2020-11-22 22:35:39 +01:00
import { DocumentService } from 'src/app/services/rest/document.service';
2021-01-26 14:48:39 -08:00
const MAX_ALERTS = 5
2020-12-08 00:45:11 +01:00
2020-11-22 22:35:39 +01:00
@Component({
selector: 'app-upload-file-widget',
templateUrl: './upload-file-widget.component.html',
styleUrls: ['./upload-file-widget.component.scss']
})
export class UploadFileWidgetComponent implements OnInit {
2021-01-26 14:48:39 -08:00
alertsExpanded = false
2020-11-22 22:35:39 +01:00
2021-01-26 00:51:45 +01:00
constructor(
private documentService: DocumentService,
private consumerStatusService: ConsumerStatusService
) { }
2020-11-22 22:35:39 +01:00
2021-01-26 00:51:45 +01:00
getStatus() {
2021-01-26 14:48:39 -08:00
return this.consumerStatusService.getConsumerStatus().slice(0, MAX_ALERTS)
}
2021-01-28 10:54:56 +01:00
getStatusSummary() {
let strings = []
let countUploadingAndProcessing = this.consumerStatusService.getConsumerStatusNotCompleted().length
let countFailed = this.getStatusFailed().length
let countSuccess = this.getStatusSuccess().length
if (countUploadingAndProcessing > 0) {
strings.push($localize`Processing: ${countUploadingAndProcessing}`)
}
if (countFailed > 0) {
strings.push($localize`Failed: ${countFailed}`)
}
if (countSuccess > 0) {
strings.push($localize`Added: ${countSuccess}`)
}
return strings.join($localize`:this string is used to separate processing, failed and added on the file upload widget:, `)
}
getStatusHidden() {
2021-01-26 14:48:39 -08:00
if (this.consumerStatusService.getConsumerStatus().length < MAX_ALERTS) return []
else return this.consumerStatusService.getConsumerStatus().slice(MAX_ALERTS)
}
getStatusUploading() {
return this.consumerStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
}
2021-01-28 10:54:56 +01:00
getStatusFailed() {
return this.consumerStatusService.getConsumerStatus(FileStatusPhase.FAILED)
}
getStatusSuccess() {
return this.consumerStatusService.getConsumerStatus(FileStatusPhase.SUCCESS)
}
getStatusCompleted() {
return this.consumerStatusService.getConsumerStatusCompleted()
}
getTotalUploadProgress() {
let current = 0
let max = 0
this.getStatusUploading().forEach(status => {
current += status.currentPhaseProgress
max += status.currentPhaseMaxProgress
})
return current / Math.max(max, 1)
2020-11-22 22:35:39 +01:00
}
2021-01-26 00:51:45 +01:00
isFinished(status: FileStatus) {
return status.phase == FileStatusPhase.FAILED || status.phase == FileStatusPhase.SUCCESS
2020-11-22 22:35:39 +01:00
}
2021-01-26 02:32:45 -08:00
getStatusColor(status: FileStatus) {
2021-01-26 00:51:45 +01:00
switch (status.phase) {
case FileStatusPhase.PROCESSING:
case FileStatusPhase.UPLOADING:
return "primary"
case FileStatusPhase.FAILED:
return "danger"
case FileStatusPhase.SUCCESS:
return "success"
}
2020-11-22 22:35:39 +01:00
}
2021-01-26 00:51:45 +01:00
dismiss(status: FileStatus) {
this.consumerStatusService.dismiss(status)
}
2021-01-26 02:32:45 -08:00
2021-01-26 02:52:16 -08:00
dismissAll() {
this.consumerStatusService.dismissAll()
}
2021-01-26 00:51:45 +01:00
ngOnInit(): void {
}
2020-12-08 00:45:11 +01:00
2021-01-26 00:51:45 +01:00
public fileOver(event){
2020-12-08 00:45:11 +01:00
}
2021-01-26 00:51:45 +01:00
public fileLeave(event){
2020-12-08 00:45:11 +01:00
}
2020-11-22 22:35:39 +01:00
public dropped(files: NgxFileDropEntry[]) {
for (const droppedFile of files) {
if (droppedFile.fileEntry.isFile) {
2020-12-08 17:35:51 +01:00
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
2020-11-22 22:35:39 +01:00
fileEntry.file((file: File) => {
2020-12-08 00:45:11 +01:00
let formData = new FormData()
2020-11-22 22:35:39 +01:00
formData.append('document', file, file.name)
let status = this.consumerStatusService.newFileUpload(file.name)
2021-01-26 02:32:45 -08:00
2021-01-27 15:50:37 +01:00
status.message = $localize`Connecting...`
2020-12-08 00:45:11 +01:00
this.documentService.uploadDocument(formData).subscribe(event => {
if (event.type == HttpEventType.UploadProgress) {
2021-01-26 00:51:45 +01:00
status.updateProgress(FileStatusPhase.UPLOADING, event.loaded, event.total)
2021-01-27 15:50:37 +01:00
status.message = $localize`Uploading...`
2020-12-08 00:45:11 +01:00
} else if (event.type == HttpEventType.Response) {
2021-01-26 00:51:45 +01:00
status.taskId = event.body["task_id"]
2021-02-02 12:56:26 +01:00
status.message = $localize`Upload complete, waiting...`
2020-12-08 00:45:11 +01:00
}
2020-11-22 22:35:39 +01:00
}, error => {
switch (error.status) {
case 400: {
2021-01-27 16:04:06 +01:00
this.consumerStatusService.fail(status, error.error.document)
break;
}
default: {
2021-02-03 16:01:32 +01:00
this.consumerStatusService.fail(status, $localize`HTTP error: ${error.status} ${error.statusText}`)
break;
}
}
2021-01-26 00:51:45 +01:00
2020-11-22 22:35:39 +01:00
})
});
}
}
}
}