1- import { uploadMongoImg } from '../image/controller' ;
21import FormData from 'form-data' ;
32import fs from 'fs' ;
43import type { ReadFileResponse } from '../../../worker/readFile/type' ;
@@ -9,6 +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' ;
11+ import { uploadImage2S3Bucket } from '../../s3/utils' ;
12+ import { Mimes } from '../../s3/constants' ;
13+ import { addDays } from 'date-fns' ;
1214
1315export type readRawTextByLocalFileParams = {
1416 teamId : string ;
@@ -17,6 +19,7 @@ export type readRawTextByLocalFileParams = {
1719 encoding : string ;
1820 customPdfParse ?: boolean ;
1921 getFormatText ?: boolean ;
22+ uploadKey : string ;
2023 metadata ?: Record < string , any > ;
2124} ;
2225export const readRawTextByLocalFile = async ( params : readRawTextByLocalFileParams ) => {
@@ -26,41 +29,47 @@ export const readRawTextByLocalFile = async (params: readRawTextByLocalFileParam
2629
2730 const buffer = await fs . promises . readFile ( path ) ;
2831
29- return readRawContentByFileBuffer ( {
32+ return readS3FileContentByBuffer ( {
3033 extension,
3134 customPdfParse : params . customPdfParse ,
3235 getFormatText : params . getFormatText ,
3336 teamId : params . teamId ,
3437 tmbId : params . tmbId ,
3538 encoding : params . encoding ,
3639 buffer,
37- metadata : params . metadata
40+ imageKeyOptions : {
41+ prefix : params . uploadKey ,
42+ expiredTime : addDays ( new Date ( ) , 1 )
43+ }
3844 } ) ;
3945} ;
4046
41- export const readRawContentByFileBuffer = async ( {
47+ export const readS3FileContentByBuffer = async ( {
4248 teamId,
4349 tmbId,
4450
4551 extension,
4652 buffer,
4753 encoding,
48- metadata,
4954 customPdfParse = false ,
5055 usageId,
51- getFormatText = true
56+ getFormatText = true ,
57+ imageKeyOptions
5258} : {
5359 teamId : string ;
5460 tmbId : string ;
5561
5662 extension : string ;
5763 buffer : Buffer ;
5864 encoding : string ;
59- metadata ?: Record < string , any > ;
6065
6166 customPdfParse ?: boolean ;
6267 usageId ?: string ;
6368 getFormatText ?: boolean ;
69+ imageKeyOptions : {
70+ prefix : string ;
71+ expiredTime ?: Date ;
72+ } ;
6473} ) : Promise < {
6574 rawText : string ;
6675} > => {
@@ -158,31 +167,35 @@ export const readRawContentByFileBuffer = async ({
158167 addLog . debug ( `Parse file success, time: ${ Date . now ( ) - start } ms. ` ) ;
159168
160169 // markdown data format
161- if ( imageList ) {
170+ if ( imageList && imageList . length > 0 ) {
171+ addLog . debug ( `Processing ${ imageList . length } images from parsed document` ) ;
172+
162173 await batchRun ( imageList , async ( item ) => {
163174 const src = await ( async ( ) => {
164175 try {
165- return await uploadMongoImg ( {
176+ const { prefix, expiredTime } = imageKeyOptions ;
177+ const ext = `.${ item . mime . split ( '/' ) [ 1 ] . replace ( 'x-' , '' ) } ` ;
178+
179+ return await uploadImage2S3Bucket ( 'private' , {
166180 base64Img : `data:${ item . mime } ;base64,${ item . base64 } ` ,
167- teamId,
168- metadata : {
169- ...metadata ,
170- mime : item . mime
171- }
181+ uploadKey : `${ prefix } /${ item . uuid } .${ ext } ` ,
182+ mimetype : Mimes [ ext as keyof typeof Mimes ] ,
183+ filename : `${ item . uuid } ${ ext } ` ,
184+ expiredTime
172185 } ) ;
173186 } catch ( error ) {
174- addLog . warn ( 'Upload file image error' , { error } ) ;
175- return 'Upload load image error' ;
187+ return `[Image Upload Failed: ${ item . uuid } ]` ;
176188 }
177189 } ) ( ) ;
178190 rawText = rawText . replace ( item . uuid , src ) ;
191+ // rawText = rawText.replace(item.uuid, jwtSignS3ObjectKey(src, addDays(new Date(), 90)));
179192 if ( formatText ) {
180193 formatText = formatText . replace ( item . uuid , src ) ;
181194 }
182195 } ) ;
183196 }
184197
185- addLog . debug ( `Upload file success, time: ${ Date . now ( ) - start } ms` ) ;
186-
187- return { rawText : getFormatText ? formatText || rawText : rawText } ;
198+ return {
199+ rawText : getFormatText ? formatText || rawText : rawText
200+ } ;
188201} ;
0 commit comments