mirror of
https://github.com/farcasclaudiu/Flowise.git
synced 2026-06-28 01:00:46 +03:00
Bugfix/Upsert files extension to input field (#3288)
get upsert files extension to input field
This commit is contained in:
@@ -872,6 +872,35 @@ export const getVersion: () => Promise<{ version: string }> = async () => {
|
||||
throw new Error('None of the package.json paths could be parsed')
|
||||
}
|
||||
|
||||
/**
|
||||
* Map Ext to InputField
|
||||
* @param {string} ext
|
||||
* @returns {string}
|
||||
*/
|
||||
export const mapExtToInputField = (ext: string) => {
|
||||
switch (ext) {
|
||||
case '.txt':
|
||||
return 'txtFile'
|
||||
case '.pdf':
|
||||
return 'pdfFile'
|
||||
case '.json':
|
||||
return 'jsonFile'
|
||||
case '.csv':
|
||||
case '.xls':
|
||||
case '.xlsx':
|
||||
return 'csvFile'
|
||||
case '.jsonl':
|
||||
return 'jsonlinesFile'
|
||||
case '.docx':
|
||||
case '.doc':
|
||||
return 'docxFile'
|
||||
case '.yaml':
|
||||
return 'yamlFile'
|
||||
default:
|
||||
return 'txtFile'
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Map MimeType to InputField
|
||||
* @param {string} mimeType
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Request } from 'express'
|
||||
import * as path from 'path'
|
||||
import {
|
||||
IFileUpload,
|
||||
convertSpeechToText,
|
||||
@@ -6,6 +7,7 @@ import {
|
||||
addSingleFileToStorage,
|
||||
addArrayFilesToStorage,
|
||||
mapMimeTypeToInputField,
|
||||
mapExtToInputField,
|
||||
IServerSideEventStreamer
|
||||
} from 'flowise-components'
|
||||
import { StatusCodes } from 'http-status-codes'
|
||||
@@ -151,9 +153,33 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals
|
||||
|
||||
const storagePath = await addArrayFilesToStorage(file.mimetype, fileBuffer, file.originalname, fileNames, chatflowid)
|
||||
|
||||
const fileInputField = mapMimeTypeToInputField(file.mimetype)
|
||||
const fileInputFieldFromMimeType = mapMimeTypeToInputField(file.mimetype)
|
||||
|
||||
overrideConfig[fileInputField] = storagePath
|
||||
const fileExtension = path.extname(file.originalname)
|
||||
|
||||
const fileInputFieldFromExt = mapExtToInputField(fileExtension)
|
||||
|
||||
let fileInputField = 'txtFile'
|
||||
|
||||
if (fileInputFieldFromExt !== 'txtFile') {
|
||||
fileInputField = fileInputFieldFromExt
|
||||
} else if (fileInputFieldFromMimeType !== 'txtFile') {
|
||||
fileInputField = fileInputFieldFromExt
|
||||
}
|
||||
|
||||
if (overrideConfig[fileInputField]) {
|
||||
const existingFileInputField = overrideConfig[fileInputField].replace('FILE-STORAGE::', '')
|
||||
const existingFileInputFieldArray = JSON.parse(existingFileInputField)
|
||||
|
||||
const newFileInputField = storagePath.replace('FILE-STORAGE::', '')
|
||||
const newFileInputFieldArray = JSON.parse(newFileInputField)
|
||||
|
||||
const updatedFieldArray = existingFileInputFieldArray.concat(newFileInputFieldArray)
|
||||
|
||||
overrideConfig[fileInputField] = `FILE-STORAGE::${JSON.stringify(updatedFieldArray)}`
|
||||
} else {
|
||||
overrideConfig[fileInputField] = storagePath
|
||||
}
|
||||
|
||||
fs.unlinkSync(file.path)
|
||||
}
|
||||
@@ -161,6 +187,9 @@ export const utilBuildChatflow = async (req: Request, isInternal: boolean = fals
|
||||
question: req.body.question ?? 'hello',
|
||||
overrideConfig
|
||||
}
|
||||
if (req.body.chatId) {
|
||||
incomingInput.chatId = req.body.chatId
|
||||
}
|
||||
}
|
||||
|
||||
/*** Get chatflows and prepare data ***/
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { Request } from 'express'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { cloneDeep, omit } from 'lodash'
|
||||
import { ICommonObject, IMessage, addArrayFilesToStorage, mapMimeTypeToInputField } from 'flowise-components'
|
||||
import { ICommonObject, IMessage, addArrayFilesToStorage, mapMimeTypeToInputField, mapExtToInputField } from 'flowise-components'
|
||||
import telemetryService from '../services/telemetry'
|
||||
import logger from '../utils/logger'
|
||||
import {
|
||||
@@ -52,15 +53,39 @@ export const upsertVector = async (req: Request, isInternal: boolean = false) =>
|
||||
|
||||
if (files.length) {
|
||||
const overrideConfig: ICommonObject = { ...req.body }
|
||||
const fileNames: string[] = []
|
||||
for (const file of files) {
|
||||
const fileNames: string[] = []
|
||||
const fileBuffer = fs.readFileSync(file.path)
|
||||
|
||||
const storagePath = await addArrayFilesToStorage(file.mimetype, fileBuffer, file.originalname, fileNames, chatflowid)
|
||||
|
||||
const fileInputField = mapMimeTypeToInputField(file.mimetype)
|
||||
const fileInputFieldFromMimeType = mapMimeTypeToInputField(file.mimetype)
|
||||
|
||||
overrideConfig[fileInputField] = storagePath
|
||||
const fileExtension = path.extname(file.originalname)
|
||||
|
||||
const fileInputFieldFromExt = mapExtToInputField(fileExtension)
|
||||
|
||||
let fileInputField = 'txtFile'
|
||||
|
||||
if (fileInputFieldFromExt !== 'txtFile') {
|
||||
fileInputField = fileInputFieldFromExt
|
||||
} else if (fileInputFieldFromMimeType !== 'txtFile') {
|
||||
fileInputField = fileInputFieldFromExt
|
||||
}
|
||||
|
||||
if (overrideConfig[fileInputField]) {
|
||||
const existingFileInputField = overrideConfig[fileInputField].replace('FILE-STORAGE::', '')
|
||||
const existingFileInputFieldArray = JSON.parse(existingFileInputField)
|
||||
|
||||
const newFileInputField = storagePath.replace('FILE-STORAGE::', '')
|
||||
const newFileInputFieldArray = JSON.parse(newFileInputField)
|
||||
|
||||
const updatedFieldArray = existingFileInputFieldArray.concat(newFileInputFieldArray)
|
||||
|
||||
overrideConfig[fileInputField] = `FILE-STORAGE::${JSON.stringify(updatedFieldArray)}`
|
||||
} else {
|
||||
overrideConfig[fileInputField] = storagePath
|
||||
}
|
||||
|
||||
fs.unlinkSync(file.path)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user