]> git.ipfire.org Git - thirdparty/paperless-ngx.git/commitdiff
Cleanup file upload susbcriptions 283/head
authorMichael Shamoon <4887959+shamoon@users.noreply.github.com>
Tue, 5 Apr 2022 20:29:51 +0000 (13:29 -0700)
committerMichael Shamoon <4887959+shamoon@users.noreply.github.com>
Tue, 5 Apr 2022 20:33:24 +0000 (13:33 -0700)
src-ui/src/app/services/upload-documents.service.ts

index ba97f0d9caa13129decf91c236a5f8bdcf7e1ec7..5e7ef7fbe2b2fa6189bd81c54717938ea265920a 100644 (file)
@@ -6,11 +6,14 @@ import {
   FileStatusPhase,
 } from './consumer-status.service'
 import { DocumentService } from './rest/document.service'
+import { Subscription } from 'rxjs'
 
 @Injectable({
   providedIn: 'root',
 })
 export class UploadDocumentsService {
+  private uploadSubscriptions: Array<Subscription> = []
+
   constructor(
     private documentService: DocumentService,
     private consumerStatusService: ConsumerStatusService
@@ -27,36 +30,43 @@ export class UploadDocumentsService {
 
           status.message = $localize`Connecting...`
 
-          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...`
-              }
-            },
-            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}`
+          this.uploadSubscriptions[file.name] = this.documentService
+            .uploadDocument(formData)
+            .subscribe({
+              next: (event) => {
+                if (event.type == HttpEventType.UploadProgress) {
+                  status.updateProgress(
+                    FileStatusPhase.UPLOADING,
+                    event.loaded,
+                    event.total
                   )
-                  break
+                  status.message = $localize`Uploading...`
+                } else if (event.type == HttpEventType.Response) {
+                  status.taskId = event.body['task_id']
+                  status.message = $localize`Upload complete, waiting...`
+                  this.uploadSubscriptions[file.name]?.complete()
+                }
+              },
+              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
+                  }
                 }
-              }
-            },
-          })
+                this.uploadSubscriptions[file.name]?.complete()
+              },
+            })
         })
       }
     }