20029: Refactor replaceFiles operations
[arvados-workbench2.git] / src / services / collection-service / collection-service.ts
index f60e81f1975da8b4c37921f7490b9c0fb150c008..a95fb47be0f8a270b008974e3d90fcce95c3f490 100644 (file)
 //
 // SPDX-License-Identifier: AGPL-3.0
 
-import { CommonResourceService } from "~/common/api/common-resource-service";
-import { CollectionResource } from "~/models/collection";
-import axios, { AxiosInstance } from "axios";
-import { KeepService } from "../keep-service/keep-service";
-import { FilterBuilder } from "~/common/api/filter-builder";
-import { CollectionFile, createCollectionFile } from "~/models/collection-file";
-import { parseKeepManifestText, stringifyKeepManifest } from "../collection-files-service/collection-manifest-parser";
-import * as _ from "lodash";
-import { KeepManifestStream } from "~/models/keep-manifest";
+import { CollectionResource, defaultCollectionSelectedFields } from "models/collection";
+import { AxiosInstance } from "axios";
+import { CollectionFile, CollectionDirectory } from "models/collection-file";
+import { WebDAV } from "common/webdav";
+import { AuthService } from "../auth-service/auth-service";
+import { extractFilesData } from "./collection-service-files-response";
+import { TrashableResourceService } from "services/common-service/trashable-resource-service";
+import { ApiActions } from "services/api/api-actions";
+import { Session } from "models/session";
+import { CommonService } from "services/common-service/common-service";
 
 export type UploadProgress = (fileId: number, loaded: number, total: number, currentTime: number) => void;
 
