v2.7
This commit is contained in:
@@ -1,15 +1,15 @@
|
||||
const express = require('express');
|
||||
const fs = require('fs');
|
||||
const multer = require('multer');
|
||||
const fsExtra = require('fs-extra');
|
||||
const path = require('path');
|
||||
const Joi = require('joi');
|
||||
const { httpStatus } = require('../helpers/http_status');
|
||||
const {lockFile, unlockFile} = require("../helpers/lock_mechanism");
|
||||
const {decryptFileInPlace, encryptFileInPlace} = require("../src/main/aes_encrypt");
|
||||
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
app.use('/share_file', express.raw({ type: 'application/octet-stream', limit: 'Infinity' }));
|
||||
|
||||
|
||||
const server = require('http').createServer(app);
|
||||
const connections = [];
|
||||
@@ -114,45 +114,51 @@ async function addFilePathToJson(filePath) {
|
||||
}
|
||||
}
|
||||
|
||||
const storage = multer.diskStorage({
|
||||
destination: function (req, file, cb) {
|
||||
const baseDir = path.join(__dirname, '..', 'uploads', req.body.idUser); // Temp storage location
|
||||
fsExtra.ensureDirSync(baseDir);
|
||||
cb(null, baseDir);
|
||||
},
|
||||
filename: function (req, file, cb) {
|
||||
cb(null, req.body.nameOfFile); // Using directly assuming it has been validated already
|
||||
app.use('/share_file', express.raw({
|
||||
type: 'application/octet-stream',
|
||||
limit: '50mb'
|
||||
}));
|
||||
|
||||
async function decryptAndGetShareDir(filePath) {
|
||||
try {
|
||||
await lockFile(filePath);
|
||||
await decryptFileInPlace(filePath);
|
||||
|
||||
const fileContent = await fs.promises.readFile(filePath, 'utf8');
|
||||
const jsonData = JSON.parse(fileContent);
|
||||
const serverIP = jsonData.path;
|
||||
|
||||
await encryptFileInPlace(filePath)
|
||||
await unlockFile(filePath);
|
||||
return serverIP;
|
||||
} catch (error) {
|
||||
console.error('An error occurred:', error);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
const upload = multer({ storage: storage }).single('file');
|
||||
|
||||
// Route for file upload
|
||||
app.post('/upload', async (req, res) => {
|
||||
const shareFileSchema = Joi.object({
|
||||
idUser: Joi.string().required(),
|
||||
nameOfFile: Joi.string().required(),
|
||||
sizeOfFile: Joi.number().required()
|
||||
});
|
||||
app.post('/share_file', async (req, res) => {
|
||||
// Extract metadata from headers
|
||||
const idUser = req.headers['x-iduser'];
|
||||
const nameOfFile = req.headers['x-nameoffile'];
|
||||
const sizeOfFile = req.headers['x-sizeoffile'];
|
||||
|
||||
// You might want to validate the metadata here
|
||||
if (!idUser || !nameOfFile || !sizeOfFile) {
|
||||
return res.status(httpStatus.BAD_REQUEST).send('Missing metadata headers');
|
||||
}
|
||||
|
||||
// Construct the file path using the metadata
|
||||
const shareDirPath = await decryptAndGetShareDir(path.join(__dirname, '..', 'shareDir.json'));
|
||||
const baseDir = path.join(shareDirPath, idUser);
|
||||
fsExtra.ensureDirSync(baseDir);
|
||||
|
||||
const filePath = path.join(shareDirPath, nameOfFile);
|
||||
|
||||
try {
|
||||
const { error, value } = shareFileSchema.validate(req.body);
|
||||
if (error) {
|
||||
return res.status(httpStatus.BAD_REQUEST).send(`Validation error: ${error.message}`);
|
||||
}
|
||||
|
||||
await upload(req, res, async (err) => {
|
||||
if (err instanceof multer.MulterError) {
|
||||
res.status(httpStatus.INTERNAL_SERVER_ERROR).send(`Multer error: ${err.message}`);
|
||||
return;
|
||||
} else if (err) {
|
||||
res.status(httpStatus.BAD_REQUEST).send(`Upload error: ${err.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// File upload logic
|
||||
res.status(httpStatus.OK).json({ message: 'Upload successful.' });
|
||||
});
|
||||
fs.writeFileSync(filePath, req.body);
|
||||
res.status(httpStatus.OK).json({ message: 'Upload successful.' });
|
||||
} catch (error) {
|
||||
console.error('Error during file upload:', error);
|
||||
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Internal server error.' });
|
||||
|
||||
Reference in New Issue
Block a user