Skip to content

Commit 36b8242

Browse files
committed
refactor(minio): update minio service
1 parent 614c1e6 commit 36b8242

File tree

1 file changed

+46
-47
lines changed

1 file changed

+46
-47
lines changed

src/minio/minio.service.ts

Lines changed: 46 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,54 @@ export class MinioService implements OnModuleInit {
7777
this.bucket = s3Bucket;
7878
}
7979

80-
async putObject(objectName: string, buffer: Buffer, mimetype?: string) {
80+
async onModuleInit(): Promise<void> {
81+
await this.ensureBucket();
82+
}
83+
84+
private generateId(filename: string, length: number = 32): string {
85+
const uuid = generateId(length);
86+
// Get the original filename to extract the proper extension
87+
const originalFilename = getOriginalFileName(filename);
88+
const extIndex = originalFilename.lastIndexOf('.');
89+
if (extIndex === -1) {
90+
return uuid;
91+
}
92+
const ext: string = originalFilename.substring(
93+
extIndex,
94+
originalFilename.length,
95+
);
96+
return `${uuid}${ext}`;
97+
}
98+
99+
private async ensureBucket(): Promise<void> {
100+
try {
101+
await this.s3Client.send(new HeadBucketCommand({ Bucket: this.bucket }));
102+
} catch (error: any) {
103+
if (
104+
error.name === 'NotFound' ||
105+
error.$metadata?.httpStatusCode === 404
106+
) {
107+
await this.s3Client.send(
108+
new CreateBucketCommand({ Bucket: this.bucket }),
109+
);
110+
} else {
111+
throw error;
112+
}
113+
}
114+
}
115+
116+
async putObject(
117+
objectName: string,
118+
buffer: Buffer,
119+
mimetype?: string,
120+
metadata?: Record<string, string>,
121+
) {
81122
const command = new PutObjectCommand({
82123
Bucket: this.bucket,
83124
Key: objectName,
84125
Body: buffer,
85126
ContentType: mimetype,
127+
Metadata: metadata,
86128
});
87129
return await this.s3Client.send(command);
88130
}
@@ -104,21 +146,6 @@ export class MinioService implements OnModuleInit {
104146
return await this.s3Client.send(command);
105147
}
106148

107-
generateId(filename: string, length: number = 32): string {
108-
const uuid = generateId(length);
109-
// Get the original filename to extract the proper extension
110-
const originalFilename = getOriginalFileName(filename);
111-
const extIndex = originalFilename.lastIndexOf('.');
112-
if (extIndex === -1) {
113-
return uuid;
114-
}
115-
const ext: string = originalFilename.substring(
116-
extIndex,
117-
originalFilename.length,
118-
);
119-
return `${uuid}${ext}`;
120-
}
121-
122149
async put(
123150
filename: string,
124151
buffer: Buffer,
@@ -127,17 +154,10 @@ export class MinioService implements OnModuleInit {
127154
): Promise<string> {
128155
const { id = this.generateId(filename), metadata = {} } = options || {};
129156
const path: string = options?.folder ? `${options.folder}/${id}` : id;
130-
const command = new PutObjectCommand({
131-
Bucket: this.bucket,
132-
Key: path,
133-
Body: buffer,
134-
ContentType: mimetype,
135-
Metadata: {
136-
filename: encodeFileName(filename),
137-
metadata: JSON.stringify(metadata),
138-
},
157+
await this.putObject(path, buffer, mimetype, {
158+
filename: encodeFileName(filename),
159+
metadata: JSON.stringify(metadata),
139160
});
140-
await this.s3Client.send(command);
141161
return id;
142162
}
143163

@@ -167,25 +187,4 @@ export class MinioService implements OnModuleInit {
167187
]);
168188
return { stream, ...info } as GetResponse;
169189
}
170-
171-
async onModuleInit(): Promise<void> {
172-
await this.ensureBucket();
173-
}
174-
175-
private async ensureBucket(): Promise<void> {
176-
try {
177-
await this.s3Client.send(new HeadBucketCommand({ Bucket: this.bucket }));
178-
} catch (error: any) {
179-
if (
180-
error.name === 'NotFound' ||
181-
error.$metadata?.httpStatusCode === 404
182-
) {
183-
await this.s3Client.send(
184-
new CreateBucketCommand({ Bucket: this.bucket }),
185-
);
186-
} else {
187-
throw error;
188-
}
189-
}
190-
}
191190
}

0 commit comments

Comments
 (0)