1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const Path = require("path");
|
4 | const fs_extra_plus_1 = require("fs-extra-plus");
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function buildFullDestPath(localPath, destPath) {
|
14 |
|
15 | if (!destPath) {
|
16 | throw new Error('No depthPath');
|
17 | }
|
18 | let fullDestPath;
|
19 |
|
20 | if (destPath.endsWith('/')) {
|
21 | const srcBaseName = Path.basename(localPath);
|
22 | fullDestPath = destPath + srcBaseName;
|
23 | }
|
24 |
|
25 | else {
|
26 | fullDestPath = destPath;
|
27 | }
|
28 | return fullDestPath;
|
29 | }
|
30 | exports.buildFullDestPath = buildFullDestPath;
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | function extractPrefixAndGlob(prefixOrGlob) {
|
42 | let glob;
|
43 | let prefix;
|
44 | let baseDir;
|
45 | if (prefixOrGlob && prefixOrGlob.length > 0) {
|
46 | const firstWildIdx = prefixOrGlob.indexOf('*');
|
47 |
|
48 | if (firstWildIdx > 0) {
|
49 | glob = prefixOrGlob;
|
50 | prefix = prefixOrGlob.substring(0, firstWildIdx);
|
51 | }
|
52 |
|
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 | }
|
65 | exports.extractPrefixAndGlob = extractPrefixAndGlob;
|
66 | async 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 | }
|
88 | exports.commonBucketDownload = commonBucketDownload;
|
89 | async 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 |
|
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 | }
|
113 | exports.commonBucketCopy = commonBucketCopy;
|
114 |
|
115 | function 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 | }
|