@@ -120,6 +120,37 @@ export const addSingleFileToStorage = async (mime: string, bf: Buffer, fileName:
120
120
}
121
121
}
122
122
123
+ export const getFileFromUpload = async ( filePath : string ) : Promise < Buffer > => {
124
+ const storageType = getStorageType ( )
125
+ if ( storageType === 's3' ) {
126
+ const { s3Client, Bucket } = getS3Config ( )
127
+
128
+ let Key = filePath
129
+ // remove the first '/' if it exists
130
+ if ( Key . startsWith ( '/' ) ) {
131
+ Key = Key . substring ( 1 )
132
+ }
133
+ const getParams = {
134
+ Bucket,
135
+ Key
136
+ }
137
+
138
+ const response = await s3Client . send ( new GetObjectCommand ( getParams ) )
139
+ const body = response . Body
140
+ if ( body instanceof Readable ) {
141
+ const streamToString = await body . transformToString ( 'base64' )
142
+ if ( streamToString ) {
143
+ return Buffer . from ( streamToString , 'base64' )
144
+ }
145
+ }
146
+ // @ts -ignore
147
+ const buffer = Buffer . concat ( response . Body . toArray ( ) )
148
+ return buffer
149
+ } else {
150
+ return fs . readFileSync ( filePath )
151
+ }
152
+ }
153
+
123
154
export const getFileFromStorage = async ( file : string , ...paths : string [ ] ) : Promise < Buffer > => {
124
155
const storageType = getStorageType ( )
125
156
const sanitizedFilename = _sanitizeFilename ( file )
@@ -183,6 +214,20 @@ export const removeFilesFromStorage = async (...paths: string[]) => {
183
214
}
184
215
}
185
216
217
+ export const removeSpecificFileFromUpload = async ( filePath : string ) => {
218
+ const storageType = getStorageType ( )
219
+ if ( storageType === 's3' ) {
220
+ let Key = filePath
221
+ // remove the first '/' if it exists
222
+ if ( Key . startsWith ( '/' ) ) {
223
+ Key = Key . substring ( 1 )
224
+ }
225
+ await _deleteS3Folder ( Key )
226
+ } else {
227
+ fs . unlinkSync ( filePath )
228
+ }
229
+ }
230
+
186
231
export const removeSpecificFileFromStorage = async ( ...paths : string [ ] ) => {
187
232
const storageType = getStorageType ( )
188
233
if ( storageType === 's3' ) {
0 commit comments