1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { uniqBy } from 'lodash';
6 import { KeepManifestStream, KeepManifestStreamFile, KeepManifest } from "./keep-manifest";
7 import { Tree, TreeNode, setNode, createTree } from './tree';
9 export type CollectionFilesTree = Tree<CollectionDirectory | CollectionFile>;
11 export enum CollectionFileType {
12 DIRECTORY = 'directory',
16 export interface CollectionDirectory {
20 type: CollectionFileType.DIRECTORY;
23 export interface CollectionFile {
28 type: CollectionFileType.FILE;
31 export const mapManifestToCollectionFilesTree = (manifest: KeepManifest): CollectionFilesTree =>
32 manifestToCollectionFiles(manifest)
33 .map(mapCollectionFileToTreeNode)
34 .reduce((tree, node) => setNode(node)(tree), createTree<CollectionFile>());
37 export const mapCollectionFileToTreeNode = (file: CollectionFile): TreeNode<CollectionFile> => ({
40 parent: file.parentId,
44 export const manifestToCollectionFiles = (manifest: KeepManifest): Array<CollectionDirectory | CollectionFile> => ([
45 ...mapManifestToDirectories(manifest),
46 ...mapManifestToFiles(manifest)
49 export const mapManifestToDirectories = (manifest: KeepManifest): CollectionDirectory[] =>
52 .map(mapStreamDirectory)
54 .reduce((all, splitted) => ([...all, ...splitted]), []),
55 directory => directory.id);
57 export const mapManifestToFiles = (manifest: KeepManifest): CollectionFile[] =>
59 .map(stream => stream.files.map(mapStreamFile(stream)))
60 .reduce((all, current) => ([...all, ...current]), []);
62 const splitDirectory = (directory: CollectionDirectory): CollectionDirectory[] => {
66 .map(mapPathComponentToDirectory);
69 const mapPathComponentToDirectory = (component: string, index: number, components: string[]): CollectionDirectory =>
71 parentId: index === 0 ? '' : joinPathComponents(components, index),
72 id: joinPathComponents(components, index + 1),
76 const joinPathComponents = (components: string[], index: number) =>
77 `/${components.slice(0, index).join('/')}`;
79 const mapStreamDirectory = (stream: KeepManifestStream): CollectionDirectory =>
86 const mapStreamFile = (stream: KeepManifestStream) =>
87 (file: KeepManifestStreamFile): CollectionFile =>
89 parentId: stream.name,
90 id: `${stream.name}/${file.name}`,
95 export const createDirectory = (data: Partial<CollectionDirectory>): CollectionDirectory => ({
99 type: CollectionFileType.DIRECTORY,
103 export const createFile = (data: Partial<CollectionFile>): CollectionFile => ({
108 type: CollectionFileType.FILE,