Enhance file upload functionality in createAttachment

- Added support for configurable allowed file types and file upload status in createFileAttachment.
- Implemented validation to ensure uploaded files match allowed types, throwing errors for disallowed types.
This commit is contained in:
Henry 2025-10-01 10:47:09 +01:00
parent 28b0174eea
commit d86a81aff1
2 changed files with 39 additions and 7 deletions

View File

@ -75,11 +75,22 @@ export const createFileAttachment = async (req: Request) => {
usage: 'perPage',
legacyBuild: false
}
let allowedFileTypes: string[] = []
let fileUploadEnabled = false
if (chatflow.chatbotConfig) {
try {
const chatbotConfig = JSON.parse(chatflow.chatbotConfig)
if (chatbotConfig?.fullFileUpload?.pdfFile) {
if (chatbotConfig?.fullFileUpload) {
fileUploadEnabled = chatbotConfig.fullFileUpload.status
// Get allowed file types from configuration
if (chatbotConfig.fullFileUpload.allowedUploadFileTypes) {
allowedFileTypes = chatbotConfig.fullFileUpload.allowedUploadFileTypes.split(',')
}
// PDF specific configuration
if (chatbotConfig.fullFileUpload.pdfFile) {
if (chatbotConfig.fullFileUpload.pdfFile.usage) {
pdfConfig.usage = chatbotConfig.fullFileUpload.pdfFile.usage
}
@ -87,9 +98,15 @@ export const createFileAttachment = async (req: Request) => {
pdfConfig.legacyBuild = chatbotConfig.fullFileUpload.pdfFile.legacyBuild
}
}
} catch (e) {
// Use default PDF config if parsing fails
}
} catch (e) {
// Use default config if parsing fails
}
}
// Check if file upload is enabled
if (!fileUploadEnabled) {
throw new InternalFlowiseError(StatusCodes.BAD_REQUEST, 'File upload is not enabled for this chatflow')
}
// Find FileLoader node
@ -109,6 +126,21 @@ export const createFileAttachment = async (req: Request) => {
if (files.length) {
const isBase64 = req.body.base64
for (const file of files) {
if (!allowedFileTypes.length) {
throw new InternalFlowiseError(
StatusCodes.BAD_REQUEST,
`File type '${file.mimetype}' is not allowed. Allowed types: ${allowedFileTypes.join(', ')}`
)
}
// Validate file type against allowed types
if (allowedFileTypes.length > 0 && !allowedFileTypes.includes(file.mimetype)) {
throw new InternalFlowiseError(
StatusCodes.BAD_REQUEST,
`File type '${file.mimetype}' is not allowed. Allowed types: ${allowedFileTypes.join(', ')}`
)
}
await checkStorage(orgId, subscriptionId, appServer.usageCacheManager)
const fileBuffer = await getFileFromUpload(file.path ?? file.key)

View File

@ -18,7 +18,7 @@ import useNotifier from '@/utils/useNotifier'
// API
import chatflowsApi from '@/api/chatflows'
const message = `Uploaded files will be parsed as strings and sent to the LLM. If file upload is enabled on the Vector Store as well, this will override and take precedence.
const message = `The full contents of uploaded files will be converted to text and sent to the Agent.
<br />
Refer <a href='https://docs.flowiseai.com/using-flowise/uploads#files' target='_blank'>docs</a> for more details.`