Skip to content

Commit 1a410d8

Browse files
authored
Chore/Enhance file upload functionality in createAttachment (#5280)
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.
1 parent 7a50755 commit 1a410d8

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

packages/server/src/utils/createAttachment.ts

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,40 @@ export const createFileAttachment = async (req: Request) => {
7575
usage: 'perPage',
7676
legacyBuild: false
7777
}
78+
let allowedFileTypes: string[] = []
79+
let fileUploadEnabled = false
7880

7981
if (chatflow.chatbotConfig) {
8082
try {
8183
const chatbotConfig = JSON.parse(chatflow.chatbotConfig)
82-
if (chatbotConfig?.fullFileUpload?.pdfFile) {
83-
if (chatbotConfig.fullFileUpload.pdfFile.usage) {
84-
pdfConfig.usage = chatbotConfig.fullFileUpload.pdfFile.usage
84+
if (chatbotConfig?.fullFileUpload) {
85+
fileUploadEnabled = chatbotConfig.fullFileUpload.status
86+
87+
// Get allowed file types from configuration
88+
if (chatbotConfig.fullFileUpload.allowedUploadFileTypes) {
89+
allowedFileTypes = chatbotConfig.fullFileUpload.allowedUploadFileTypes.split(',')
8590
}
86-
if (chatbotConfig.fullFileUpload.pdfFile.legacyBuild !== undefined) {
87-
pdfConfig.legacyBuild = chatbotConfig.fullFileUpload.pdfFile.legacyBuild
91+
92+
// PDF specific configuration
93+
if (chatbotConfig.fullFileUpload.pdfFile) {
94+
if (chatbotConfig.fullFileUpload.pdfFile.usage) {
95+
pdfConfig.usage = chatbotConfig.fullFileUpload.pdfFile.usage
96+
}
97+
if (chatbotConfig.fullFileUpload.pdfFile.legacyBuild !== undefined) {
98+
pdfConfig.legacyBuild = chatbotConfig.fullFileUpload.pdfFile.legacyBuild
99+
}
88100
}
89101
}
90102
} catch (e) {
91-
// Use default PDF config if parsing fails
103+
// Use default config if parsing fails
92104
}
93105
}
94106

107+
// Check if file upload is enabled
108+
if (!fileUploadEnabled) {
109+
throw new InternalFlowiseError(StatusCodes.BAD_REQUEST, 'File upload is not enabled for this chatflow')
110+
}
111+
95112
// Find FileLoader node
96113
const fileLoaderComponent = appServer.nodesPool.componentNodes['fileLoader']
97114
const fileLoaderNodeInstanceFilePath = fileLoaderComponent.filePath as string
@@ -109,6 +126,21 @@ export const createFileAttachment = async (req: Request) => {
109126
if (files.length) {
110127
const isBase64 = req.body.base64
111128
for (const file of files) {
129+
if (!allowedFileTypes.length) {
130+
throw new InternalFlowiseError(
131+
StatusCodes.BAD_REQUEST,
132+
`File type '${file.mimetype}' is not allowed. Allowed types: ${allowedFileTypes.join(', ')}`
133+
)
134+
}
135+
136+
// Validate file type against allowed types
137+
if (allowedFileTypes.length > 0 && !allowedFileTypes.includes(file.mimetype)) {
138+
throw new InternalFlowiseError(
139+
StatusCodes.BAD_REQUEST,
140+
`File type '${file.mimetype}' is not allowed. Allowed types: ${allowedFileTypes.join(', ')}`
141+
)
142+
}
143+
112144
await checkStorage(orgId, subscriptionId, appServer.usageCacheManager)
113145

114146
const fileBuffer = await getFileFromUpload(file.path ?? file.key)

packages/ui/src/ui-component/extended/FileUpload.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import useNotifier from '@/utils/useNotifier'
1818
// API
1919
import chatflowsApi from '@/api/chatflows'
2020

21-
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.
21+
const message = `The full contents of uploaded files will be converted to text and sent to the Agent.
2222
<br />
2323
Refer <a href='https://docs.flowiseai.com/using-flowise/uploads#files' target='_blank'>docs</a> for more details.`
2424

0 commit comments

Comments
 (0)