From 69d8d435e88f0c5090e57897f2039e1186c462f4 Mon Sep 17 00:00:00 2001 From: Daniel Freytag Date: Fri, 24 Jul 2020 19:48:25 +0200 Subject: [PATCH] fixing storage.save bug for local files --- .github/workflows/linter.yml | 2 +- .github/workflows/publish.yml | 4 ++-- package.json | 2 +- src/delete.js | 6 +++--- src/list.js | 12 ++++++------ src/load.js | 7 +++---- src/save.js | 14 +++++++------- yarn.lock | 8 ++++---- 8 files changed, 27 insertions(+), 28 deletions(-) diff --git a/.github/workflows/linter.yml b/.github/workflows/linter.yml index 3b96da9..49d65d1 100644 --- a/.github/workflows/linter.yml +++ b/.github/workflows/linter.yml @@ -9,7 +9,7 @@ jobs: steps: - name: Checkout Code - uses: actions/checkout@v2 + uses: actions/checkout@v2.3.1 - name: Lint Code Base uses: docker://github/super-linter:v2.2.0 diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 9d9b67c..befef30 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -11,7 +11,7 @@ jobs: - name: 👀 Checkout repo uses: actions/checkout@v2.3.1 - name: 🛠 Setup Node - uses: actions/setup-node@v2.1.0 + uses: actions/setup-node@v2.1.1 with: node-version: '12.x' registry-url: 'https://npm.pkg.github.com' @@ -29,7 +29,7 @@ jobs: - name: 👀 Checkout repo uses: actions/checkout@v2.3.1 - name: 🛠 Setup Node - uses: actions/setup-node@v2.1.0 + uses: actions/setup-node@v2.1.1 with: node-version: '12.x' registry-url: 'https://registry.npmjs.org' diff --git a/package.json b/package.json index 42c962e..96d3eb3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@swrlab/node-storage-wrapper", - "version": "0.0.1", + "version": "0.0.2", "description": "Wrapping AWS S3, GCP GCS, file storage", "main": "./src/index.js", "engines": { diff --git a/src/delete.js b/src/delete.js index 417b8b1..c74c015 100644 --- a/src/delete.js +++ b/src/delete.js @@ -4,9 +4,9 @@ */ -const deleteLocalFile = (filePath) => +const deleteLocalFile = (that, filePath) => new Promise((resolve, reject) => { - this.sdk.fs.unlink(filePath, (err) => { + that.sdk.fs.unlink(filePath, (err) => { if (err) reject(err); else resolve(); }); @@ -54,7 +54,7 @@ module.exports = async function (uri) { this.log('log', ['storage.delete.local >', uri]); // delete file - await deleteLocalFile(uri); + await deleteLocalFile(this, uri); // return ok return Promise.resolve(); diff --git a/src/list.js b/src/list.js index 7134981..b23f67e 100644 --- a/src/list.js +++ b/src/list.js @@ -4,10 +4,10 @@ */ -const awsListObjects = async (bucket, path, next) => { +const awsListObjects = async (that, bucket, path, next) => { try { // load list from aws, pass next token (nullable) - let files = await this.sdk.s3 + let files = await that.sdk.s3 .listObjectsV2({ Bucket: bucket, Prefix: path, @@ -30,9 +30,9 @@ const awsListObjects = async (bucket, path, next) => { } }; -const listLocalFiles = (uri) => +const listLocalFiles = (that, uri) => new Promise((resolve, reject) => { - this.sdk.fs.readdir(uri, 'utf8', (err, data) => { + that.sdk.fs.readdir(uri, 'utf8', (err, data) => { if (err) reject(err); else resolve(data); }); @@ -57,7 +57,7 @@ module.exports = async function (uri) { do { // load data - let awsReturn = await awsListObjects(bucket, path, next ? next : null); + let awsReturn = await awsListObjects(this, bucket, path, next ? next : null); // set next token next = awsReturn.next; @@ -89,7 +89,7 @@ module.exports = async function (uri) { this.log('log', ['storage.list.local >', uri]); // local file - let file = await listLocalFiles(uri); + let file = await listLocalFiles(this, uri); // return list return Promise.resolve(file); diff --git a/src/load.js b/src/load.js index 7248e50..26f65c3 100644 --- a/src/load.js +++ b/src/load.js @@ -5,12 +5,11 @@ */ // load node utils -const fs = require('fs'); const fetch = require('node-fetch'); -const loadLocalFile = (uri) => +const loadLocalFile = (that, uri) => new Promise((resolve, reject) => { - fs.readFile(uri, 'utf8', (err, data) => { + that.sdk.fs.readFile(uri, 'utf8', (err, data) => { if (err) reject(err); else resolve(data); }); @@ -70,7 +69,7 @@ module.exports = async function (uri) { } } else { // local file - let file = await loadLocalFile(uri); + let file = await loadLocalFile(this, uri); this.log('log', ['storage.load.local >', uri]); diff --git a/src/save.js b/src/save.js index 9dcb2df..a9f14d7 100644 --- a/src/save.js +++ b/src/save.js @@ -9,17 +9,17 @@ const os = require('os'); const pathUtil = require('path'); const { v4: uuidv4 } = require('uuid'); -const saveLocalFile = (uri, buffer) => +const saveLocalFile = (that, uri, buffer) => new Promise((resolve, reject) => { - this.sdk.fs.writeFile(uri, buffer, (err) => { + that.sdk.fs.writeFile(uri, buffer, (err) => { if (err) reject(err); else resolve(); }); }); -const deleteLocalFile = (filePath) => +const deleteLocalFile = (that, filePath) => new Promise((resolve, reject) => { - this.sdk.fs.unlink(filePath, (err) => { + that.sdk.fs.unlink(filePath, (err) => { if (err) reject(err); else resolve(); }); @@ -57,7 +57,7 @@ module.exports = async function (uri, buffer) { // save to local file let tempFilePath = pathUtil.resolve(os.tmpdir(), uuidv4()); - await saveLocalFile(tempFilePath, buffer); + await saveLocalFile(this, tempFilePath, buffer); // log progress this.log('log', ['storage.save.gs >', uri]); @@ -70,7 +70,7 @@ module.exports = async function (uri, buffer) { }); // delete local temp file - await deleteLocalFile(tempFilePath); + await deleteLocalFile(this, tempFilePath); // return ok return Promise.resolve(); @@ -81,7 +81,7 @@ module.exports = async function (uri, buffer) { this.log('log', ['storage.save.local >', uri]); // save file - let file = await saveLocalFile(uri); + let file = await saveLocalFile(this, uri, buffer); // return ok return Promise.resolve(file); diff --git a/yarn.lock b/yarn.lock index 41f8bf7..c37bed9 100644 --- a/yarn.lock +++ b/yarn.lock @@ -305,10 +305,10 @@ astral-regex@^1.0.0: resolved "https://registry.yarnpkg.com/astral-regex/-/astral-regex-1.0.0.tgz#6c8c3fb827dd43ee3918f27b82782ab7658a6fd9" integrity sha512-+Ryf6g3BKoRc7jfp7ad8tM4TtMiaWvbF/1/sQcZPkkS7ag3D5nMBCe2UfOTONtAkaG0tO0ij3C5Lwmf1EiyjHg== -aws-sdk@2.718.0: - version "2.718.0" - resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.718.0.tgz#f659e022602cd93c66f043364810d04e3cbb0241" - integrity sha512-YMWR1RJ3VuSbUOGeOfDw2QqRzwX51oa9TCm2G6SW+JywJUy0FTxi/Nj0VjVEQvKC0GqGu5QCgUTaarF7S0nQdw== +aws-sdk@2.719.0: + version "2.719.0" + resolved "https://registry.yarnpkg.com/aws-sdk/-/aws-sdk-2.719.0.tgz#346421f3335a7ded2cc6184fbcdbf71e42544d08" + integrity sha512-vqVAeZ2C8VLvL1hJIBCRFnKMomIoWSIUeYjULRiwRlBYH95EgcDY559Mq2rkx+E0733jdf3wNHA+eRVmwHyXvQ== dependencies: buffer "4.9.2" events "1.1.1"