diff --git a/src/controllers/02-user-controller.ts b/src/controllers/02-user-controller.ts index a6cbe44..bdadf33 100644 --- a/src/controllers/02-user-controller.ts +++ b/src/controllers/02-user-controller.ts @@ -715,7 +715,7 @@ export class UserController extends Controller { new Promise((resolve, reject) => { const item: string[] = []; - const stream = minio.listObjectsV2(MINIO_BUCKET, `${attachmentLocation(userId)}/`); + const stream = minio.listObjectsV2(MINIO_BUCKET, fileLocation.user.attachment(userId)); stream.on("data", (v) => v && v.name && item.push(v.name)); stream.on("end", () => resolve(item)); @@ -784,10 +784,6 @@ export class UserController extends Controller { } } -function attachmentLocation(uid: string) { - return `user-attachment/${uid}`; -} - @Route("api/v1/user/{userId}/profile-image") @Tags("User") export class UserProfileController extends Controller { @@ -866,22 +862,7 @@ export class UserAttachmentController extends Controller { throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound"); } - const list = await new Promise((resolve, reject) => { - const item: string[] = []; - - const stream = minio.listObjectsV2(MINIO_BUCKET, `${attachmentLocation(userId)}/`); - - stream.on("data", (v) => v && v.name && item.push(v.name)); - stream.on("end", () => resolve(item)); - stream.on("error", () => reject(new Error("MinIO error."))); - }); - - return await Promise.all( - list.map(async (v) => ({ - name: v.split("/").at(-1) as string, - url: await minio.presignedGetObject(MINIO_BUCKET, v, 12 * 60 * 60), - })), - ); + return await listFile(fileLocation.user.attachment(userId)); } @Post() @@ -897,10 +878,10 @@ export class UserAttachmentController extends Controller { return await Promise.all( payload.file.map(async (v) => ({ name: v, - url: await minio.presignedGetObject(MINIO_BUCKET, `${attachmentLocation(userId)}/${v}`), + url: await minio.presignedGetObject(MINIO_BUCKET, fileLocation.user.attachment(userId, v)), uploadUrl: await minio.presignedPutObject( MINIO_BUCKET, - `${attachmentLocation(userId)}/${v}`, + fileLocation.user.attachment(userId, v), 12 * 60 * 60, ), })), @@ -911,7 +892,7 @@ export class UserAttachmentController extends Controller { async deleteAttachment(@Path() userId: string, @Body() payload: { file: string[] }) { await Promise.all( payload.file.map(async (v) => { - await minio.removeObject(MINIO_BUCKET, `${attachmentLocation(userId)}/${v}`, { + await minio.removeObject(MINIO_BUCKET, fileLocation.user.attachment(userId, v), { forceDelete: true, }); }),