X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/9fed03a84c6ef5234d61f7d62c93f7629da2409d..9f9072365214fbf9d19f0359df90bbf2ab7ae3c9:/src/services/collection-files-service/collection-manifest-mapper.ts diff --git a/src/services/collection-files-service/collection-manifest-mapper.ts b/src/services/collection-files-service/collection-manifest-mapper.ts index c2a8ae8e13..6e64f833e5 100644 --- a/src/services/collection-files-service/collection-manifest-mapper.ts +++ b/src/services/collection-files-service/collection-manifest-mapper.ts @@ -2,10 +2,23 @@ // // SPDX-License-Identifier: AGPL-3.0 -import { uniqBy } from 'lodash'; -import { KeepManifestStream, KeepManifestStreamFile, KeepManifest } from "../../models/keep-manifest"; -import { TreeNode, setNode, createTree } from '../../models/tree'; -import { CollectionFilesTree, CollectionFile, CollectionDirectory, createCollectionDirectory, createCollectionFile } from '../../models/collection-file'; +import { uniqBy, groupBy } from 'lodash'; +import { KeepManifestStream, KeepManifestStreamFile, KeepManifest } from "~/models/keep-manifest"; +import { TreeNode, setNode, createTree, getNodeDescendantsIds, getNodeValue, TreeNodeStatus } from '~/models/tree'; +import { CollectionFilesTree, CollectionFile, CollectionDirectory, createCollectionDirectory, createCollectionFile, CollectionFileType } from '../../models/collection-file'; + +export const mapCollectionFilesTreeToManifest = (tree: CollectionFilesTree): KeepManifest => { + const values = getNodeDescendantsIds('')(tree).map(id => getNodeValue(id)(tree)); + const files = values.filter(value => value && value.type === CollectionFileType.FILE) as CollectionFile[]; + const fileGroups = groupBy(files, file => file.path); + return Object + .keys(fileGroups) + .map(dirName => ({ + name: dirName, + locators: [], + files: fileGroups[dirName].map(mapCollectionFile) + })); +}; export const mapManifestToCollectionFilesTree = (manifest: KeepManifest): CollectionFilesTree => manifestToCollectionFiles(manifest) @@ -16,8 +29,12 @@ export const mapManifestToCollectionFilesTree = (manifest: KeepManifest): Collec export const mapCollectionFileToTreeNode = (file: CollectionFile): TreeNode => ({ children: [], id: file.id, - parent: file.parentId, - value: file + parent: file.path, + value: file, + active: false, + selected: false, + expanded: false, + status: TreeNodeStatus.INITIAL, }); export const manifestToCollectionFiles = (manifest: KeepManifest): Array => ([ @@ -47,7 +64,7 @@ const splitDirectory = (directory: CollectionDirectory): CollectionDirectory[] = const mapPathComponentToDirectory = (component: string, index: number, components: string[]): CollectionDirectory => createCollectionDirectory({ - parentId: index === 0 ? '' : joinPathComponents(components, index), + path: index === 0 ? '' : joinPathComponents(components, index), id: joinPathComponents(components, index + 1), name: component, }); @@ -55,9 +72,15 @@ const mapPathComponentToDirectory = (component: string, index: number, component const joinPathComponents = (components: string[], index: number) => `/${components.slice(0, index).join('/')}`; +const mapCollectionFile = (file: CollectionFile): KeepManifestStreamFile => ({ + name: file.name, + position: '', + size: file.size +}); + const mapStreamDirectory = (stream: KeepManifestStream): CollectionDirectory => createCollectionDirectory({ - parentId: '', + path: '', id: stream.name, name: stream.name, }); @@ -65,7 +88,7 @@ const mapStreamDirectory = (stream: KeepManifestStream): CollectionDirectory => const mapStreamFile = (stream: KeepManifestStream) => (file: KeepManifestStreamFile): CollectionFile => createCollectionFile({ - parentId: stream.name, + path: stream.name, id: `${stream.name}/${file.name}`, name: file.name, size: file.size,