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, TreeNodeStatus } 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> => ({
37 status: TreeNodeStatus.INITIAL,
40 export const manifestToCollectionFiles = (manifest: KeepManifest): Array<CollectionDirectory | CollectionFile> => ([
41 ...mapManifestToDirectories(manifest),
42 ...mapManifestToFiles(manifest)
45 export const mapManifestToDirectories = (manifest: KeepManifest): CollectionDirectory[] =>
48 .map(mapStreamDirectory)
50 .reduce((all, splitted) => ([...all, ...splitted]), []),
51 directory => directory.id);
53 export const mapManifestToFiles = (manifest: KeepManifest): CollectionFile[] =>
55 .map(stream => stream.files.map(mapStreamFile(stream)))
56 .reduce((all, current) => ([...all, ...current]), []);
58 const splitDirectory = (directory: CollectionDirectory): CollectionDirectory[] => {
62 .map(mapPathComponentToDirectory);
65 const mapPathComponentToDirectory = (component: string, index: number, components: string[]): CollectionDirectory =>
66 createCollectionDirectory({
67 path: index === 0 ? '' : joinPathComponents(components, index),
68 id: joinPathComponents(components, index + 1),
72 const joinPathComponents = (components: string[], index: number) =>
73 `/${components.slice(0, index).join('/')}`;
75 const mapCollectionFile = (file: CollectionFile): KeepManifestStreamFile => ({
81 const mapStreamDirectory = (stream: KeepManifestStream): CollectionDirectory =>
82 createCollectionDirectory({
88 const mapStreamFile = (stream: KeepManifestStream) =>
89 (file: KeepManifestStreamFile): CollectionFile =>
90 createCollectionFile({
92 id: `${stream.name}/${file.name}`,