feat: multiple profile imagea
This commit is contained in:
parent
1581626880
commit
93840f4c8c
2 changed files with 87 additions and 31 deletions
|
|
@ -29,6 +29,7 @@ import {
|
||||||
removeUserRoles,
|
removeUserRoles,
|
||||||
} from "../services/keycloak";
|
} from "../services/keycloak";
|
||||||
import { isSystem } from "../utils/keycloak";
|
import { isSystem } from "../utils/keycloak";
|
||||||
|
import { fileLocation, listFile } from "../utils/minio";
|
||||||
|
|
||||||
if (!process.env.MINIO_BUCKET) {
|
if (!process.env.MINIO_BUCKET) {
|
||||||
throw Error("Require MinIO bucket.");
|
throw Error("Require MinIO bucket.");
|
||||||
|
|
@ -316,13 +317,7 @@ export class UserController extends Controller {
|
||||||
|
|
||||||
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
|
if (!record) throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
|
||||||
|
|
||||||
return Object.assign(record, {
|
return record;
|
||||||
profileImageUrl: await minio.presignedGetObject(
|
|
||||||
MINIO_BUCKET,
|
|
||||||
imageLocation(record.id),
|
|
||||||
60 * 60,
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Post()
|
@Post()
|
||||||
|
|
@ -463,18 +458,7 @@ export class UserController extends Controller {
|
||||||
|
|
||||||
this.setStatus(HttpStatus.CREATED);
|
this.setStatus(HttpStatus.CREATED);
|
||||||
|
|
||||||
return Object.assign(record, {
|
return record;
|
||||||
profileImageUrl: await minio.presignedPutObject(
|
|
||||||
MINIO_BUCKET,
|
|
||||||
imageLocation(record.id),
|
|
||||||
12 * 60 * 60,
|
|
||||||
),
|
|
||||||
profileImageUploadUrl: await minio.presignedPutObject(
|
|
||||||
MINIO_BUCKET,
|
|
||||||
imageLocation(record.id),
|
|
||||||
12 * 60 * 60,
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Put("{userId}")
|
@Put("{userId}")
|
||||||
|
|
@ -654,18 +638,7 @@ export class UserController extends Controller {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.assign(record, {
|
return record;
|
||||||
profileImageUrl: await minio.presignedGetObject(
|
|
||||||
MINIO_BUCKET,
|
|
||||||
imageLocation(record.id),
|
|
||||||
12 * 60 * 60,
|
|
||||||
),
|
|
||||||
profileImageUploadUrl: await minio.presignedPutObject(
|
|
||||||
MINIO_BUCKET,
|
|
||||||
imageLocation(record.id),
|
|
||||||
12 * 60 * 60,
|
|
||||||
),
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Delete("{userId}")
|
@Delete("{userId}")
|
||||||
|
|
@ -815,6 +788,70 @@ function attachmentLocation(uid: string) {
|
||||||
return `user-attachment/${uid}`;
|
return `user-attachment/${uid}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Route("api/v1/user/{userId}/profile-image")
|
||||||
|
@Tags("User")
|
||||||
|
export class UserProfileController extends Controller {
|
||||||
|
@Get()
|
||||||
|
@Security("keycloak")
|
||||||
|
async listImage(@Path() userId: string) {
|
||||||
|
const record = await prisma.user.findFirst({
|
||||||
|
where: { id: userId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!record) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
|
||||||
|
}
|
||||||
|
|
||||||
|
return await listFile(fileLocation.user.profile(userId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Get("{name}")
|
||||||
|
async getImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
|
||||||
|
const record = await prisma.user.findFirst({
|
||||||
|
where: { id: userId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!record) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
|
||||||
|
}
|
||||||
|
|
||||||
|
return req.res?.redirect(
|
||||||
|
await minio.presignedGetObject(
|
||||||
|
MINIO_BUCKET,
|
||||||
|
fileLocation.user.profile(userId, name),
|
||||||
|
12 * 60 * 60,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Put("{name}")
|
||||||
|
@Security("keycloak")
|
||||||
|
async putImage(@Request() req: RequestWithUser, @Path() userId: string, @Path() name: string) {
|
||||||
|
const record = await prisma.user.findFirst({
|
||||||
|
where: { id: userId },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!record) {
|
||||||
|
throw new HttpError(HttpStatus.NOT_FOUND, "User cannot be found.", "userNotFound");
|
||||||
|
}
|
||||||
|
|
||||||
|
return req.res?.redirect(
|
||||||
|
await minio.presignedPutObject(
|
||||||
|
MINIO_BUCKET,
|
||||||
|
fileLocation.user.profile(userId, name),
|
||||||
|
12 * 60 * 60,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Delete("{name}")
|
||||||
|
async deleteAttachment(@Path() userId: string, @Path() name: string) {
|
||||||
|
await minio.removeObject(MINIO_BUCKET, fileLocation.user.profile(userId, name), {
|
||||||
|
forceDelete: true,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Route("api/v1/user/{userId}/attachment")
|
@Route("api/v1/user/{userId}/attachment")
|
||||||
@Tags("User")
|
@Tags("User")
|
||||||
@Security("keycloak")
|
@Security("keycloak")
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,22 @@ if (!process.env.MINIO_BUCKET) {
|
||||||
|
|
||||||
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
const MINIO_BUCKET = process.env.MINIO_BUCKET;
|
||||||
|
|
||||||
|
export async function listFile(path: string) {
|
||||||
|
return await new Promise<string[]>((resolve, reject) => {
|
||||||
|
const item: string[] = [];
|
||||||
|
|
||||||
|
const stream = minio.listObjectsV2(MINIO_BUCKET, path);
|
||||||
|
|
||||||
|
stream.on("data", (v) => v && v.name && item.push(v.name));
|
||||||
|
stream.on("end", () => resolve(item));
|
||||||
|
stream.on("error", () => reject(new Error("MinIO error.")));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteFile(path: string) {
|
||||||
|
await minio.removeObject(MINIO_BUCKET, path, { forceDelete: true });
|
||||||
|
}
|
||||||
|
|
||||||
export async function deleteFolder(path: string) {
|
export async function deleteFolder(path: string) {
|
||||||
new Promise<string[]>((resolve, reject) => {
|
new Promise<string[]>((resolve, reject) => {
|
||||||
const item: string[] = [];
|
const item: string[] = [];
|
||||||
|
|
@ -29,4 +45,7 @@ export const fileLocation = {
|
||||||
map: (branchId: string) => `branch/map-img-${branchId}`,
|
map: (branchId: string) => `branch/map-img-${branchId}`,
|
||||||
bank: (branchId: string, bankId: string) => `branch/bank-qr-${branchId}-${bankId}`,
|
bank: (branchId: string, bankId: string) => `branch/bank-qr-${branchId}-${bankId}`,
|
||||||
},
|
},
|
||||||
|
user: {
|
||||||
|
profile: (userId: string, name?: string) => `user/profile-image-${userId}/${name || ""}`,
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue