Add multiple files uploading
[arvados-workbench2.git] / src / services / collection-service / collection-service.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { CommonResourceService } from "../../common/api/common-resource-service";
6 import { CollectionResource } from "../../models/collection";
7 import axios, { AxiosInstance } from "axios";
8 import { KeepService } from "../keep-service/keep-service";
9 import { FilterBuilder } from "../../common/api/filter-builder";
10 import { CollectionFile, CollectionFileType, createCollectionFile } from "../../models/collection-file";
11 import { parseKeepManifestText, stringifyKeepManifest } from "../collection-files-service/collection-manifest-parser";
12 import * as _ from "lodash";
13 import { KeepManifestStream } from "../../models/keep-manifest";
14
15 export class CollectionService extends CommonResourceService<CollectionResource> {
16     constructor(serverApi: AxiosInstance, private keepService: KeepService) {
17         super(serverApi, "collections");
18     }
19
20     uploadFile(keepServiceHost: string, file: File, fileIdx = 0): Promise<CollectionFile> {
21         const fd = new FormData();
22         fd.append(`file_${fileIdx}`, file);
23
24         return axios.post<string>(keepServiceHost, fd, {
25             onUploadProgress: (e: ProgressEvent) => {
26                 console.log(`${e.loaded} / ${e.total}`);
27             }
28         }).then(data => createCollectionFile({
29             id: data.data,
30             name: file.name,
31             size: file.size
32         }));
33     }
34
35     private async updateManifest(collectionUuid: string, files: CollectionFile[]): Promise<CollectionResource> {
36         const collection = await this.get(collectionUuid);
37         const manifest: KeepManifestStream[] = parseKeepManifestText(collection.manifestText);
38
39         files.forEach(f => {
40             let kms = manifest.find(stream => stream.name === f.path);
41             if (!kms) {
42                 kms = {
43                     files: [],
44                     locators: [],
45                     name: f.path
46                 };
47                 manifest.push(kms);
48             }
49             kms.locators.push(f.id);
50             const len = kms.files.length;
51             const nextPos = len > 0
52                 ? parseInt(kms.files[len - 1].position, 10) + kms.files[len - 1].size
53                 : 0;
54             kms.files.push({
55                 name: f.name,
56                 position: nextPos.toString(),
57                 size: f.size
58             });
59         });
60
61         console.log(manifest);
62
63         const manifestText = stringifyKeepManifest(manifest);
64         const data = { ...collection, manifestText };
65         return this.update(collectionUuid, CommonResourceService.mapKeys(_.snakeCase)(data));
66     }
67
68     uploadFiles(collectionUuid: string, files: File[]) {
69         console.log("Uploading files", files);
70
71         const filters = FilterBuilder.create()
72             .addEqual("service_type", "proxy");
73
74         return this.keepService.list({ filters }).then(data => {
75             if (data.items && data.items.length > 0) {
76                 const serviceHost =
77                     (data.items[0].serviceSslFlag ? "https://" : "http://") +
78                     data.items[0].serviceHost +
79                     ":" + data.items[0].servicePort;
80
81                 console.log("Servicehost", serviceHost);
82
83                 const files$ = files.map((f, idx) => this.uploadFile(serviceHost, f, idx));
84                 Promise.all(files$).then(values => {
85                     this.updateManifest(collectionUuid, values).then(() => {
86                         console.log("Upload done!");
87                     });
88                 });
89             }
90         });
91     }
92 }