@pittica/google-cloud-storage-helpers 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -16,6 +16,9 @@ const {
16
16
  Storage
17
17
  } = require("@google-cloud/storage");
18
18
  const log = require("@pittica/logger-helpers");
19
+ const {
20
+ getStorage
21
+ } = require("./storage");
19
22
 
20
23
  /**
21
24
  * Moves all files from a bucket to another.
@@ -24,21 +27,26 @@ const log = require("@pittica/logger-helpers");
24
27
  * @param {string} destination Destination bucket.
25
28
  */
26
29
  exports.moveFiles = async (source, destination) => {
27
- const storage = new Storage({
28
- retryOptions: {
29
- autoRetry: true,
30
- retryDelayMultiplier: 4
31
- }
32
- });
30
+ const storage = getStorage();
33
31
  const bucket = storage.bucket(destination);
34
32
  const [files] = await storage.bucket(source).getFiles();
35
- files.forEach(results => results.forEach(file => {
36
- const {
37
- id
38
- } = file;
39
- log.info(`Moving "${id}" from "${source}" to "${destination}"`);
40
- file.copy(bucket.file(id)).then(() => this.deleteFile(file) ? log.success(`Moved "${id}" from "${source}" to "${destination}"`) : log.error(`Failed deleted "${id}" in "${source}"`)).catch(() => log.error(`Failed moving "${id}" from "${source}" to "${destination}"`));
41
- }));
33
+ files.forEach(results => Array.isArray(results) ? results.forEach(file => this.moveFile(file, bucket)) : this.moveFile(results, bucket));
34
+ };
35
+
36
+ /**
37
+ * Moves a file from a bucket to another.
38
+ *
39
+ * @param {File} file File object.
40
+ * @param {Bucket} bucket Bucket object.
41
+ */
42
+ exports.moveFile = async (file, bucket) => {
43
+ const {
44
+ id
45
+ } = file;
46
+ const source = file.metadata.bucket;
47
+ const destination = bucket.id;
48
+ log.info(`Moving "${id}" from "${source}" to "${destination}"`);
49
+ file.copy(bucket.file(id)).then(() => this.deleteFile(file) ? log.success(`Moved "${id}" from "${source}" to "${destination}"`) : log.error(`Failed deleted "${id}" in "${source}"`)).catch(() => log.error(`Failed moving "${id}" from "${source}" to "${destination}"`));
42
50
  };
43
51
 
44
52
  /**
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pittica/google-cloud-storage-helpers",
3
3
  "private": false,
4
- "version": "1.2.0",
4
+ "version": "1.2.1",
5
5
  "description": "Helpers for Google Cloud Storage.",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {