Browse Source

refactor: use more Promises, delete contracts directory if already exists

develop
Apostolos Fanakis 4 years ago
parent
commit
50b5338fe2
  1. 6
      packages/concordia-contracts-provider/src/controllers/download.js
  2. 22
      packages/concordia-contracts-provider/src/controllers/upload.js
  3. 2
      packages/concordia-contracts-provider/src/middleware/upload.js

6
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 path from 'path';
import { getStorageLocation, getTagsDirectory } from '../utils/storageUtils'; import { getStorageLocation, getTagsDirectory } from '../utils/storageUtils';
@ -14,13 +14,13 @@ const downloadContracts = async (req, res) => {
const { params: { hash: identifier } } = req; const { params: { hash: identifier } } = req;
const hashBasedDirectoryPath = getStorageLocation(identifier); const hashBasedDirectoryPath = getStorageLocation(identifier);
return fs.access(hashBasedDirectoryPath) return fs.access(hashBasedDirectoryPath, constants.R_OK)
.then(() => readContractFilesToArray(hashBasedDirectoryPath)) .then(() => readContractFilesToArray(hashBasedDirectoryPath))
.catch(() => { .catch(() => {
const tagsDirectory = getTagsDirectory(); const tagsDirectory = getTagsDirectory();
return fs return fs
.access(tagsDirectory) .access(tagsDirectory, constants.R_OK)
.then(() => { .then(() => {
const tagFilePath = path.join(tagsDirectory, identifier); const tagFilePath = path.join(tagsDirectory, identifier);

22
packages/concordia-contracts-provider/src/controllers/upload.js

@ -1,7 +1,18 @@
import path from 'path'; import path from 'path';
import { promises as fs } from 'fs'; import { constants, promises as fs } from 'fs';
import upload from '../middleware/upload'; import uploadFilesUsingMiddleware from '../middleware/upload';
import { getTagsDirectory } from '../utils/storageUtils'; 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 addOrTransferTag = (tag, hash) => {
const tagsDirectory = getTagsDirectory(); const tagsDirectory = getTagsDirectory();
@ -12,7 +23,8 @@ const addOrTransferTag = (tag, hash) => {
.then(() => fs.writeFile(tagFilePath, hash, 'utf-8')); .then(() => fs.writeFile(tagFilePath, hash, 'utf-8'));
}; };
const uploadContracts = async (req, res) => upload(req, res) const uploadContracts = async (req, res) => provisionContractsDirectory(req)
.then(() => uploadFilesUsingMiddleware(req, res)
.then(() => { .then(() => {
if (req.files.length <= 0) { if (req.files.length <= 0) {
return Promise.reject(new Error('You must select at least 1 file.')); return Promise.reject(new Error('You must select at least 1 file.'));
@ -27,6 +39,6 @@ const uploadContracts = async (req, res) => upload(req, res)
} }
return res.send('Files have been uploaded.'); return res.send('Files have been uploaded.');
}); }));
export default uploadContracts; export default uploadContracts;

2
packages/concordia-contracts-provider/src/middleware/upload.js

@ -1,5 +1,4 @@
import * as util from 'util'; import * as util from 'util';
import * as fs from 'fs';
import multer from 'multer'; import multer from 'multer';
import { getStorageLocation } from '../utils/storageUtils'; import { getStorageLocation } from '../utils/storageUtils';
@ -8,7 +7,6 @@ const storage = multer.diskStorage({
const { params: { hash } } = req; const { params: { hash } } = req;
const contractsPath = getStorageLocation(hash); const contractsPath = getStorageLocation(hash);
fs.mkdirSync(contractsPath, { recursive: true });
callback(null, contractsPath); callback(null, contractsPath);
}, },
filename: (req, file, callback) => { filename: (req, file, callback) => {

Loading…
Cancel
Save