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     streamName: string;
9     locators: string[];
10     files: Array<KeepManifestStreamFile>;
11 }
12
13 export interface KeepManifestStreamFile {
14     fileName: 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.split(/\n/).map(parseKeepManifestStream);
24
25 /**
26  * Documentation [http://doc.arvados.org/api/storage.html](http://doc.arvados.org/api/storage.html)
27  */
28 export const parseKeepManifestStream = (stream: string): KeepManifestStream => {
29     const tokens = stream.split(' ');
30     return {
31         streamName: streamName(tokens),
32         locators: locators(tokens),
33         files: files(tokens)
34     };
35 };
36
37 const FILE_LOCATOR_REGEXP = /^([0-9a-f]{32})\+([0-9]+)(\+[A-Z][-A-Za-z0-9@_]*)*$/;
38
39 const FILE_REGEXP = /([0-9]+):([0-9]+):(.*)/;
40
41 const streamName = (tokens: string[]) => tokens[0];
42
43 const locators = (tokens: string[]) => tokens.filter(isFileLocator);
44
45 const files = (tokens: string[]) => tokens.filter(isFile).map(parseFile);
46
47 const isFileLocator = (token: string) => FILE_LOCATOR_REGEXP.test(token);
48
49 const isFile = (token: string) => FILE_REGEXP.test(token);
50
51 const parseFile = (token: string): KeepManifestStreamFile => {
52     const match = FILE_REGEXP.exec(token);
53     const [position, size, fileName] = match!.slice(1);
54     return { fileName, position, size: parseInt(size, 10) };
55 };