mirror of
https://github.com/paperless-ngx/paperless-ngx.git
synced 2025-05-23 17:52:23 -04:00
65 lines
2.0 KiB
TypeScript
65 lines
2.0 KiB
TypeScript
import { HttpEventType } from '@angular/common/http'
|
|
import { Injectable } from '@angular/core'
|
|
import { Subscription } from 'rxjs'
|
|
import { DocumentService } from './rest/document.service'
|
|
import {
|
|
FileStatusPhase,
|
|
WebsocketStatusService,
|
|
} from './websocket-status.service'
|
|
|
|
@Injectable({
|
|
providedIn: 'root',
|
|
})
|
|
export class UploadDocumentsService {
|
|
private uploadSubscriptions: Array<Subscription> = []
|
|
|
|
constructor(
|
|
private documentService: DocumentService,
|
|
private websocketStatusService: WebsocketStatusService
|
|
) {}
|
|
|
|
public uploadFile(file: File) {
|
|
let formData = new FormData()
|
|
formData.append('document', file, file.name)
|
|
formData.append('from_webui', 'true')
|
|
let status = this.websocketStatusService.newFileUpload(file.name)
|
|
|
|
status.message = $localize`Connecting...`
|
|
|
|
this.uploadSubscriptions[file.name] = 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'] ?? event.body.toString()
|
|
status.message = $localize`Upload complete, waiting...`
|
|
this.uploadSubscriptions[file.name]?.complete()
|
|
}
|
|
},
|
|
error: (error) => {
|
|
switch (error.status) {
|
|
case 400: {
|
|
this.websocketStatusService.fail(status, error.error.document)
|
|
break
|
|
}
|
|
default: {
|
|
this.websocketStatusService.fail(
|
|
status,
|
|
$localize`HTTP error: ${error.status} ${error.statusText}`
|
|
)
|
|
break
|
|
}
|
|
}
|
|
this.uploadSubscriptions[file.name]?.complete()
|
|
},
|
|
})
|
|
}
|
|
}
|