⚙️ fix: File Config Handling (#4664)

* 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
This commit is contained in:
Danny Avila 2024-11-07 11:11:20 -05:00 committed by GitHub
parent d60a0af878
commit 49ee88b6e8
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 123 additions and 82 deletions

View file

@ -30,21 +30,41 @@ const importFileFilter = (req, file, cb) => {
}
};
const fileFilter = (req, file, cb) => {
if (!file) {
return cb(new Error('No file provided'), 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);
}
if (!defaultFileConfig.checkType(file.mimetype)) {
return cb(new Error('Unsupported file type: ' + file.mimetype), false);
}
const endpoint = req.body.endpoint;
const supportedTypes =
customFileConfig?.endpoints?.[endpoint]?.supportedMimeTypes ??
customFileConfig?.endpoints?.default.supportedMimeTypes ??
defaultFileConfig?.endpoints?.[endpoint]?.supportedMimeTypes;
cb(null, true);
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,