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

120 lines
3.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)
}
getStatusesHidden() {
if (this.consumerStatusService.getConsumerStatus().length < MAX_ALERTS) return []
else return this.consumerStatusService.getConsumerStatus().slice(MAX_ALERTS)
}
getStatusUploading() {
return this.consumerStatusService.getConsumerStatus(FileStatusPhase.UPLOADING)
}
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
status.message = "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)
status.message = "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"]
status.message = "Upload complete."
2020-12-08 00:45:11 +01:00
}
2020-11-22 22:35:39 +01:00
}, error => {
2021-01-26 00:51:45 +01:00
status.updateProgress(FileStatusPhase.FAILED)
switch (error.status) {
case 400: {
2021-01-26 00:51:45 +01:00
status.message = error.error.document
break;
}
default: {
2021-01-26 00:51:45 +01:00
status.message = $localize`An error has occurred while uploading the document. Sorry!`
break;
}
}
2021-01-26 00:51:45 +01:00
2020-11-22 22:35:39 +01:00
})
});
}
}
}
}