1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { uniqBy, groupBy } from 'lodash';
6 import { KeepManifestStream, KeepManifestStreamFile, KeepManifest } from "~/models/keep-manifest";
7 import { TreeNode, setNode, createTree, getNodeDescendantsIds, getNodeValue } from '~/models/tree';
8 import { CollectionFilesTree, CollectionFile, CollectionDirectory, createCollectionDirectory, createCollectionFile, CollectionFileType } from '../../models/collection-file';
10 export const mapCollectionFilesTreeToManifest = (tree: CollectionFilesTree): KeepManifest => {
11 const values = getNodeDescendantsIds('')(tree).map(id => getNodeValue(id)(tree));
12 const files = values.filter(value => value && value.type === CollectionFileType.FILE) as CollectionFile[];
13 const fileGroups = groupBy(files, file => file.path);
19 files: fileGroups[dirName].map(mapCollectionFile)
23 export const mapManifestToCollectionFilesTree = (manifest: KeepManifest): CollectionFilesTree =>
24 manifestToCollectionFiles(manifest)
25 .map(mapCollectionFileToTreeNode)
26 .reduce((tree, node) => setNode(node)(tree), createTree<CollectionFile>());
29 export const mapCollectionFileToTreeNode = (file: CollectionFile): TreeNode<CollectionFile> => ({
36 export const manifestToCollectionFiles = (manifest: KeepManifest): Array<CollectionDirectory | CollectionFile> => ([
37 ...mapManifestToDirectories(manifest),
38 ...mapManifestToFiles(manifest)
41 export const mapManifestToDirectories = (manifest: KeepManifest): CollectionDirectory[] =>
44 .map(mapStreamDirectory)
46 .reduce((all, splitted) => ([...all, ...splitted]), []),
47 directory => directory.id);
49 export const mapManifestToFiles = (manifest: KeepManifest): CollectionFile[] =>
51 .map(stream => stream.files.map(mapStreamFile(stream)))
52 .reduce((all, current) => ([...all, ...current]), []);
54 const splitDirectory = (directory: CollectionDirectory): CollectionDirectory[] => {
58 .map(mapPathComponentToDirectory);
61 const mapPathComponentToDirectory = (component: string, index: number, components: string[]): CollectionDirectory =>
62 createCollectionDirectory({
63 path: index === 0 ? '' : joinPathComponents(components, index),
64 id: joinPathComponents(components, index + 1),
68 const joinPathComponents = (components: string[], index: number) =>
69 `/${components.slice(0, index).join('/')}`;
71 const mapCollectionFile = (file: CollectionFile): KeepManifestStreamFile => ({
77 const mapStreamDirectory = (stream: KeepManifestStream): CollectionDirectory =>
78 createCollectionDirectory({
84 const mapStreamFile = (stream: KeepManifestStream) =>
85 (file: KeepManifestStreamFile): CollectionFile =>
86 createCollectionFile({
88 id: `${stream.name}/${file.name}`,