feat: add attachment to user controller
This commit is contained in:
parent
8ab2ab156f
commit
d44c259999
1 changed files with 97 additions and 0 deletions
|
|
@ -379,6 +379,22 @@ export class UserController extends Controller {
|
||||||
forceDelete: true,
|
forceDelete: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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.")));
|
||||||
|
}).then((list) => {
|
||||||
|
list.map(async (v) => {
|
||||||
|
await minio.removeObject(MINIO_BUCKET, `${attachmentLocation(userId)}/${v}`, {
|
||||||
|
forceDelete: true,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
return await prisma.user.delete({
|
return await prisma.user.delete({
|
||||||
include: {
|
include: {
|
||||||
province: true,
|
province: true,
|
||||||
|
|
@ -389,3 +405,84 @@ export class UserController extends Controller {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function attachmentLocation(uid: string) {
|
||||||
|
return `user-attachment/${uid}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Route("api/user/{userId}/attachment")
|
||||||
|
@Tags("User")
|
||||||
|
@Security("keycloak")
|
||||||
|
export class UserAttachmentController extends Controller {
|
||||||
|
@Get()
|
||||||
|
async listAttachment(@Path() userId: string) {
|
||||||
|
const record = await prisma.user.findFirst({
|
||||||
|
include: {
|
||||||
|
province: true,
|
||||||
|
district: true,
|
||||||
|
subDistrict: true,
|
||||||
|
},
|
||||||
|
where: { id: userId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!record) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "data_not_found");
|
||||||
|
}
|
||||||
|
|
||||||
|
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),
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post()
|
||||||
|
async addAttachment(@Path() userId: string, @Body() payload: { file: string[] }) {
|
||||||
|
const record = await prisma.user.findFirst({
|
||||||
|
include: {
|
||||||
|
province: true,
|
||||||
|
district: true,
|
||||||
|
subDistrict: true,
|
||||||
|
},
|
||||||
|
where: { id: userId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!record) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "data_not_found");
|
||||||
|
}
|
||||||
|
|
||||||
|
return await Promise.all(
|
||||||
|
payload.file.map(async (v) => ({
|
||||||
|
name: v,
|
||||||
|
url: await minio.presignedGetObject(MINIO_BUCKET, `${attachmentLocation(userId)}/${v}`),
|
||||||
|
uploadUrl: await minio.presignedPutObject(
|
||||||
|
MINIO_BUCKET,
|
||||||
|
`${attachmentLocation(userId)}/${v}`,
|
||||||
|
12 * 60 * 60,
|
||||||
|
),
|
||||||
|
})),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete()
|
||||||
|
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}`, {
|
||||||
|
forceDelete: true,
|
||||||
|
});
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue