Skip to content

Commit

Permalink
feat: replace express fileupload to multer
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed May 25, 2023
1 parent dbb4358 commit 392fece
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 120 deletions.
184 changes: 156 additions & 28 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
"csvtojson": "^2.0.10",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"express-fileupload": "^1.2.1",
"express-joi-validation": "^5.0.1",
"joi": "^17.5.0",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"log-update": "^4.0.0",
"multer": "*",
"mysql2": "^2.3.3",
"node-xlsx": "^0.21.0",
"regenerator-runtime": "^0.13.9",
Expand Down
41 changes: 21 additions & 20 deletions src/controllers/fileStore.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import _ from 'lodash';

import crypto from 'crypto';
import { FileStore } from '../models';

Expand Down Expand Up @@ -86,26 +84,29 @@ export const getFile = async (req, res) => {

export const addFileToFileStore = async (req, res) => {
try {
if (_.get(req, 'files.file.data')) {
const { fileName } = req.body;
if (!fileName) {
throw new Error('Missing file name, can not upload file');
}
const buffer = req.files.file.data;
const base64File = buffer.toString('base64');
const SHA256 = crypto
.createHash('sha256')
.update(base64File)
.digest('base64');
await FileStore.addFileToFileStore(SHA256, fileName, base64File);
return res.json({
message:
'File is being added to the file store, please wait for it to confirm.',
fileId: SHA256,
});
} else {
if (!req.file) {
throw new Error('Missing file data, can not upload file.');
}

const { originalname: fileName, buffer } = req.file;

if (!fileName) {
throw new Error('Missing file name, can not upload file');
}

const base64File = buffer.toString('base64');
const SHA256 = crypto
.createHash('sha256')
.update(base64File)
.digest('base64');

await FileStore.addFileToFileStore(SHA256, fileName, base64File);

return res.json({
message:
'File is being added to the file store, please wait for it to confirm.',
fileId: SHA256,
});
} catch (error) {
console.trace(error);
res.status(400).json({
Expand Down
Loading

0 comments on commit 392fece

Please sign in to comment.