v2.6
This commit is contained in:
@@ -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,38 +127,39 @@ const storage = multer.diskStorage({
|
||||
|
||||
const upload = multer({ storage: storage }).single('file');
|
||||
|
||||
app.post('/upload', (req, res) => {
|
||||
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}`);
|
||||
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.'});
|
||||
}
|
||||
// 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(`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.');
|
||||
const PORT = process.env.PORT || 3000;
|
||||
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
||||
|
||||
Reference in New Issue
Block a user