refactor: structure

This commit is contained in:
Methapon Metanipat 2024-09-06 13:09:17 +07:00
parent 48254ceec0
commit 81f8aeeb7c

View file

@ -715,7 +715,7 @@ export class UserController extends Controller {
new Promise<string[]>((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<string[]>((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,
});
}),