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
+31 -42
View File
@@ -99,7 +99,7 @@ async function addFilePathToJson(filePath) {
if (await fsExtra.pathExists(jsonFilePath)) { if (await fsExtra.pathExists(jsonFilePath)) {
await lockFile(jsonFilePath); await lockFile(jsonFilePath);
data = await fsExtra.readJson(jsonFilePath); data = await fsExtra.readJson(jsonFilePath);
} else { }else{
await lockFile(jsonFilePath); await lockFile(jsonFilePath);
} }
@@ -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({ const storage = multer.diskStorage({
destination: function (req, file, cb) { destination: function (req, file, cb) {
const { error, value } = shareFileSchema.validate(req.body); const baseDir = path.join(__dirname, '..', 'uploads', req.body.idUser); // Temp storage location
if (error) {
cb(error, undefined);
return;
}
const baseDir = path.join(__dirname, '..', 'uploads', value.idUser); // Temp storage location
fsExtra.ensureDirSync(baseDir); fsExtra.ensureDirSync(baseDir);
cb(null, baseDir); cb(null, baseDir);
}, },
@@ -139,38 +127,39 @@ const storage = multer.diskStorage({
const upload = multer({ storage: storage }).single('file'); const upload = multer({ storage: storage }).single('file');
app.post('/upload', (req, res) => { // Route for file upload
upload(req, res, async (err) => { app.post('/upload', async (req, res) => {
if (err instanceof multer.MulterError) { const shareFileSchema = Joi.object({
res.status(httpStatus.INTERNAL_SERVER_ERROR).send(`Multer error: ${err.message}`); idUser: Joi.string().required(),
return; nameOfFile: Joi.string().required(),
} else if (err) { sizeOfFile: Joi.number().required()
res.status(httpStatus.BAD_REQUEST).send(`Validation 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.' });
} catch (error) {
console.error('Failed to move file:', error);
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Server error while moving the file.' });
}
}); });
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.' });
});
} catch (error) {
console.error('Error during file upload:', error);
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Internal server error.' });
}
}); });
console.log('external_endpoints.json started.'); console.log('external_endpoints.json started.');
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`)); server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
-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 "totalSize": 19363
}, },
"59c712d9-e6dd-438f-aec9-31ca2ae750e6": { "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\"]}}}", "directoryStructure": "{\"backup_dir\":{\"files\":[],\"fisiere\":{\"files\":[\"New Microsoft Excel Worksheet.xlsx\",\"New Microsoft Word Document.docx\"]}}}",
"totalSize": 19363 "totalSize": 19363
} }
+30 -41
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({ const storage = multer.diskStorage({
destination: function (req, file, cb) { destination: function (req, file, cb) {
const { error, value } = shareFileSchema.validate(req.body); const baseDir = path.join(__dirname, '..', 'uploads', req.body.idUser); // Temp storage location
if (error) {
cb(error, undefined);
return;
}
const baseDir = path.join(__dirname, '..', 'uploads', value.idUser); // Temp storage location
fsExtra.ensureDirSync(baseDir); fsExtra.ensureDirSync(baseDir);
cb(null, baseDir); cb(null, baseDir);
}, },
@@ -139,38 +127,39 @@ const storage = multer.diskStorage({
const upload = multer({ storage: storage }).single('file'); const upload = multer({ storage: storage }).single('file');
app.post('/upload', (req, res) => { // Route for file upload
upload(req, res, async (err) => { app.post('/upload', async (req, res) => {
if (err instanceof multer.MulterError) { const shareFileSchema = Joi.object({
res.status(httpStatus.INTERNAL_SERVER_ERROR).send(`Multer error: ${err.message}`); idUser: Joi.string().required(),
return; nameOfFile: Joi.string().required(),
} else if (err) { sizeOfFile: Joi.number().required()
res.status(httpStatus.BAD_REQUEST).send(`Validation 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.'})
} catch (error) {
console.error('Failed to move file:', error);
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({message: 'Server error while moving the file.'});
}
}); });
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.' });
});
} catch (error) {
console.error('Error during file upload:', error);
res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ message: 'Internal server error.' });
}
}); });
console.log('external_endpoints.json started.'); console.log('external_endpoints.json started.');
const PORT = process.env.PORT || 3000; const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`)); server.listen(PORT, () => console.log(`Server running on port ${PORT}`));