Create method for extracting files info from propfind response
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 9 Aug 2018 15:59:49 +0000 (17:59 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Thu, 9 Aug 2018 15:59:49 +0000 (17:59 +0200)
Feature #13990

Arvados-DCO-1.1-Signed-off-by: Michal Klobukowski <michal.klobukowski@contractors.roche.com>

src/common/config.ts
src/services/collection-service/collection-service.ts
src/services/services.ts
src/store/collection-panel/collection-panel-action.ts

index 5391927e21dd7525a72dd5a97abb3273d76f454b..740f6f723b23dedb9366ff96b601fdb1710be7e4 100644 (file)
@@ -21,7 +21,8 @@ export const fetchConfig = () => {
 
 const mapConfig = (config: Config): Config => ({
     ...config,
-    API_HOST: addProtocol(config.API_HOST)
+    API_HOST: addProtocol(config.API_HOST),
+    KEEP_WEB_HOST: addProtocol(config.KEEP_WEB_HOST)
 });
 
 const getDefaultConfig = (): Config => ({
index 4d750362e3972c339d95ca27f18aff7d3bed82e0..c97307ea196f4e12c77461b81f4e6e1b2249f985 100644 (file)
@@ -11,14 +11,54 @@ import { CollectionFile, createCollectionFile } from "../../models/collection-fi
 import { parseKeepManifestText, stringifyKeepManifest } from "../collection-files-service/collection-manifest-parser";
 import * as _ from "lodash";
 import { KeepManifestStream } from "../../models/keep-manifest";
+import { WebDAV } from "../../common/webdav";
+import { AuthService } from "../auth-service/auth-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) {
+    constructor(serverApi: AxiosInstance, private keepService: KeepService, private webdavClient: WebDAV, private authService: AuthService) {
         super(serverApi, "collections");
     }
 
+    async files(uuid: string) {
+        const request = await this.webdavClient.propfind(`/c=${uuid}`);
+        if (request.responseXML != null) {
+            return this.extractFilesData(request.responseXML);
+        }
+        return Promise.reject();
+    }
+
+    extractFilesData(document: Document) {
+        return Array
+            .from(document.getElementsByTagName('D:response'))
+            .filter(element => {
+                const [resourceTypeElement] = Array.from(element.getElementsByTagName('D:resourcetype'));
+                return resourceTypeElement && resourceTypeElement.innerHTML === '';
+            })
+            .map(element => {
+                const [displayNameElement] = Array.from(element.getElementsByTagName('D:displayname'));
+                const name = displayNameElement ? displayNameElement.innerHTML : undefined;
+
+                const [sizeElement] = Array.from(element.getElementsByTagName('D:getcontentlength'));
+                const size = sizeElement ? sizeElement.innerHTML : undefined;
+
+                const [hrefElement] = Array.from(element.getElementsByTagName('D:href'));
+                const pathname = hrefElement ? hrefElement.innerHTML : undefined;
+                const directory = pathname && pathname.replace(/\/c=[0-9a-zA-Z\-]*/, '').replace(`/${name || ''}`, '');
+
+                const href = this.webdavClient.defaults.baseUrl + pathname + '?api_token=' + this.authService.getApiToken();
+
+                return {
+                    directory,
+                    href,
+                    name,
+                    size,
+                };
+
+            });
+    }
+
     private readFile(file: File): Promise<ArrayBuffer> {
         return new Promise<ArrayBuffer>(resolve => {
             const reader = new FileReader();
index d2a8ce434da601637ed031039670aca1e7075e03..cbf8cc3e45dfab1a0768a249145c838f6b1865b6 100644 (file)
@@ -30,7 +30,7 @@ export const createServices = (config: Config) => {
     const projectService = new ProjectService(apiClient);
     const linkService = new LinkService(apiClient);
     const favoriteService = new FavoriteService(linkService, groupsService);
-    const collectionService = new CollectionService(apiClient, keepService);
+    const collectionService = new CollectionService(apiClient, keepService, webdavClient, authService);
     const tagService = new TagService(linkService);
     const collectionFilesService = new CollectionFilesService(collectionService);
 
index f2774f6fb384f2d72256e57a93bd9cda92d71493..5fe09fbc03322bb647aa624e470efbecd382081d 100644 (file)
@@ -36,6 +36,7 @@ export const loadCollection = (uuid: string, kind: ResourceKind) =>
             .get(uuid)
             .then(item => {
                 dispatch(collectionPanelActions.LOAD_COLLECTION_SUCCESS({ item }));
+                services.collectionService.files(item.uuid).then(console.log);
                 return services.collectionFilesService.getFiles(item.uuid);
             })
             .then(files => {