Merge branch '13855-data-collection-files-card'
[arvados-workbench2.git] / src / services / collection-files-service / collection-manifest-parser.ts
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import { KeepManifestStream, KeepManifestStreamFile } from "../../models/keep-manifest";
6
7 /**
8  * Documentation [http://doc.arvados.org/api/storage.html](http://doc.arvados.org/api/storage.html)
9  */
10 export const parseKeepManifestText = (text: string) =>
11     text
12         .split(/\n/)
13         .filter(streamText => streamText.length > 0)
14         .map(parseKeepManifestStream);
15
16 /**
17  * Documentation [http://doc.arvados.org/api/storage.html](http://doc.arvados.org/api/storage.html)
18  */
19 export const parseKeepManifestStream = (stream: string): KeepManifestStream => {
20     const tokens = stream.split(' ');
21     return {
22         name: streamName(tokens),
23         locators: locators(tokens),
24         files: files(tokens)
25     };
26 };
27
28 const FILE_LOCATOR_REGEXP = /^([0-9a-f]{32})\+([0-9]+)(\+[A-Z][-A-Za-z0-9@_]*)*$/;
29
30 const FILE_REGEXP = /([0-9]+):([0-9]+):(.*)/;
31
32 const streamName = (tokens: string[]) => tokens[0].slice(1);
33
34 const locators = (tokens: string[]) => tokens.filter(isFileLocator);
35
36 const files = (tokens: string[]) => tokens.filter(isFile).map(parseFile);
37
38 const isFileLocator = (token: string) => FILE_LOCATOR_REGEXP.test(token);
39
40 const isFile = (token: string) => FILE_REGEXP.test(token);
41
42 const parseFile = (token: string): KeepManifestStreamFile => {
43     const match = FILE_REGEXP.exec(token);
44     const [position, size, name] = match!.slice(1);
45     return { name, position, size: parseInt(size, 10) };
46 };