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();
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);