1- import { Client , type RemoveOptions , type CopyConditions , type LifecycleConfig } from 'minio' ;
1+ import { Client , type RemoveOptions , type CopyConditions } from 'minio' ;
22import {
3- type ExtensionType ,
43 type CreatePostPresignedUrlOptions ,
54 type CreatePostPresignedUrlParams ,
65 type CreatePostPresignedUrlResult ,
98import { defaultS3Options , Mimes } from '../constants' ;
109import path from 'node:path' ;
1110import { MongoS3TTL } from '../schema' ;
12- import { UserError } from '@fastgpt/global/common/error/utils' ;
1311import { getNanoid } from '@fastgpt/global/common/string/tools' ;
1412import { addHours } from 'date-fns' ;
13+ import { addLog } from '../../system/log' ;
1514
1615export class S3BaseBucket {
1716 private _client : Client ;
@@ -56,15 +55,18 @@ export class S3BaseBucket {
5655 await this . client . makeBucket ( this . bucketName ) ;
5756 }
5857 await this . options . afterInit ?.( ) ;
58+ console . log ( `S3 init success: ${ this . name } ` ) ;
5959 } ;
6060 init ( ) ;
6161 }
6262
6363 get name ( ) : string {
6464 return this . bucketName ;
6565 }
66-
67- protected get client ( ) : Client {
66+ get client ( ) : Client {
67+ return this . _client ;
68+ }
69+ get externalClient ( ) : Client {
6870 return this . _externalClient ?? this . _client ;
6971 }
7072
@@ -93,30 +95,31 @@ export class S3BaseBucket {
9395 try {
9496 const { expiredHours } = options ;
9597 const filename = params . filename ;
96- const ext = path . extname ( filename ) . toLowerCase ( ) as ExtensionType ;
97- const contentType = Mimes [ ext ] ?? 'application/octet-stream' ;
98- const maxFileSize = this . options . maxFileSize as number ;
98+ const ext = path . extname ( filename ) . toLowerCase ( ) ;
99+ const contentType = Mimes [ ext as keyof typeof Mimes ] ?? 'application/octet-stream' ;
100+ const maxFileSize = this . options . maxFileSize ;
99101
100102 const key = ( ( ) => {
101103 if ( 'rawKey' in params ) return params . rawKey ;
102104
103105 return `${ params . source } /${ params . teamId } /${ getNanoid ( 6 ) } -${ filename } ` ;
104106 } ) ( ) ;
105107
106- const policy = this . client . newPostPolicy ( ) ;
108+ const policy = this . externalClient . newPostPolicy ( ) ;
107109 policy . setKey ( key ) ;
108110 policy . setBucket ( this . name ) ;
109111 policy . setContentType ( contentType ) ;
110- policy . setContentLengthRange ( 1 , maxFileSize ) ;
112+ if ( maxFileSize ) {
113+ policy . setContentLengthRange ( 1 , maxFileSize ) ;
114+ }
111115 policy . setExpires ( new Date ( Date . now ( ) + 10 * 60 * 1000 ) ) ;
112116 policy . setUserMetaData ( {
113- 'content-type' : contentType ,
114117 'content-disposition' : `attachment; filename="${ encodeURIComponent ( filename ) } "` ,
115118 'origin-filename' : encodeURIComponent ( filename ) ,
116119 'upload-time' : new Date ( ) . toISOString ( )
117120 } ) ;
118121
119- const { formData, postURL } = await this . client . presignedPostPolicy ( policy ) ;
122+ const { formData, postURL } = await this . externalClient . presignedPostPolicy ( policy ) ;
120123
121124 if ( expiredHours ) {
122125 await MongoS3TTL . create ( {
@@ -131,7 +134,8 @@ export class S3BaseBucket {
131134 fields : formData
132135 } ;
133136 } catch ( error ) {
134- return Promise . reject ( error ) ;
137+ addLog . error ( 'Failed to create post presigned url' , error ) ;
138+ return Promise . reject ( 'Failed to create post presigned url' ) ;
135139 }
136140 }
137141}
0 commit comments