fix: Proper file extensions inside Minios
This commit is contained in:
parent
188d3e8318
commit
3d467e8219
@ -23,7 +23,18 @@ class MinioClient {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async uploadProfile(stream: Readable) {
|
private getFileExtension(mimetype: string): string {
|
||||||
|
switch (mimetype) {
|
||||||
|
case 'image/jpeg':
|
||||||
|
return '.jpg';
|
||||||
|
case 'image/png':
|
||||||
|
return '.png';
|
||||||
|
default:
|
||||||
|
throw new Error('Unsupported file type');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async uploadProfile(stream: Readable, mime: string) {
|
||||||
try {
|
try {
|
||||||
const bucket = 'profile-photos';
|
const bucket = 'profile-photos';
|
||||||
if (await !this.client.bucketExists(bucket)) {
|
if (await !this.client.bucketExists(bucket)) {
|
||||||
@ -31,7 +42,7 @@ class MinioClient {
|
|||||||
console.log('Bucket "' + bucket + '" created in "us-east-1".');
|
console.log('Bucket "' + bucket + '" created in "us-east-1".');
|
||||||
}
|
}
|
||||||
|
|
||||||
const objectId = v4();
|
const objectId = `${v4()}${this.getFileExtension(mime)}`;
|
||||||
const upload = await this.client.putObject(bucket, objectId, stream);
|
const upload = await this.client.putObject(bucket, objectId, stream);
|
||||||
return {
|
return {
|
||||||
bucket,
|
bucket,
|
||||||
|
@ -23,7 +23,7 @@ export const POST = async ({ request }) => {
|
|||||||
const buffer = await file.arrayBuffer();
|
const buffer = await file.arrayBuffer();
|
||||||
const stream = Readable.from(Buffer.from(buffer));
|
const stream = Readable.from(Buffer.from(buffer));
|
||||||
|
|
||||||
const uploadResponse = await fsClient?.uploadProfile(stream);
|
const uploadResponse = await fsClient?.uploadProfile(stream, file.type);
|
||||||
|
|
||||||
return json(uploadResponse);
|
return json(uploadResponse);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
Loading…
Reference in New Issue
Block a user