@@ -37,8 +37,6 @@ export type FileInput = z.infer<typeof FileInputSchema>;
3737export class FileService {
3838 private minioClient : Minio . Client ;
3939 private config : FileConfig ;
40- private initPromise : Promise < void > ;
41- private isInitialized = false ;
4240
4341 // ================================
4442 // 2.1 初始化相关
@@ -55,142 +53,12 @@ export class FileService {
5553 accessKey : this . config . accessKey ,
5654 secretKey : this . config . secretKey
5755 } ) ;
58-
59- this . initPromise = this . initialize ( ) ;
60- }
61-
62- private async initialize ( ) : Promise < void > {
63- if ( this . isInitialized ) return ;
64-
65- try {
66- await this . testConnection ( ) ;
67- await this . initBucket ( ) ;
68- this . isInitialized = true ;
69- addLog . info ( 'FileService initialized successfully' ) ;
70- } catch ( error ) {
71- addLog . error ( 'FileService initialization failed:' , error ) ;
72- throw error ;
73- }
74- }
75-
76- private async ensureInitialized ( ) : Promise < void > {
77- if ( ! this . isInitialized ) {
78- await this . initPromise ;
79- }
8056 }
8157
8258 // ================================
8359 // 2.2 连接和存储桶管理
8460 // ================================
8561
86- private async testConnection ( ) : Promise < void > {
87- try {
88- addLog . info (
89- `Testing MinIO connection to ${ this . config . endpoint } :${ this . config . port } (SSL: ${ this . config . useSSL } )`
90- ) ;
91- const buckets = await this . minioClient . listBuckets ( ) ;
92- addLog . info (
93- `MinIO connection successful. Found ${ buckets . length } buckets${ buckets . length ? ': ' + buckets . map ( ( b ) => b . name ) . join ( ', ' ) : '' } `
94- ) ;
95- } catch ( error ) {
96- const message = error instanceof Error ? error . message : 'Unknown error' ;
97- addLog . error ( 'MinIO connection failed:' , {
98- endpoint : this . config . endpoint ,
99- port : this . config . port ,
100- useSSL : this . config . useSSL ,
101- error : message
102- } ) ;
103-
104- if ( error instanceof Error ) {
105- const errorHints = {
106- 'socket connection was closed' : [
107- 'MinIO server running?' ,
108- 'Correct endpoint/port?' ,
109- 'Network/firewall OK?' ,
110- `Try: curl http://${ this . config . endpoint } :${ this . config . port } /minio/health/live`
111- ] ,
112- ECONNREFUSED : [ 'MinIO server not running on specified port' ] ,
113- ENOTFOUND : [ 'DNS resolution failed - check endpoint' ]
114- } ;
115-
116- for ( const [ key , hints ] of Object . entries ( errorHints ) ) {
117- if ( message . includes ( key ) ) {
118- hints . forEach ( ( hint , i ) => addLog . error ( `${ i + 1 } . ${ hint } ` ) ) ;
119- break ;
120- }
121- }
122- }
123-
124- throw new Error ( `MinIO connection failed: ${ message } ` ) ;
125- }
126- }
127-
128- private async initBucket ( ) {
129- try {
130- addLog . info ( `Checking bucket: ${ this . config . bucket } ` ) ;
131- const bucketExists = await this . minioClient . bucketExists ( this . config . bucket ) ;
132- if ( ! bucketExists ) {
133- addLog . info ( `Creating bucket: ${ this . config . bucket } ` ) ;
134- await this . minioClient . makeBucket ( this . config . bucket ) ;
135- }
136- await this . setBucketDownloadOnly ( ) ;
137- } catch ( error ) {
138- addLog . error ( 'Failed to initialize bucket:' , error ) ;
139- if ( error instanceof Error && error . message . includes ( 'Method Not Allowed' ) ) {
140- addLog . warn ( 'Method Not Allowed - bucket may exist with different permissions' ) ;
141- return ;
142- }
143- throw error ;
144- }
145- }
146-
147- private async setBucketDownloadOnly ( ) {
148- try {
149- const accessPolicy = {
150- Version : '2012-10-17' ,
151- Statement : [
152- {
153- Effect : 'Allow' ,
154- Principal : '*' ,
155- Action : [ 's3:GetObject' ] ,
156- Resource : [ `arn:aws:s3:::${ this . config . bucket } /*` ]
157- }
158- ]
159- } ;
160- await this . minioClient . setBucketPolicy ( this . config . bucket , JSON . stringify ( accessPolicy ) ) ;
161- await this . setBucketLifecycle ( ) ;
162- addLog . info ( `Bucket ${ this . config . bucket } policies set successfully` ) ;
163- } catch ( error ) {
164- addLog . warn (
165- `Failed to set bucket policies: ${ error instanceof Error ? error . message : String ( error ) } `
166- ) ;
167- }
168- }
169-
170- private async setBucketLifecycle ( ) {
171- try {
172- const lifecycleConfig : Minio . LifecycleConfig = {
173- Rule : [
174- {
175- ID : 'AutoDeleteRule' ,
176- Status : 'Enabled' ,
177- Expiration : {
178- Days : this . config . retentionDays ,
179- DeleteMarker : false ,
180- DeleteAll : false
181- }
182- }
183- ]
184- } ;
185- await this . minioClient . setBucketLifecycle ( this . config . bucket , lifecycleConfig ) ;
186- addLog . info ( `Lifecycle policy set: ${ this . config . retentionDays } days retention` ) ;
187- } catch ( error ) {
188- addLog . warn (
189- `Failed to set lifecycle policy: ${ error instanceof Error ? error . message : String ( error ) } `
190- ) ;
191- }
192- }
193-
19462 // ================================
19563 // 2.3 文件处理工具方法
19664 // ================================
@@ -239,8 +107,6 @@ export class FileService {
239107 // ================================
240108
241109 async uploadFile ( fileBuffer : Buffer , originalFilename : string ) : Promise < FileMetadata > {
242- await this . ensureInitialized ( ) ;
243-
244110 if ( fileBuffer . length > this . config . maxFileSize ) {
245111 throw new Error ( `File size ${ fileBuffer . length } exceeds limit ${ this . config . maxFileSize } ` ) ;
246112 }
@@ -368,20 +234,6 @@ export class FileService {
368234 addLog . info ( `Processing buffer file: ${ input . filename } ` ) ;
369235 return { buffer : input . buffer ! , filename : input . filename ! } ;
370236 }
371-
372- // ================================
373- // 2.6 静态方法和实例管理
374- // ================================
375-
376- static createForWorker ( config ?: Partial < FileConfig > ) : FileService {
377- return new FileService ( config ) ;
378- }
379- static getDefaultConfig ( ) : FileConfig {
380- return { ...defaultFileConfig } ;
381- }
382- getConfig ( ) : FileConfig {
383- return { ...this . config } ;
384- }
385237}
386238
387239// ================================
0 commit comments