From 50b5338fe21f962b7b98f39915cf6bdf9d535b9f Mon Sep 17 00:00:00 2001 From: apostolof Date: Thu, 4 Feb 2021 23:18:47 +0200 Subject: [PATCH] refactor: use more Promises, delete contracts directory if already exists --- .../src/controllers/download.js | 6 +-- .../src/controllers/upload.js | 44 ++++++++++++------- .../src/middleware/upload.js | 2 - 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/packages/concordia-contracts-provider/src/controllers/download.js b/packages/concordia-contracts-provider/src/controllers/download.js index baa8151..ce23f2e 100755 --- a/packages/concordia-contracts-provider/src/controllers/download.js +++ b/packages/concordia-contracts-provider/src/controllers/download.js @@ -1,4 +1,4 @@ -import { promises as fs } from 'fs'; +import { promises as fs, constants } from 'fs'; import path from 'path'; import { getStorageLocation, getTagsDirectory } from '../utils/storageUtils'; @@ -14,13 +14,13 @@ const downloadContracts = async (req, res) => { const { params: { hash: identifier } } = req; const hashBasedDirectoryPath = getStorageLocation(identifier); - return fs.access(hashBasedDirectoryPath) + return fs.access(hashBasedDirectoryPath, constants.R_OK) .then(() => readContractFilesToArray(hashBasedDirectoryPath)) .catch(() => { const tagsDirectory = getTagsDirectory(); return fs - .access(tagsDirectory) + .access(tagsDirectory, constants.R_OK) .then(() => { const tagFilePath = path.join(tagsDirectory, identifier); diff --git a/packages/concordia-contracts-provider/src/controllers/upload.js b/packages/concordia-contracts-provider/src/controllers/upload.js index 894aa40..a2adee1 100755 --- a/packages/concordia-contracts-provider/src/controllers/upload.js +++ b/packages/concordia-contracts-provider/src/controllers/upload.js @@ -1,7 +1,18 @@ import path from 'path'; -import { promises as fs } from 'fs'; -import upload from '../middleware/upload'; -import { getTagsDirectory } from '../utils/storageUtils'; +import { constants, promises as fs } from 'fs'; +import uploadFilesUsingMiddleware from '../middleware/upload'; +import { getStorageLocation, getTagsDirectory } from '../utils/storageUtils'; + +const provisionContractsDirectory = (req) => { + const { params: { hash } } = req; + const contractsPath = getStorageLocation(hash); + + return fs + .access(contractsPath, constants.W_OK) + .then(() => fs.rmdir(contractsPath, { recursive: true })) + .catch(() => Promise.resolve()) + .then(() => fs.mkdir(contractsPath, { recursive: true })); +}; const addOrTransferTag = (tag, hash) => { const tagsDirectory = getTagsDirectory(); @@ -12,21 +23,22 @@ const addOrTransferTag = (tag, hash) => { .then(() => fs.writeFile(tagFilePath, hash, 'utf-8')); }; -const uploadContracts = async (req, res) => upload(req, res) - .then(() => { - if (req.files.length <= 0) { - return Promise.reject(new Error('You must select at least 1 file.')); - } +const uploadContracts = async (req, res) => provisionContractsDirectory(req) + .then(() => uploadFilesUsingMiddleware(req, res) + .then(() => { + if (req.files.length <= 0) { + return Promise.reject(new Error('You must select at least 1 file.')); + } - const { body: { tag } } = req; - const { params: { hash } } = req; + const { body: { tag } } = req; + const { params: { hash } } = req; - if (tag) { - return addOrTransferTag(tag, hash) - .then(() => res.send('Files have been uploaded and tagged.')); - } + if (tag) { + return addOrTransferTag(tag, hash) + .then(() => res.send('Files have been uploaded and tagged.')); + } - return res.send('Files have been uploaded.'); - }); + return res.send('Files have been uploaded.'); + })); export default uploadContracts; diff --git a/packages/concordia-contracts-provider/src/middleware/upload.js b/packages/concordia-contracts-provider/src/middleware/upload.js index 67ce0b6..c383d14 100755 --- a/packages/concordia-contracts-provider/src/middleware/upload.js +++ b/packages/concordia-contracts-provider/src/middleware/upload.js @@ -1,5 +1,4 @@ import * as util from 'util'; -import * as fs from 'fs'; import multer from 'multer'; import { getStorageLocation } from '../utils/storageUtils'; @@ -8,7 +7,6 @@ const storage = multer.diskStorage({ const { params: { hash } } = req; const contractsPath = getStorageLocation(hash); - fs.mkdirSync(contractsPath, { recursive: true }); callback(null, contractsPath); }, filename: (req, file, callback) => {