Skip to content

Commit

Permalink
Bugfix/Upsert files extension to input field (#3288)
Browse files Browse the repository at this point in the history
get upsert files extension to input field
  • Loading branch information
HenryHengZJ authored Sep 30, 2024
1 parent 01ae2ac commit 04e8d02
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 6 deletions.
29 changes: 29 additions & 0 deletions packages/components/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
33 changes: 31 additions & 2 deletions packages/server/src/utils/buildChatflow.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Request } from 'express'
import * as path from 'path'
import {
IFileUpload,
convertSpeechToText,
ICommonObject,
addSingleFileToStorage,
addArrayFilesToStorage,
mapMimeTypeToInputField,
mapExtToInputField,
IServerSideEventStreamer
} from 'flowise-components'
import { StatusCodes } from 'http-status-codes'
Expand Down Expand Up @@ -151,16 +153,43 @@ 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)
}
incomingInput = {
question: req.body.question ?? 'hello',
overrideConfig
}
if (req.body.chatId) {
incomingInput.chatId = req.body.chatId
}
}

/*** Get chatflows and prepare data ***/
Expand Down
33 changes: 29 additions & 4 deletions packages/server/src/utils/upsertVector.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 04e8d02

Please sign in to comment.