-export class CollectionService extends CommonResourceService<CollectionResource> {
-    constructor(serverApi: AxiosInstance, private keepService: KeepService) {
-        super(serverApi, "collections");
+export const emptyCollectionUuid = 'd41d8cd98f00b204e9800998ecf8427e+0';
+
+export class CollectionService extends TrashableResourceService<CollectionResource> {
+    constructor(serverApi: AxiosInstance, private webdavClient: WebDAV, private authService: AuthService, actions: ApiActions) {
+        super(serverApi, "collections", actions, [
+            'fileCount',
+            'fileSizeTotal',
+            'replicationConfirmed',
+            'replicationConfirmedAt',
+            'storageClassesConfirmed',
+            'storageClassesConfirmedAt',
+            'unsignedManifestText',
+            'version',
+        ]);
     }
 
-    private readFile(file: File): Promise<ArrayBuffer> {
-        return new Promise<ArrayBuffer>(resolve => {
-            const reader = new FileReader();
-            reader.onload = () => {
-                resolve(reader.result as ArrayBuffer);
-            };
+    async get(uuid: string, showErrors?: boolean, select?: string[], session?: Session) {
+        super.validateUuid(uuid);
+        const selectParam = select || defaultCollectionSelectedFields;
+        return super.get(uuid, showErrors, selectParam, session);
+    }
 
-            reader.readAsArrayBuffer(file);
-        });
+    create(data?: Partial<CollectionResource>, showErrors?: boolean) {
+        return super.create({ ...data, preserveVersion: true }, showErrors);
     }
 
-    private uploadFile(keepServiceHost: string, file: File, fileId: number, onProgress?: UploadProgress): Promise<CollectionFile> {
-        return this.readFile(file).then(content => {
-            return axios.post<string>(keepServiceHost, content, {
-                headers: {
-                    'Content-Type': 'text/octet-stream'
-                },
-                onUploadProgress: (e: ProgressEvent) => {
-                    if (onProgress) {
-                        onProgress(fileId, e.loaded, e.total, Date.now());
-                    }
-                    console.log(`${e.loaded} / ${e.total}`);
-                }
-            }).then(data => createCollectionFile({
-                id: data.data,
-                name: file.name,
-                size: file.size
-            }));
-        });
+    update(uuid: string, data: Partial<CollectionResource>, showErrors?: boolean) {
+        const select = [...Object.keys(data), 'version', 'modifiedAt'];
+        return super.update(uuid, { ...data, preserveVersion: true }, showErrors, select);
     }
 
-    private async updateManifest(collectionUuid: string, files: CollectionFile[]): Promise<CollectionResource> {
-        const collection = await this.get(collectionUuid);
-        const manifest: KeepManifestStream[] = parseKeepManifestText(collection.manifestText);
+    async files(uuid: string) {
+        const request = await this.webdavClient.propfind(`c=${uuid}`);
+        if (request.responseXML != null) {
+            return extractFilesData(request.responseXML);
+        }
+        return Promise.reject();
+    }
 
-        files.forEach(f => {
-            let kms = manifest.find(stream => stream.name === f.path);
-            if (!kms) {
-                kms = {
-                    files: [],
-                    locators: [],
-                    name: f.path
-                };
-                manifest.push(kms);
+    private combineFilePath(parts: string[]) {
+        return parts.reduce((path, part) => {
+            // Trim leading and trailing slashes
+            const trimmedPart = part.split('/').filter(Boolean).join('/');
+            if (trimmedPart.length) {
+                const separator = path.endsWith('/') ? '' : '/';
+                return `${path}${separator}${trimmedPart}`;
+            } else {
+                return path;
             }
-            kms.locators.push(f.id);
-            const len = kms.files.length;
-            const nextPos = len > 0
-                ? parseInt(kms.files[len - 1].position, 10) + kms.files[len - 1].size
-                : 0;
-            kms.files.push({
-                name: f.name,
-                position: nextPos.toString(),
-                size: f.size
-            });
+        }, "/");
+    }
+
+    private replaceFiles(collectionUuid: string, fileMap: {}, showErrors?: boolean) {
+        const payload = {
+            collection: {
+                preserve_version: true
+            },
+            replace_files: fileMap
+        };
+
+        return CommonService.defaultResponse(
+            this.serverApi
+                .put<CollectionResource>(`/${this.resourceType}/${collectionUuid}`, payload),
+            this.actions,
+            true, // mapKeys
+            showErrors
+        );
+    }
+
+    async uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress, targetLocation: string = '') {
+        if (collectionUuid === "" || files.length === 0) { return; }
+        // files have to be uploaded sequentially
+        for (let idx = 0; idx < files.length; idx++) {
+            await this.uploadFile(collectionUuid, files[idx], idx, onProgress, targetLocation);
+        }
+        await this.update(collectionUuid, { preserveVersion: true });
+    }
+
+    async renameFile(collectionUuid: string, collectionPdh: string, oldPath: string, newPath: string) {
+        return this.replaceFiles(collectionUuid, {
+            [this.combineFilePath([newPath])]: `${collectionPdh}${this.combineFilePath([oldPath])}`,
+            [this.combineFilePath([oldPath])]: '',
         });
+    }
+
+    extendFileURL = (file: CollectionDirectory | CollectionFile) => {
+        const baseUrl = this.webdavClient.getBaseUrl().endsWith('/')
+            ? this.webdavClient.getBaseUrl().slice(0, -1)
+            : this.webdavClient.getBaseUrl();
+        const apiToken = this.authService.getApiToken();
+        const encodedApiToken = apiToken ? encodeURI(apiToken) : '';
+        const userApiToken = `/t=${encodedApiToken}/`;
+        const splittedPrevFileUrl = file.url.split('/');
+        const url = `${baseUrl}/${splittedPrevFileUrl[1]}${userApiToken}${splittedPrevFileUrl.slice(2).join('/')}`;
+        return {
+            ...file,
+            url
+        };
+    }
 
-        console.log(manifest);
+    async getFileContents(file: CollectionFile) {
+        return (await this.webdavClient.get(`c=${file.id}`)).response;
+    }
 
-        const manifestText = stringifyKeepManifest(manifest);
-        const data = { ...collection, manifestText };
-        return this.update(collectionUuid, CommonResourceService.mapKeys(_.snakeCase)(data));
+    private async uploadFile(collectionUuid: string, file: File, fileId: number, onProgress: UploadProgress = () => { return; }, targetLocation: string = '') {
+        const fileURL = `c=${targetLocation !== '' ? targetLocation : collectionUuid}/${file.name}`.replace('//', '/');
+        const requestConfig = {
+            headers: {
+                'Content-Type': 'text/octet-stream'
+            },
+            onUploadProgress: (e: ProgressEvent) => {
+                onProgress(fileId, e.loaded, e.total, Date.now());
+            },
+        };
+        return this.webdavClient.upload(fileURL, [file], requestConfig);
     }
 
-    uploadFiles(collectionUuid: string, files: File[], onProgress?: UploadProgress): Promise<CollectionResource | never> {
-        const filters = FilterBuilder.create()
-            .addEqual("service_type", "proxy");
+    deleteFiles(collectionUuid: string, files: string[], showErrors?: boolean) {
+        const optimizedFiles = files
+            .sort((a, b) => a.length - b.length)
+            .reduce((acc, currentPath) => {
+                const parentPathFound = acc.find((parentPath) => currentPath.indexOf(`${parentPath}/`) > -1);
 
-        return this.keepService.list({ filters }).then(data => {
-            if (data.items && data.items.length > 0) {
-                const serviceHost =
-                    (data.items[0].serviceSslFlag ? "https://" : "http://") +
-                    data.items[0].serviceHost +
-                    ":" + data.items[0].servicePort;
+                if (!parentPathFound) {
+                    return [...acc, currentPath];
+                }
 
-                console.log("serviceHost", serviceHost);
+                return acc;
+            }, []);
 
-                const files$ = files.map((f, idx) => this.uploadFile(serviceHost, f, idx, onProgress));
-                return Promise.all(files$).then(values => {
-                    return this.updateManifest(collectionUuid, values);
-                });
-            } else {
-                return Promise.reject("Missing keep service host");
+        const fileMap = optimizedFiles.reduce((obj, filePath) => {
+            return {
+                ...obj,
+                [this.combineFilePath([filePath])]: ''
             }
-        });
+        }, {})
+
+        return this.replaceFiles(collectionUuid, fileMap, showErrors);
+    }
+
+    copyFiles(sourcePdh: string, files: string[], destinationCollectionUuid: string, destinationPath: string, showErrors?: boolean) {
+        const fileMap = files.reduce((obj, sourceFile) => {
+            const sourceFileName = sourceFile.split('/').filter(Boolean).slice(-1).join("");
+            return {
+                ...obj,
+                [this.combineFilePath([destinationPath, sourceFileName])]: `${sourcePdh}${this.combineFilePath([sourceFile])}`
+            };
+        }, {});
+
+        return this.replaceFiles(destinationCollectionUuid, fileMap, showErrors);
     }
+
+    moveFiles(sourceUuid: string, sourcePdh: string, files: string[], destinationCollectionUuid: string, destinationPath: string, showErrors?: boolean) {
+        if (sourceUuid === destinationCollectionUuid) {
+            const fileMap = files.reduce((obj, sourceFile) => {
+                const sourceFileName = sourceFile.split('/').filter(Boolean).slice(-1).join("");
+                return {
+                    ...obj,
+                    [this.combineFilePath([destinationPath, sourceFileName])]: `${sourcePdh}${this.combineFilePath([sourceFile])}`,
+                    [this.combineFilePath([sourceFile])]: '',
+                };
+            }, {});
+
+            return this.replaceFiles(sourceUuid, fileMap, showErrors)
+        } else {
+            return this.copyFiles(sourcePdh, files, destinationCollectionUuid, destinationPath, showErrors)
+                .then(() => {
+                    return this.deleteFiles(sourceUuid, files, showErrors);
+                });
+        }
+    }
+
+    createDirectory(collectionUuid: string, path: string, showErrors?: boolean) {
+        const fileMap = {[this.combineFilePath([path])]: emptyCollectionUuid};
+
+        return this.replaceFiles(collectionUuid, fileMap, showErrors);
+    }
+
 }