Add typescript paths to top level folders
[arvados-workbench2.git] / src / services / collection-files-service / collection-manifest-mapper.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { uniqBy, groupBy } from 'lodash';
6 import { KeepManifestStream, KeepManifestStreamFile, KeepManifest } from "~/models/keep-manifest";
7 import { TreeNode, setNode, createTree, getNodeDescendants, getNodeValue } from '~/models/tree';
8 import { CollectionFilesTree, CollectionFile, CollectionDirectory, createCollectionDirectory, createCollectionFile, CollectionFileType } from '../../models/collection-file';
9
10 export const mapCollectionFilesTreeToManifest = (tree: CollectionFilesTree): KeepManifest => {
11     const values = getNodeDescendants('')(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);
14     return Object
15         .keys(fileGroups)
16         .map(dirName => ({
17             name: dirName,
18             locators: [],
19             files: fileGroups[dirName].map(mapCollectionFile)
20         }));
21 };
22
23 export const mapManifestToCollectionFilesTree = (manifest: KeepManifest): CollectionFilesTree =>
24     manifestToCollectionFiles(manifest)
25         .map(mapCollectionFileToTreeNode)
26         .reduce((tree, node) => setNode(node)(tree), createTree<CollectionFile>());
27
28
29 export const mapCollectionFileToTreeNode = (file: CollectionFile): TreeNode<CollectionFile> => ({
30     children: [],
31     id: file.id,
32     parent: file.path,
33     value: file
34 });
35
36 export const manifestToCollectionFiles = (manifest: KeepManifest): Array<CollectionDirectory | CollectionFile> => ([
37     ...mapManifestToDirectories(manifest),
38     ...mapManifestToFiles(manifest)
39 ]);
40
41 export const mapManifestToDirectories = (manifest: KeepManifest): CollectionDirectory[] =>
42     uniqBy(
43         manifest
44             .map(mapStreamDirectory)
45             .map(splitDirectory)
46             .reduce((all, splitted) => ([...all, ...splitted]), []),
47         directory => directory.id);
48
49 export const mapManifestToFiles = (manifest: KeepManifest): CollectionFile[] =>
50     manifest
51         .map(stream => stream.files.map(mapStreamFile(stream)))
52         .reduce((all, current) => ([...all, ...current]), []);
53
54 const splitDirectory = (directory: CollectionDirectory): CollectionDirectory[] => {
55     return directory.name
56         .split('/')
57         .slice(1)
58         .map(mapPathComponentToDirectory);
59 };
60
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),
65         name: component,
66     });
67
68 const joinPathComponents = (components: string[], index: number) =>
69     `/${components.slice(0, index).join('/')}`;
70
71 const mapCollectionFile = (file: CollectionFile): KeepManifestStreamFile => ({
72     name: file.name,
73     position: '',
74     size: file.size
75 });
76
77 const mapStreamDirectory = (stream: KeepManifestStream): CollectionDirectory =>
78     createCollectionDirectory({
79         path: '',
80         id: stream.name,
81         name: stream.name,
82     });
83
84 const mapStreamFile = (stream: KeepManifestStream) =>
85     (file: KeepManifestStreamFile): CollectionFile =>
86         createCollectionFile({
87             path: stream.name,
88             id: `${stream.name}/${file.name}`,
89             name: file.name,
90             size: file.size,
91         });
92