1
- import { GetObjectCommand , DeleteObjectCommand , PutObjectCommand , ListObjectsV2Command , DeleteObjectsCommand , ListObjectsV2CommandOutput } from '@aws-sdk/client-s3'
1
+ import {
2
+ GetObjectCommand ,
3
+ DeleteObjectCommand ,
4
+ PutObjectCommand ,
5
+ ListObjectsV2Command ,
6
+ DeleteObjectsCommand ,
7
+ ListObjectsV2CommandOutput ,
8
+ } from '@aws-sdk/client-s3'
2
9
import type { RagFile , RagIndex } from '../../db/models'
3
10
import { ApplicationError } from '../../util/ApplicationError'
4
11
import { pdfToText , pdfToTextWithVLM } from '../../util/pdfToText'
@@ -27,16 +34,16 @@ export const FileStore = {
27
34
Prefix : prefix ,
28
35
ContinuationToken : continuationToken ,
29
36
} )
30
- const listResponse = await s3Client . send ( listCommand ) as ListObjectsV2CommandOutput
31
- const keys = ( listResponse . Contents || [ ] ) . map ( obj => ( { Key : obj . Key ! } ) )
37
+ const listResponse = ( await s3Client . send ( listCommand ) ) as ListObjectsV2CommandOutput
38
+ const keys = ( listResponse . Contents || [ ] ) . map ( ( obj ) => ( { Key : obj . Key ! } ) )
32
39
33
40
if ( keys . length > 0 ) {
34
41
const deleteCommand = new DeleteObjectsCommand ( {
35
42
Bucket : S3_BUCKET ,
36
- Delete : { Objects : keys }
43
+ Delete : { Objects : keys } ,
37
44
} )
38
45
const deleteResponse = await s3Client . send ( deleteCommand )
39
- console . log ( " Deleted:" , deleteResponse . Deleted ?. length , " objects." )
46
+ console . log ( ' Deleted:' , deleteResponse . Deleted ?. length , ' objects.' )
40
47
}
41
48
42
49
continuationToken = listResponse . NextContinuationToken
@@ -81,13 +88,15 @@ export const FileStore = {
81
88
try {
82
89
const fileObj = await s3Client . send ( new GetObjectCommand ( { Bucket : S3_BUCKET , Key : s3Key } ) )
83
90
const buf = await streamToBuffer ( fileObj . Body )
84
- const text = await pdfToTextWithVLM ( buf )
85
- await s3Client . send ( new PutObjectCommand ( {
86
- Bucket : S3_BUCKET ,
87
- Key : pdfTextKey ,
88
- Body : JSON . stringify ( text , null , 2 ) ,
89
- ContentType : 'text/plain' ,
90
- } ) )
91
+ const text = await pdfToText ( buf )
92
+ await s3Client . send (
93
+ new PutObjectCommand ( {
94
+ Bucket : S3_BUCKET ,
95
+ Key : pdfTextKey ,
96
+ Body : JSON . stringify ( text , null , 2 ) ,
97
+ ContentType : 'text/plain' ,
98
+ } ) ,
99
+ )
91
100
return text
92
101
} catch ( error ) {
93
102
console . error ( `Failed to create PDF text file ${ pdfTextKey } in S3:` , error )
@@ -114,12 +123,14 @@ export const FileStore = {
114
123
115
124
async saveText ( s3Key : string , text : string ) {
116
125
try {
117
- await s3Client . send ( new PutObjectCommand ( {
118
- Bucket : S3_BUCKET ,
119
- Key : s3Key ,
120
- Body : text ,
121
- ContentType : 'text/plain' ,
122
- } ) )
126
+ await s3Client . send (
127
+ new PutObjectCommand ( {
128
+ Bucket : S3_BUCKET ,
129
+ Key : s3Key ,
130
+ Body : text ,
131
+ ContentType : 'text/plain' ,
132
+ } ) ,
133
+ )
123
134
} catch ( error ) {
124
135
console . error ( `Failed to save text content to ${ s3Key } in S3:` , error )
125
136
throw ApplicationError . InternalServerError ( `Failed to save text content` )
@@ -136,7 +147,6 @@ const streamToString = (stream: any): Promise<string> => {
136
147
} )
137
148
}
138
149
139
-
140
150
const streamToBuffer = ( stream : any ) : Promise < Buffer > => {
141
151
return new Promise ( ( resolve , reject ) => {
142
152
const chunks : any [ ] = [ ]
@@ -145,4 +155,3 @@ const streamToBuffer = (stream: any): Promise<Buffer> => {
145
155
stream . on ( 'end' , ( ) => resolve ( Buffer . concat ( chunks ) ) )
146
156
} )
147
157
}
148
-
0 commit comments