mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-18 08:25:30 +01:00
🖼️ feat: File Size and MIME Type Filtering at Agent level (#10446)
* refactor: add image file size validation as part of payload build * feat: implement file size and MIME type filtering in endpoint configuration * chore: import order
This commit is contained in:
parent
b443254151
commit
937563f645
4 changed files with 768 additions and 7 deletions
|
|
@ -16,6 +16,11 @@ export interface AudioValidationResult {
|
|||
error?: string;
|
||||
}
|
||||
|
||||
export interface ImageValidationResult {
|
||||
isValid: boolean;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export async function validatePdf(
|
||||
pdfBuffer: Buffer,
|
||||
fileSize: number,
|
||||
|
|
@ -229,3 +234,53 @@ export async function validateAudio(
|
|||
|
||||
return { isValid: true };
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates image files for different providers
|
||||
* @param imageBuffer - The image file as a buffer
|
||||
* @param fileSize - The file size in bytes
|
||||
* @param provider - The provider to validate for
|
||||
* @param configuredFileSizeLimit - Optional configured file size limit from fileConfig (in bytes)
|
||||
* @returns Promise that resolves to validation result
|
||||
*/
|
||||
export async function validateImage(
|
||||
imageBuffer: Buffer,
|
||||
fileSize: number,
|
||||
provider: Providers | string,
|
||||
configuredFileSizeLimit?: number,
|
||||
): Promise<ImageValidationResult> {
|
||||
if (provider === Providers.GOOGLE || provider === Providers.VERTEXAI) {
|
||||
const providerLimit = mbToBytes(20);
|
||||
const effectiveLimit = configuredFileSizeLimit ?? providerLimit;
|
||||
|
||||
if (fileSize > effectiveLimit) {
|
||||
const limitMB = Math.round(effectiveLimit / (1024 * 1024));
|
||||
return {
|
||||
isValid: false,
|
||||
error: `Image file size (${Math.round(fileSize / (1024 * 1024))}MB) exceeds the ${limitMB}MB limit`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (provider === Providers.ANTHROPIC) {
|
||||
const providerLimit = mbToBytes(5);
|
||||
const effectiveLimit = configuredFileSizeLimit ?? providerLimit;
|
||||
|
||||
if (fileSize > effectiveLimit) {
|
||||
const limitMB = Math.round(effectiveLimit / (1024 * 1024));
|
||||
return {
|
||||
isValid: false,
|
||||
error: `Image file size (${Math.round(fileSize / (1024 * 1024))}MB) exceeds the ${limitMB}MB limit`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (!imageBuffer || imageBuffer.length < 10) {
|
||||
return {
|
||||
isValid: false,
|
||||
error: 'Invalid image file: too small or corrupted',
|
||||
};
|
||||
}
|
||||
|
||||
return { isValid: true };
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue