2023-08-15 00:22:55 +02:00
|
|
|
const multer = require("multer");
|
|
|
|
const path = require("path");
|
|
|
|
const fs = require("fs");
|
2023-12-07 23:11:51 +01:00
|
|
|
const { v4 } = require("uuid");
|
2023-08-15 00:22:55 +02:00
|
|
|
|
2024-10-25 21:03:19 +02:00
|
|
|
/**
|
|
|
|
* Handle File uploads for auto-uploading.
|
|
|
|
* Mostly used for internal GUI/API uploads.
|
|
|
|
*/
|
2024-04-01 21:06:47 +02:00
|
|
|
const fileUploadStorage = multer.diskStorage({
|
|
|
|
destination: function (_, __, cb) {
|
|
|
|
const uploadOutput =
|
|
|
|
process.env.NODE_ENV === "development"
|
|
|
|
? path.resolve(__dirname, `../../../collector/hotdir`)
|
|
|
|
: path.resolve(process.env.STORAGE_DIR, `../../collector/hotdir`);
|
|
|
|
cb(null, uploadOutput);
|
|
|
|
},
|
|
|
|
filename: function (_, file, cb) {
|
|
|
|
file.originalname = Buffer.from(file.originalname, "latin1").toString(
|
|
|
|
"utf8"
|
|
|
|
);
|
|
|
|
cb(null, file.originalname);
|
|
|
|
},
|
|
|
|
});
|
2023-08-15 00:22:55 +02:00
|
|
|
|
2024-10-25 21:03:19 +02:00
|
|
|
/**
|
|
|
|
* Handle API file upload as documents - this does not manipulate the filename
|
|
|
|
* at all for encoding/charset reasons.
|
|
|
|
*/
|
|
|
|
const fileAPIUploadStorage = multer.diskStorage({
|
|
|
|
destination: function (_, __, cb) {
|
|
|
|
const uploadOutput =
|
|
|
|
process.env.NODE_ENV === "development"
|
|
|
|
? path.resolve(__dirname, `../../../collector/hotdir`)
|
|
|
|
: path.resolve(process.env.STORAGE_DIR, `../../collector/hotdir`);
|
|
|
|
cb(null, uploadOutput);
|
|
|
|
},
|
|
|
|
filename: function (_, file, cb) {
|
|
|
|
cb(null, file.originalname);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-04-01 21:06:47 +02:00
|
|
|
// Asset storage for logos
|
|
|
|
const assetUploadStorage = multer.diskStorage({
|
|
|
|
destination: function (_, __, cb) {
|
|
|
|
const uploadOutput =
|
|
|
|
process.env.NODE_ENV === "development"
|
|
|
|
? path.resolve(__dirname, `../../storage/assets`)
|
|
|
|
: path.resolve(process.env.STORAGE_DIR, "assets");
|
|
|
|
fs.mkdirSync(uploadOutput, { recursive: true });
|
|
|
|
return cb(null, uploadOutput);
|
|
|
|
},
|
|
|
|
filename: function (_, file, cb) {
|
|
|
|
file.originalname = Buffer.from(file.originalname, "latin1").toString(
|
|
|
|
"utf8"
|
|
|
|
);
|
|
|
|
cb(null, file.originalname);
|
|
|
|
},
|
|
|
|
});
|
2023-06-17 01:01:27 +02:00
|
|
|
|
2024-10-25 21:03:19 +02:00
|
|
|
/**
|
|
|
|
* Handle PFP file upload as logos
|
|
|
|
*/
|
2024-04-01 21:06:47 +02:00
|
|
|
const pfpUploadStorage = multer.diskStorage({
|
|
|
|
destination: function (_, __, cb) {
|
|
|
|
const uploadOutput =
|
|
|
|
process.env.NODE_ENV === "development"
|
|
|
|
? path.resolve(__dirname, `../../storage/assets/pfp`)
|
|
|
|
: path.resolve(process.env.STORAGE_DIR, "assets/pfp");
|
|
|
|
fs.mkdirSync(uploadOutput, { recursive: true });
|
|
|
|
return cb(null, uploadOutput);
|
|
|
|
},
|
|
|
|
filename: function (req, file, cb) {
|
|
|
|
const randomFileName = `${v4()}${path.extname(file.originalname)}`;
|
|
|
|
req.randomFileName = randomFileName;
|
|
|
|
cb(null, randomFileName);
|
|
|
|
},
|
|
|
|
});
|
2023-08-15 00:22:55 +02:00
|
|
|
|
2024-10-25 21:03:19 +02:00
|
|
|
/**
|
|
|
|
* Handle Generic file upload as documents from the GUI
|
|
|
|
* @param {Request} request
|
|
|
|
* @param {Response} response
|
|
|
|
* @param {NextFunction} next
|
|
|
|
*/
|
2024-04-01 21:06:47 +02:00
|
|
|
function handleFileUpload(request, response, next) {
|
|
|
|
const upload = multer({ storage: fileUploadStorage }).single("file");
|
|
|
|
upload(request, response, function (err) {
|
|
|
|
if (err) {
|
|
|
|
response
|
|
|
|
.status(500)
|
|
|
|
.json({
|
|
|
|
success: false,
|
|
|
|
error: `Invalid file upload. ${err.message}`,
|
|
|
|
})
|
|
|
|
.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
2023-07-15 02:32:30 +02:00
|
|
|
}
|
|
|
|
|
2024-10-25 21:03:19 +02:00
|
|
|
/**
|
|
|
|
* Handle API file upload as documents - this does not manipulate the filename
|
|
|
|
* at all for encoding/charset reasons.
|
|
|
|
* @param {Request} request
|
|
|
|
* @param {Response} response
|
|
|
|
* @param {NextFunction} next
|
|
|
|
*/
|
|
|
|
function handleAPIFileUpload(request, response, next) {
|
|
|
|
const upload = multer({ storage: fileAPIUploadStorage }).single("file");
|
|
|
|
upload(request, response, function (err) {
|
|
|
|
if (err) {
|
|
|
|
response
|
|
|
|
.status(500)
|
|
|
|
.json({
|
|
|
|
success: false,
|
|
|
|
error: `Invalid file upload. ${err.message}`,
|
|
|
|
})
|
|
|
|
.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle logo asset uploads
|
|
|
|
*/
|
2024-04-01 21:06:47 +02:00
|
|
|
function handleAssetUpload(request, response, next) {
|
|
|
|
const upload = multer({ storage: assetUploadStorage }).single("logo");
|
|
|
|
upload(request, response, function (err) {
|
|
|
|
if (err) {
|
|
|
|
response
|
|
|
|
.status(500)
|
|
|
|
.json({
|
|
|
|
success: false,
|
|
|
|
error: `Invalid file upload. ${err.message}`,
|
|
|
|
})
|
|
|
|
.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
next();
|
2023-12-07 23:11:51 +01:00
|
|
|
});
|
2024-04-01 21:06:47 +02:00
|
|
|
}
|
2023-12-07 23:11:51 +01:00
|
|
|
|
2024-10-25 21:03:19 +02:00
|
|
|
/**
|
|
|
|
* Handle PFP file upload as logos
|
|
|
|
*/
|
2024-04-01 21:06:47 +02:00
|
|
|
function handlePfpUpload(request, response, next) {
|
|
|
|
const upload = multer({ storage: pfpUploadStorage }).single("file");
|
|
|
|
upload(request, response, function (err) {
|
|
|
|
if (err) {
|
|
|
|
response
|
|
|
|
.status(500)
|
|
|
|
.json({
|
|
|
|
success: false,
|
|
|
|
error: `Invalid file upload. ${err.message}`,
|
|
|
|
})
|
|
|
|
.end();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
});
|
2023-12-07 23:11:51 +01:00
|
|
|
}
|
|
|
|
|
2023-06-17 01:01:27 +02:00
|
|
|
module.exports = {
|
2024-04-01 21:06:47 +02:00
|
|
|
handleFileUpload,
|
2024-10-25 21:03:19 +02:00
|
|
|
handleAPIFileUpload,
|
2024-04-01 21:06:47 +02:00
|
|
|
handleAssetUpload,
|
|
|
|
handlePfpUpload,
|
2023-06-17 01:01:27 +02:00
|
|
|
};
|