UNPKG

4.8 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const Path = require("path");
4const fs_extra_plus_1 = require("fs-extra-plus");
5//#region ---------- Common Bucket Utils ----------
6/**
7 * Build the full destination path from the local path name and the destPath
8 * - If `destPath` ends with `/`, then baseName of `localPath` is concatenated.
9 * - Otherwise, `destPath` is the fulLDestPath.
10 *
11 * @throws exception if destPath is not present.
12 */
13function buildFullDestPath(localPath, destPath) {
14 // we we do not have a dest path, throw error
15 if (!destPath) {
16 throw new Error('No depthPath');
17 }
18 let fullDestPath;
19 // if it is a folder, we just concatinate the base name
20 if (destPath.endsWith('/')) {
21 const srcBaseName = Path.basename(localPath);
22 fullDestPath = destPath + srcBaseName;
23 }
24 // if the destPath is not a folder, assume it is the new file name.
25 else {
26 fullDestPath = destPath;
27 }
28 return fullDestPath;
29}
30exports.buildFullDestPath = buildFullDestPath;
31/**
32 * Return a clean prefix and glob when defined in the string. Clean prefix, meaning, glob less one,
33 * that can be passed to most cloud storage api.
34 *
35 * @param prefixOrGlob undefined, null, e.g., 'some-prefix', 'folder/', 'folder/glob-pattern.*'
36 * @returns {prefix, glob, baseDir}
37 * - prefix is the first characters unitl the first glob character ('*')
38 * - glob is prefixOrGlob value if it is a glob, otherwise undefined.
39 * - baseDir is the eventual longest directory path without any glob char (ending with '/')
40 */
41function extractPrefixAndGlob(prefixOrGlob) {
42 let glob;
43 let prefix;
44 let baseDir;
45 if (prefixOrGlob && prefixOrGlob.length > 0) {
46 const firstWildIdx = prefixOrGlob.indexOf('*');
47 // if it has a '*' then it is a pattern
48 if (firstWildIdx > 0) {
49 glob = prefixOrGlob;
50 prefix = prefixOrGlob.substring(0, firstWildIdx);
51 }
52 // otherwise, it is just a
53 else {
54 prefix = prefixOrGlob;
55 }
56 }
57 if (prefix) {
58 const lastSlashIdx = prefix.lastIndexOf('/');
59 if (lastSlashIdx > -1) {
60 baseDir = prefix.substring(0, lastSlashIdx + 1);
61 }
62 }
63 return { prefix, glob, baseDir };
64}
65exports.extractPrefixAndGlob = extractPrefixAndGlob;
66async function commonBucketDownload(bucket, cloudFiles, pathOrGlob, localDir, downloadr) {
67 const files = [];
68 const { baseDir } = extractPrefixAndGlob(pathOrGlob);
69 for (let cf of cloudFiles) {
70 const remotePath = bucket.getPath(cf);
71 const localPath = getDestPath(baseDir, remotePath, localDir);
72 const localPathDir = Path.dirname(localPath);
73 await fs_extra_plus_1.mkdirp(localPathDir);
74 process.stdout.write(`Downloading ${bucket.type}://${bucket.name}/${remotePath} to ${localPath}`);
75 try {
76 await downloadr(cf, localPath);
77 process.stdout.write(` - DONE\n`);
78 const file = { bucket, path: remotePath, size: -1, local: localPath };
79 files.push(file);
80 }
81 catch (ex) {
82 process.stdout.write(` - FAIL - ABORT - Cause: ${ex}\n`);
83 throw ex;
84 }
85 }
86 return files;
87}
88exports.commonBucketDownload = commonBucketDownload;
89async function commonBucketCopy(bucket, cloudFiles, pathOrGlob, destDir, copier) {
90 const destBucket = ((typeof destDir === 'string') ? bucket : destDir.bucket);
91 const destPathDir = (typeof destDir === 'string') ? destDir : destDir.path;
92 // check if destPathDir is a dir (must end with `/`)
93 if (!destPathDir.endsWith('/')) {
94 throw new Error(`FATAL - CS ERROR - destDir must end with '/', but was '${destPathDir}')`);
95 }
96 const { baseDir } = extractPrefixAndGlob(pathOrGlob);
97 const files = [];
98 for (let cf of cloudFiles) {
99 const remotePath = bucket.getPath(cf);
100 const destPath = getDestPath(baseDir, remotePath, destPathDir);
101 process.stdout.write(`Copying ${bucket.type}://${bucket.name}/${remotePath} to ${bucket.type}://${destBucket.name}/${destPath}`);
102 try {
103 await copier(cf, { bucket: destBucket, path: destPath });
104 process.stdout.write(` - DONE\n`);
105 }
106 catch (ex) {
107 process.stdout.write(` - FAIL - ABORT - Cause: ${ex}\n`);
108 throw ex;
109 }
110 }
111 return files;
112}
113exports.commonBucketCopy = commonBucketCopy;
114//#endregion ---------- /Common Bucket Utils ----------
115function getDestPath(baseDir, remotePath, destPathDir) {
116 const baseName = Path.basename(remotePath);
117 const filePath = (baseDir) ? Path.relative(baseDir, remotePath) : baseName;
118 const destPath = `${destPathDir}${filePath}`;
119 return destPath;
120}