Skip to content

Commit

Permalink
fixing storage.save bug for local files
Browse files Browse the repository at this point in the history
  • Loading branch information
frytg committed Jul 24, 2020
1 parent 6c8f57f commit 69d8d43
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- name: 👀 Checkout repo
uses: actions/[email protected]
- name: 🛠 Setup Node
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
node-version: '12.x'
registry-url: 'https://npm.pkg.github.com'
Expand All @@ -29,7 +29,7 @@ jobs:
- name: 👀 Checkout repo
uses: actions/[email protected]
- name: 🛠 Setup Node
uses: actions/[email protected].0
uses: actions/[email protected].1
with:
node-version: '12.x'
registry-url: 'https://registry.npmjs.org'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions src/delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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);
});
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down Expand Up @@ -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]);

Expand Down
14 changes: 7 additions & 7 deletions src/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down Expand Up @@ -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]);
Expand All @@ -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();
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 69d8d43

Please sign in to comment.