mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 08:12:00 +02:00

* chore: typing * refactor: create file filter from custom fileConfig, if provided * refactor: use logger utility to avoid overly verbose axios error logs when using RAG_API * fix(useFileHandling): use memoization/callbacks to make sure the appropriate fileConfig is used; refactor: move endpoint to first field applied to formdata * chore: update librechat-data-provider version to 0.7.54 * chore: revert type change
75 lines
2.3 KiB
JavaScript
75 lines
2.3 KiB
JavaScript
const fs = require('fs');
|
|
const path = require('path');
|
|
const crypto = require('crypto');
|
|
const multer = require('multer');
|
|
const { fileConfig: defaultFileConfig, mergeFileConfig } = require('librechat-data-provider');
|
|
const { getCustomConfig } = require('~/server/services/Config');
|
|
|
|
const storage = multer.diskStorage({
|
|
destination: function (req, file, cb) {
|
|
const outputPath = path.join(req.app.locals.paths.uploads, 'temp', req.user.id);
|
|
if (!fs.existsSync(outputPath)) {
|
|
fs.mkdirSync(outputPath, { recursive: true });
|
|
}
|
|
cb(null, outputPath);
|
|
},
|
|
filename: function (req, file, cb) {
|
|
req.file_id = crypto.randomUUID();
|
|
file.originalname = decodeURIComponent(file.originalname);
|
|
cb(null, `${file.originalname}`);
|
|
},
|
|
});
|
|
|
|
const importFileFilter = (req, file, cb) => {
|
|
if (file.mimetype === 'application/json') {
|
|
cb(null, true);
|
|
} else if (path.extname(file.originalname).toLowerCase() === '.json') {
|
|
cb(null, true);
|
|
} else {
|
|
cb(new Error('Only JSON files are allowed'), false);
|
|
}
|
|
};
|
|
|
|
/**
|
|
*
|
|
* @param {import('librechat-data-provider').FileConfig | undefined} customFileConfig
|
|
*/
|
|
const createFileFilter = (customFileConfig) => {
|
|
/**
|
|
* @param {ServerRequest} req
|
|
* @param {Express.Multer.File}
|
|
* @param {import('multer').FileFilterCallback} cb
|
|
*/
|
|
const fileFilter = (req, file, cb) => {
|
|
if (!file) {
|
|
return cb(new Error('No file provided'), false);
|
|
}
|
|
|
|
const endpoint = req.body.endpoint;
|
|
const supportedTypes =
|
|
customFileConfig?.endpoints?.[endpoint]?.supportedMimeTypes ??
|
|
customFileConfig?.endpoints?.default.supportedMimeTypes ??
|
|
defaultFileConfig?.endpoints?.[endpoint]?.supportedMimeTypes;
|
|
|
|
if (!defaultFileConfig.checkType(file.mimetype, supportedTypes)) {
|
|
return cb(new Error('Unsupported file type: ' + file.mimetype), false);
|
|
}
|
|
|
|
cb(null, true);
|
|
};
|
|
|
|
return fileFilter;
|
|
};
|
|
|
|
const createMulterInstance = async () => {
|
|
const customConfig = await getCustomConfig();
|
|
const fileConfig = mergeFileConfig(customConfig?.fileConfig);
|
|
const fileFilter = createFileFilter(fileConfig);
|
|
return multer({
|
|
storage,
|
|
fileFilter,
|
|
limits: { fileSize: fileConfig.serverFileSizeLimit },
|
|
});
|
|
};
|
|
|
|
module.exports = { createMulterInstance, storage, importFileFilter };
|