X-Git-Url: https://git.arvados.org/arvados-workbench2.git/blobdiff_plain/72c12f527787181e4abb09fc968f07e795179c3c..4b956bd2e3bcccdeb808df7391d135659cf85b94:/src/models/collection-file.ts diff --git a/src/models/collection-file.ts b/src/models/collection-file.ts index 3a7e55c2..91008d1f 100644 --- a/src/models/collection-file.ts +++ b/src/models/collection-file.ts @@ -2,9 +2,8 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { uniqBy } from 'lodash'; -import { KeepManifestStream, KeepManifestStreamFile, KeepManifest } from "./keep-manifest"; -import { Tree, TreeNode, setNode, createTree } from './tree'; +import { Tree, createTree, setNode, TreeNodeStatus } from './tree'; +import { head, split, pipe, join } from 'lodash/fp'; export type CollectionFilesTree = Tree; @@ -14,97 +13,68 @@ export enum CollectionFileType { } export interface CollectionDirectory { - parentId: string; + path: string; + url: string; id: string; name: string; type: CollectionFileType.DIRECTORY; } export interface CollectionFile { - parentId: string; + path: string; + url: string; id: string; name: string; size: number; type: CollectionFileType.FILE; } -export const mapManifestToCollectionFilesTree = (manifest: KeepManifest): CollectionFilesTree => - manifestToCollectionFiles(manifest) - .map(mapCollectionFileToTreeNode) - .reduce((tree, node) => setNode(node)(tree), createTree()); - - -export const mapCollectionFileToTreeNode = (file: CollectionFile): TreeNode => ({ - children: [], - id: file.id, - parent: file.parentId, - value: file -}); - -export const manifestToCollectionFiles = (manifest: KeepManifest): Array => ([ - ...mapManifestToDirectories(manifest), - ...mapManifestToFiles(manifest) -]); - -export const mapManifestToDirectories = (manifest: KeepManifest): CollectionDirectory[] => - uniqBy( - manifest - .map(mapStreamDirectory) - .map(splitDirectory) - .reduce((all, splitted) => ([...all, ...splitted]), []), - directory => directory.id); - -export const mapManifestToFiles = (manifest: KeepManifest): CollectionFile[] => - manifest - .map(stream => stream.files.map(mapStreamFile(stream))) - .reduce((all, current) => ([...all, ...current]), []); - -const splitDirectory = (directory: CollectionDirectory): CollectionDirectory[] => { - return directory.name - .split('/') - .slice(1) - .map(mapPathComponentToDirectory); -}; - -const mapPathComponentToDirectory = (component: string, index: number, components: string[]): CollectionDirectory => - createDirectory({ - parentId: index === 0 ? '' : joinPathComponents(components, index), - id: joinPathComponents(components, index + 1), - name: component, - }); - -const joinPathComponents = (components: string[], index: number) => - `/${components.slice(0, index).join('/')}`; - -const mapStreamDirectory = (stream: KeepManifestStream): CollectionDirectory => - createDirectory({ - parentId: '', - id: stream.name, - name: stream.name, - }); - -const mapStreamFile = (stream: KeepManifestStream) => - (file: KeepManifestStreamFile): CollectionFile => - createFile({ - parentId: stream.name, - id: `${stream.name}/${file.name}`, - name: file.name, - size: file.size, - }); +export interface CollectionUploadFile { + name: string; +} -export const createDirectory = (data: Partial): CollectionDirectory => ({ +export const createCollectionDirectory = (data: Partial): CollectionDirectory => ({ id: '', name: '', - parentId: '', + path: '', + url: '', type: CollectionFileType.DIRECTORY, ...data }); -export const createFile = (data: Partial): CollectionFile => ({ +export const createCollectionFile = (data: Partial): CollectionFile => ({ id: '', name: '', - parentId: '', + path: '', + url: '', size: 0, type: CollectionFileType.FILE, ...data -}); \ No newline at end of file +}); + +export const createCollectionFilesTree = (data: Array, joinParents: Boolean = true) => { + const directories = data.filter(item => item.type === CollectionFileType.DIRECTORY); + directories.sort((a, b) => a.path.localeCompare(b.path)); + const files = data.filter(item => item.type === CollectionFileType.FILE); + return [...directories, ...files] + .reduce((tree, item) => setNode({ + children: [], + id: item.id, + parent: joinParents ? getParentId(item) : '', + value: item, + active: false, + selected: false, + expanded: false, + status: TreeNodeStatus.INITIAL + })(tree), createTree()); +}; + +const getParentId = (item: CollectionDirectory | CollectionFile) => + item.path + ? join('', [getCollectionId(item.id), item.path]) + : item.path; + +const getCollectionId = pipe( + split('/'), + head, +); \ No newline at end of file