1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import { KeepManifestStream, KeepManifestStreamFile, KeepManifest } from "~/models/keep-manifest";
8 * Documentation [http://doc.arvados.org/api/storage.html](http://doc.arvados.org/api/storage.html)
10 export const parseKeepManifestText: (text: string) => KeepManifestStream[] = (text: string) =>
13 .filter(streamText => streamText.length > 0)
14 .map(parseKeepManifestStream);
17 * Documentation [http://doc.arvados.org/api/storage.html](http://doc.arvados.org/api/storage.html)
19 export const parseKeepManifestStream = (stream: string): KeepManifestStream => {
20 const tokens = stream.split(' ');
22 name: streamName(tokens),
23 locators: locators(tokens),
28 export const stringifyKeepManifest = (manifest: KeepManifest) =>
29 manifest.map(stringifyKeepManifestStream).join('');
31 export const stringifyKeepManifestStream = (stream: KeepManifestStream) =>
32 `.${stream.name} ${stream.locators.join(' ')} ${stream.files.map(stringifyFile).join(' ')}\n`;
34 const FILE_LOCATOR_REGEXP = /^([0-9a-f]{32})\+([0-9]+)(\+[A-Z][-A-Za-z0-9@_]*)*$/;
36 const FILE_REGEXP = /([0-9]+):([0-9]+):(.*)/;
38 const streamName = (tokens: string[]) => tokens[0].slice(1);
40 const locators = (tokens: string[]) => tokens.filter(isFileLocator);
42 const files = (tokens: string[]) => tokens.filter(isFile).map(parseFile);
44 const isFileLocator = (token: string) => FILE_LOCATOR_REGEXP.test(token);
46 const isFile = (token: string) => FILE_REGEXP.test(token);
48 const parseFile = (token: string): KeepManifestStreamFile => {
49 const match = FILE_REGEXP.exec(token);
50 const [position, size, name] = match!.slice(1);
51 return { name, position, size: parseInt(size, 10) };
54 const stringifyFile = (file: KeepManifestStreamFile) =>
55 `${file.position}:${file.size}:${file.name}`;