This commit is contained in:
andrei-mihnea-cerbu
2024-04-24 12:29:55 +03:00
parent 437270b055
commit 93be92768b
4 changed files with 62 additions and 107 deletions
+21 -32
View File
@@ -114,21 +114,9 @@ async function addFilePathToJson(filePath) {
}
}
const shareFileSchema = Joi.object({
idUser: Joi.string().required(),
nameOfFile: Joi.string().required(),
sizeOfFile: Joi.number().required()
});
const storage = multer.diskStorage({
destination: function (req, file, cb) {
const { error, value } = shareFileSchema.validate(req.body);
if (error) {
cb(error, undefined);
return;
}
const baseDir = path.join(__dirname, '..', 'uploads', value.idUser); // Temp storage location
const baseDir = path.join(__dirname, '..', 'uploads', req.body.idUser); // Temp storage location
fsExtra.ensureDirSync(baseDir);
cb(null, baseDir);
},
@@ -139,36 +127,37 @@ const storage = multer.diskStorage({
const upload = multer({ storage: storage }).single('file');
app.post('/upload', (req, res) => {
upload(req, res, async (err) => {
// 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()
});
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(`Validation error: ${err.message}`);
res.status(httpStatus.BAD_REQUEST).send(`Upload error: ${err.message}`);
return;
}
try {
const { idUser, nameOfFile } = req.body;
const dirConfig = await fsExtra.readJson(path.join(__dirname, '..', 'dirShare.json'));
const baseDir = dirConfig.path;
const finalPath = path.join(baseDir, idUser, nameOfFile);
await fsExtra.move(req.file.path, finalPath, { overwrite: true });
await addFilePathToJson(finalPath);
// File upload logic
res.status(httpStatus.OK).json({ message: 'Upload successful.' });
});
} catch (error) {
console.error('Failed to move file:', error);
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Server error while moving the file.' });
console.error('Error during file upload:', error);
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Internal server error.' });
}
});
});
console.log('external_endpoints.json started.');
-23
View File
@@ -1,23 +0,0 @@
# Use the latest official Node runtime as a parent image
FROM node:latest
# Set the working directory in the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json (if available)
COPY package*.json ./
# Install any needed packages specified in package.json
RUN npm install
# Bundle app source inside Docker image
COPY . .
# Make ports 80, 3000, and 5000 available to the world outside this container
EXPOSE 80 3000 5000
# Define environment variable
ENV NODE_ENV=production
# Run the app when the container launches
CMD ["npm", "start"]
+1 -1
View File
@@ -5,7 +5,7 @@
"totalSize": 19363
},
"59c712d9-e6dd-438f-aec9-31ca2ae750e6": {
"ip": "192.168.0.195",
"ip": "192.168.0.18",
"directoryStructure": "{\"backup_dir\":{\"files\":[],\"fisiere\":{\"files\":[\"New Microsoft Excel Worksheet.xlsx\",\"New Microsoft Word Document.docx\"]}}}",
"totalSize": 19363
}
+22 -33
View File
@@ -114,21 +114,9 @@ async function addFilePathToJson(filePath) {
}
}
const shareFileSchema = Joi.object({
idUser: Joi.string().required(),
nameOfFile: Joi.string().required(),
sizeOfFile: Joi.number().required()
});
const storage = multer.diskStorage({
destination: function (req, file, cb) {
const { error, value } = shareFileSchema.validate(req.body);
if (error) {
cb(error, undefined);
return;
}
const baseDir = path.join(__dirname, '..', 'uploads', value.idUser); // Temp storage location
const baseDir = path.join(__dirname, '..', 'uploads', req.body.idUser); // Temp storage location
fsExtra.ensureDirSync(baseDir);
cb(null, baseDir);
},
@@ -139,36 +127,37 @@ const storage = multer.diskStorage({
const upload = multer({ storage: storage }).single('file');
app.post('/upload', (req, res) => {
upload(req, res, async (err) => {
// 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()
});
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(`Validation error: ${err.message}`);
res.status(httpStatus.BAD_REQUEST).send(`Upload error: ${err.message}`);
return;
}
try {
const { idUser, nameOfFile } = req.body;
const dirConfig = await fsExtra.readJson(path.join(__dirname, '..', 'dirShare.json'));
const baseDir = dirConfig.path;
const finalPath = path.join(baseDir, idUser, nameOfFile);
await fsExtra.move(req.file.path, finalPath, { overwrite: true });
await addFilePathToJson(finalPath);
res.status(httpStatus.OK).json({message: 'Upload successful.'})
// File upload logic
res.status(httpStatus.OK).json({ message: 'Upload successful.' });
});
} catch (error) {
console.error('Failed to move file:', error);
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({message: 'Server error while moving the file.'});
console.error('Error during file upload:', error);
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Internal server error.' });
}
});
});
console.log('external_endpoints.json started.');