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
|
|
|
|
2023-06-17 01:01:27 +02:00
|
|
|
function setupMulter() {
|
|
|
|
// Handle File uploads for auto-uploading.
|
|
|
|
const storage = multer.diskStorage({
|
2023-12-13 01:07:23 +01:00
|
|
|
destination: function (_, __, cb) {
|
2023-06-17 01:01:27 +02:00
|
|
|
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) {
|
2023-12-13 01:07:23 +01:00
|
|
|
file.originalname = Buffer.from(file.originalname, "latin1").toString(
|
|
|
|
"utf8"
|
|
|
|
);
|
2023-06-17 01:01:27 +02:00
|
|
|
cb(null, file.originalname);
|
|
|
|
},
|
|
|
|
});
|
2023-08-15 00:22:55 +02:00
|
|
|
|
|
|
|
return { handleUploads: multer({ storage }) };
|
2023-06-17 01:01:27 +02:00
|
|
|
}
|
|
|
|
|
2023-07-15 02:32:30 +02:00
|
|
|
function setupDataImports() {
|
|
|
|
// Handle File uploads for auto-uploading.
|
|
|
|
const storage = multer.diskStorage({
|
2023-12-13 01:07:23 +01:00
|
|
|
destination: function (_, __, cb) {
|
2023-07-15 02:32:30 +02:00
|
|
|
const uploadOutput = path.resolve(__dirname, `../../storage/imports`);
|
|
|
|
fs.mkdirSync(uploadOutput, { recursive: true });
|
|
|
|
return cb(null, uploadOutput);
|
|
|
|
},
|
|
|
|
filename: function (_, file, cb) {
|
|
|
|
cb(null, file.originalname);
|
|
|
|
},
|
|
|
|
});
|
2023-08-15 00:22:55 +02:00
|
|
|
|
|
|
|
return { handleImports: multer({ storage }) };
|
|
|
|
}
|
|
|
|
|
|
|
|
function setupLogoUploads() {
|
|
|
|
// Handle Logo uploads.
|
|
|
|
const storage = multer.diskStorage({
|
2023-12-13 01:07:23 +01:00
|
|
|
destination: function (_, __, cb) {
|
2023-12-07 23:11:51 +01:00
|
|
|
const uploadOutput =
|
|
|
|
process.env.NODE_ENV === "development"
|
|
|
|
? path.resolve(__dirname, `../../storage/assets`)
|
|
|
|
: path.resolve(process.env.STORAGE_DIR, "assets");
|
2023-08-15 00:22:55 +02:00
|
|
|
fs.mkdirSync(uploadOutput, { recursive: true });
|
|
|
|
return cb(null, uploadOutput);
|
|
|
|
},
|
|
|
|
filename: function (_, file, cb) {
|
2023-12-13 01:07:23 +01:00
|
|
|
file.originalname = Buffer.from(file.originalname, "latin1").toString(
|
|
|
|
"utf8"
|
|
|
|
);
|
2023-08-15 00:22:55 +02:00
|
|
|
cb(null, file.originalname);
|
|
|
|
},
|
2023-07-15 02:32:30 +02:00
|
|
|
});
|
2023-08-15 00:22:55 +02:00
|
|
|
|
|
|
|
return { handleLogoUploads: multer({ storage }) };
|
2023-07-15 02:32:30 +02:00
|
|
|
}
|
|
|
|
|
2023-12-07 23:11:51 +01:00
|
|
|
function setupPfpUploads() {
|
|
|
|
const storage = multer.diskStorage({
|
2023-12-13 01:07:23 +01:00
|
|
|
destination: function (_, __, cb) {
|
2023-12-07 23:11:51 +01:00
|
|
|
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);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return { handlePfpUploads: multer({ storage }) };
|
|
|
|
}
|
|
|
|
|
2023-06-17 01:01:27 +02:00
|
|
|
module.exports = {
|
|
|
|
setupMulter,
|
2023-07-15 02:32:30 +02:00
|
|
|
setupDataImports,
|
2023-08-15 00:22:55 +02:00
|
|
|
setupLogoUploads,
|
2023-12-07 23:11:51 +01:00
|
|
|
setupPfpUploads,
|
2023-06-17 01:01:27 +02:00
|
|
|
};
|