Merge changes from webdav-service
authorMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 13 Aug 2018 07:19:20 +0000 (09:19 +0200)
committerMichal Klobukowski <michal.klobukowski@contractors.roche.com>
Mon, 13 Aug 2018 07:19:20 +0000 (09:19 +0200)
Feature #13990

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

1  2 
src/services/collection-service/collection-service.ts
src/services/services.ts

index 32eeac4f74ef8d6a3eeeee92948a4047f49b3223,4d750362e3972c339d95ca27f18aff7d3bed82e0..47268255d9605efde6e3842c791acc0ed9bcb35d
@@@ -7,66 -7,18 +7,66 @@@ import { CollectionResource } from "../
  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 { CollectionFile, createCollectionFile, createCollectionDirectory, createCollectionFilesTree } 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 { 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");
      }
  
-                 const href = this.webdavClient.defaults.baseUrl + pathname + '?api_token=' + this.authService.getApiToken();
 +    async files(uuid: string) {
 +        const request = await this.webdavClient.propfind(`/c=${uuid}`);
 +        if (request.responseXML != null) {
 +            return createCollectionFilesTree(this.extractFilesData(request.responseXML));
 +        }
 +        return Promise.reject();
 +    }
 +
 +    async deleteFile(collectionUuid: string, filePath: string){
 +        return this.webdavClient.delete(`/c=${collectionUuid}${filePath}`);
 +    }
 +
 +
 +    extractFilesData(document: Document) {
 +        return Array
 +            .from(document.getElementsByTagName('D:response'))
 +            .slice(1)
 +            .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 ? parseInt(sizeElement.innerHTML, 10) : 0;
 +
 +                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();
 +
 +                const data = {
 +                    url: href,
 +                    id: `${directory}/${name}`,
 +                    name,
 +                    path: directory,
 +                };
 +
 +                const [resourceTypeElement] = Array.from(element.getElementsByTagName('D:resourcetype'));
 +                return resourceTypeElement && resourceTypeElement.innerHTML === ''
 +                    ? createCollectionFile({ ...data, size })
 +                    : createCollectionDirectory(data);
 +
 +            });
 +    }
 +
 +
      private readFile(file: File): Promise<ArrayBuffer> {
          return new Promise<ArrayBuffer>(resolve => {
              const reader = new FileReader();
diff --combined src/services/services.ts
index cbf8cc3e45dfab1a0768a249145c838f6b1865b6,45d360c19a470f77a0fd5073a90e1df94d06cca7..eedd1240c7f7ced2495b3966591b311810efa1d7
@@@ -19,18 -19,18 +19,18 @@@ export type ServiceRepository = ReturnT
  
  export const createServices = (config: Config) => {
      const apiClient = Axios.create();
-     apiClient.defaults.baseURL = `${config.API_HOST}/arvados/v1`;
+     apiClient.defaults.baseURL = `${config.apiHost}/arvados/v1`;
  
-     const webdavClient = WebDAV.create();
-     webdavClient.defaults.baseUrl = config.KEEP_WEB_HOST;
+     const webdavClient = new WebDAV();
+     webdavClient.defaults.baseURL = config.keepWebHost;
  
-     const authService = new AuthService(apiClient, config.API_HOST);
+     const authService = new AuthService(apiClient, config.apiHost);
      const keepService = new KeepService(apiClient);
      const groupsService = new GroupsService(apiClient);
      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);