From eb357562fdd5a3cb10fe50f5a818df9cbe81efc5 Mon Sep 17 00:00:00 2001 From: Shibly Teknologi Solusi Date: Sat, 23 May 2026 01:35:10 +0700 Subject: [PATCH] update --- src/controllers/user.ts | 29 ++++++++++++++++------------- src/routes/public.ts | 1 + 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/src/controllers/user.ts b/src/controllers/user.ts index 7a538b1..8aa76e1 100644 --- a/src/controllers/user.ts +++ b/src/controllers/user.ts @@ -14,14 +14,17 @@ import path from "path"; import fileUpload from "express-fileupload"; import { v4 as uuidv4 } from "uuid"; import config from "../../config/stag.json"; -import { profile } from "console"; - const log: Logger = new Logger({ name: "[UserController]", type: "pretty", }); const STORAGE_DIR = "assets/saude/"; +const buildFileUrl = (fileName: string): string => { + const base = config.server.host_swagger.endsWith("/") ? config.server.host_swagger : `${config.server.host_swagger}/`; + return `${base}${STORAGE_DIR}${fileName}`; +}; + export class UserController { static async list(req: Request, res: Response, next: NextFunction): Promise { /* @@ -138,7 +141,7 @@ export class UserController { const mapped_data = list_data.map((user) => ({ ...user, profile_picture: user.profile_picture ?? null, - file_profile_picture: user.profile_picture ? `${config.server.host_swagger}uploads/doctor-profile/${user.profile_picture}` : null, + file_profile_picture: user.profile_picture ? buildFileUrl(user.profile_picture) : null, })); return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, mapped_data); @@ -247,7 +250,7 @@ export class UserController { const mapped_data = list_data.map((user) => ({ ...user, profile_picture: user.profile_picture ?? null, - file_profile_picture: user.profile_picture ? `${config.server.host_swagger}uploads/doctor-profile/${user.profile_picture}` : null, + file_profile_picture: user.profile_picture ? buildFileUrl(user.profile_picture) : null, })); return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, mapped_data); @@ -356,9 +359,9 @@ export class UserController { const mapped_data = list_data.map((user) => ({ ...user, profile_picture: user.profile_picture ?? null, - file_profile_picture: user.profile_picture ? `${config.server.host_swagger}uploads/doctor-profile/${user.profile_picture}` : null, + file_profile_picture: user.profile_picture ? buildFileUrl(user.profile_picture) : null, signature: user.signature ?? null, - file_signature: user.signature ? `${config.server.host_swagger}uploads/user-signature/${user.signature}` : null, + file_signature: user.signature ? buildFileUrl(user.signature) : null, })); return ReturnHelper.successResponselist(res, 200, Language.lang.success_view, count_data, current_page, total_count_data, mapped_data); @@ -782,7 +785,7 @@ export class UserController { } } - static async uploadFile(files: fileUpload.FileArray | null | undefined, fieldName: string, folderPath: string): Promise { + static async uploadFile(files: fileUpload.FileArray | null | undefined, fieldName: string): Promise { if (!files || !files[fieldName]) return null; const file = files[fieldName] as fileUpload.UploadedFile; @@ -792,10 +795,10 @@ export class UserController { throw { message: `${fieldName}: File size exceeds 1MB limit` }; } - const allowedMimes = ["image/jpeg", "image/jpg", "image/png", "image/webp"]; + const allowedMimes = ["image/jpeg", "image/jpg", "image/png", "image/webp", "image/svg+xml", "image/svg"]; if (!allowedMimes.includes(file.mimetype)) { throw { - message: `${fieldName}: Invalid file type. Only JPG, PNG, and WEBP are allowed`, + message: `${fieldName}: Invalid file type. Only JPG, PNG, WEBP, and SVG are allowed`, }; } @@ -879,7 +882,7 @@ export class UserController { const data = await userRepository.findOneBy({ id: param.id }); if (data != null) { - const doctorprofile = await UserController.uploadFile(req.files, "", STORAGE_DIR); + const doctorprofile = await UserController.uploadFile(req.files, ""); if (doctorprofile) { if (data.profile_picture) { const oldFilePath = path.join(STORAGE_DIR, data.profile_picture); @@ -973,7 +976,7 @@ export class UserController { const data = await userRepository.findOneBy({ id: param.id }); if (data != null) { - const usersignature = await UserController.uploadFile(req.files, "user_signature", STORAGE_DIR); + const usersignature = await UserController.uploadFile(req.files, "user_signature"); if (usersignature) { // ← Hapus signature lama jika ada @@ -1097,9 +1100,9 @@ export class UserController { const data = { ...detail, signature: detail.signature ?? null, - file_signature: detail.signature ? `${config.server.host_swagger}uploads/user-signature/${detail.signature}` : null, + file_signature: detail.signature ? buildFileUrl(detail.signature) : null, profile: detail.profile_picture ?? null, - profile_picture: detail.profile_picture ? `${config.server.host_swagger}uploads/doctor-profile/${detail.profile_picture}` : null, + profile_picture: detail.profile_picture ? buildFileUrl(detail.profile_picture) : null, }; return ReturnHelper.successResponseAny(res, 200, Language.lang.success_view, data); diff --git a/src/routes/public.ts b/src/routes/public.ts index 9a8c087..8f95235 100644 --- a/src/routes/public.ts +++ b/src/routes/public.ts @@ -16,6 +16,7 @@ export class RoutePublic { app.put('/api/logout/:refresh_token', AuthController.logout) app.put('/api/renew_token/:refresh_token/:application', AuthController.renewToken) app.use('/static', express.static(path.join(__dirname, 'assets'))); + app.use('/assets', express.static(path.join(process.cwd(), 'assets'))); } } \ No newline at end of file