mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-24 04:10:15 +01:00
⚙️ 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:
parent
d60a0af878
commit
49ee88b6e8
5 changed files with 123 additions and 82 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ const fs = require('fs');
|
|||
const axios = require('axios');
|
||||
const FormData = require('form-data');
|
||||
const { FileSources } = require('librechat-data-provider');
|
||||
const { logAxiosError } = require('~/utils');
|
||||
const { logger } = require('~/config');
|
||||
|
||||
/**
|
||||
|
|
@ -32,7 +33,10 @@ const deleteVectors = async (req, file) => {
|
|||
data: [file.file_id],
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error('Error deleting vectors', error);
|
||||
logAxiosError({
|
||||
error,
|
||||
message: 'Error deleting vectors',
|
||||
});
|
||||
throw new Error(error.message || 'An error occurred during file deletion.');
|
||||
}
|
||||
};
|
||||
|
|
@ -91,7 +95,10 @@ async function uploadVectors({ req, file, file_id }) {
|
|||
embedded: Boolean(responseData.known_type),
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Error embedding file', error);
|
||||
logAxiosError({
|
||||
error,
|
||||
message: 'Error uploading vectors',
|
||||
});
|
||||
throw new Error(error.message || 'An error occurred during file upload.');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue