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