1- import { uploadMongoImg } from '../image/controller' ;
21import FormData from 'form-data' ;
32import fs from 'fs' ;
43import type { ReadFileResponse } from '../../../worker/readFile/type' ;
@@ -9,12 +8,9 @@ import { matchMdImg } from '@fastgpt/global/common/string/markdown';
98import { createPdfParseUsage } from '../../../support/wallet/usage/controller' ;
109import { useDoc2xServer } from '../../../thirdProvider/doc2x' ;
1110import { readRawContentFromBuffer } from '../../../worker/function' ;
12- import { getS3DatasetSource } from '../../s3/sources/dataset' ;
13- import type { ParsedFileContentS3KeyParams } from '../../s3/sources/dataset/type' ;
14- import { getNanoid } from '@fastgpt/global/common/string/tools' ;
15- import path from 'path' ;
16- import { S3Sources } from '../../s3/type' ;
17- import { randomUUID } from 'crypto' ;
11+ import { uploadImage2S3Bucket } from '../../s3/utils' ;
12+ import { Mimes } from '../../s3/constants' ;
13+ import { addDays } from 'date-fns' ;
1814
1915export type readRawTextByLocalFileParams = {
2016 teamId : string ;
@@ -42,7 +38,8 @@ export const readRawTextByLocalFile = async (params: readRawTextByLocalFileParam
4238 encoding : params . encoding ,
4339 buffer,
4440 imageKeyOptions : {
45- prefix : params . uploadKey
41+ prefix : params . uploadKey ,
42+ expiredTime : addDays ( new Date ( ) , 1 )
4643 }
4744 } ) ;
4845} ;
@@ -71,11 +68,10 @@ export const readS3FileContentByBuffer = async ({
7168 getFormatText ?: boolean ;
7269 imageKeyOptions : {
7370 prefix : string ;
74- hasTTL ?: boolean ;
71+ expiredTime ?: Date ;
7572 } ;
7673} ) : Promise < {
7774 rawText : string ;
78- imageKeys ?: string [ ] ;
7975} > => {
8076 const systemParse = ( ) =>
8177 readRawContentFromBuffer ( {
@@ -171,26 +167,23 @@ export const readS3FileContentByBuffer = async ({
171167 addLog . debug ( `Parse file success, time: ${ Date . now ( ) - start } ms. ` ) ;
172168
173169 // markdown data format
174- const uploadedImageKeys : string [ ] = [ ] ;
175170 if ( imageList && imageList . length > 0 ) {
176171 addLog . debug ( `Processing ${ imageList . length } images from parsed document` ) ;
177172
178173 await batchRun ( imageList , async ( item ) => {
179174 const src = await ( async ( ) => {
180175 try {
181- const { prefix, hasTTL } = imageKeyOptions ;
182- const ext = item . mime . split ( '/' ) [ 1 ] . replace ( 'x-' , '' ) ;
183- const imageKey = await getS3DatasetSource ( ) . uploadDatasetImage ( {
176+ const { prefix, expiredTime } = imageKeyOptions ;
177+ const ext = `.${ item . mime . split ( '/' ) [ 1 ] . replace ( 'x-' , '' ) } ` ;
178+
179+ return await uploadImage2S3Bucket ( 'private' , {
184180 base64Img : `data:${ item . mime } ;base64,${ item . base64 } ` ,
185- mimetype : `${ ext } ` ,
186- filename : `${ item . uuid } .${ ext } ` ,
187181 uploadKey : `${ prefix } /${ item . uuid } .${ ext } ` ,
188- hasTTL
182+ mimetype : Mimes [ ext as keyof typeof Mimes ] ,
183+ filename : `${ item . uuid } .${ ext } ` ,
184+ expiredTime
189185 } ) ;
190- uploadedImageKeys . push ( imageKey ) ;
191- return imageKey ;
192186 } catch ( error ) {
193- // Don't add to uploadedImageKeys if upload failed, but still continue processing
194187 return `[Image Upload Failed: ${ item . uuid } ]` ;
195188 }
196189 } ) ( ) ;
@@ -199,63 +192,9 @@ export const readS3FileContentByBuffer = async ({
199192 formatText = formatText . replace ( item . uuid , src ) ;
200193 }
201194 } ) ;
202-
203- // Log summary of image processing
204- addLog . info ( `Image processing completed` , {
205- total : imageList . length ,
206- successful : uploadedImageKeys . length ,
207- failed : imageList . length - uploadedImageKeys . length
208- } ) ;
209195 }
210196
211- addLog . debug ( `Upload file to S3 success, time: ${ Date . now ( ) - start } ms` , {
212- uploadedImageKeysCount : uploadedImageKeys . length ,
213- uploadedImageKeys
214- } ) ;
215-
216197 return {
217- rawText : getFormatText ? formatText || rawText : rawText ,
218- imageKeys : uploadedImageKeys
198+ rawText : getFormatText ? formatText || rawText : rawText
219199 } ;
220200} ;
221-
222- export const parsedFileContentS3Key = {
223- // 临时的文件路径(比如 evaluation)
224- temp : ( appId : string ) => `chat/${ appId } /temp/parsed/${ randomUUID ( ) } ` ,
225-
226- // 对话中上传的文件的解析结果的图片的 Key
227- chat : ( { appId, chatId, uId } : { chatId : string ; uId : string ; appId : string } ) =>
228- `chat/${ appId } /${ uId } /${ chatId } /parsed` ,
229-
230- // 上传数据集的文件的解析结果的图片的 Key
231- dataset : ( params : ParsedFileContentS3KeyParams ) => {
232- const { datasetId, mimetype, filename, parentFileKey } = params ;
233-
234- const extension = mimetype ;
235- const image = ( ( ) => {
236- if ( filename ) {
237- return Boolean ( path . extname ( filename ) )
238- ? `${ getNanoid ( 6 ) } -${ filename } `
239- : `${ getNanoid ( 6 ) } -${ filename } .${ extension } ` ;
240- }
241- return `${ getNanoid ( 6 ) } .${ extension } ` ;
242- } ) ( ) ;
243-
244- const parentFilename = parentFileKey ?. slice ( ) . split ( '/' ) . at ( - 1 ) ;
245- const parsedParentFilename = parentFilename
246- ? `parsed-${ path . basename ( parentFilename , path . extname ( parentFilename ) ) } `
247- : '' ;
248- const parsedParentFileKey = parentFileKey
249- ?. split ( '/' )
250- . slice ( 0 , - 1 )
251- . concat ( parsedParentFilename )
252- . join ( '/' ) ;
253-
254- return {
255- key : parsedParentFileKey
256- ? `${ parsedParentFileKey } /${ image } `
257- : [ S3Sources . dataset , datasetId , image ] . join ( '/' ) ,
258- filename : image
259- } ;
260- }
261- } ;
0 commit comments