refactor: simplify avatar deletion logic and improve error handling in user service.

This commit is contained in:
JakkrapartXD 2026-01-28 15:01:48 +07:00
parent 70d2dfa4c7
commit b28dd410e2

View file

@ -207,11 +207,8 @@ export class UserService {
// Delete old avatar if exists
if (user.profile?.avatar_url) {
try {
// Extract file path from URL
const urlParts = user.profile.avatar_url.split('/');
const oldFilePath = urlParts.slice(-2).join('/'); // Get avatars/{userId}/{filename}
await deleteFile(oldFilePath);
logger.info(`Deleted old avatar: ${oldFilePath}`);
await deleteFile(user.profile.avatar_url);
logger.info(`Deleted old avatar: ${user.profile.avatar_url}`);
} catch (error) {
logger.warn(`Failed to delete old avatar: ${error}`);
// Continue with upload even if delete fails
@ -221,6 +218,7 @@ export class UserService {
// Upload to MinIO
await uploadFile(filePath, file.buffer, file.mimetype || 'image/jpeg');
logger.info(`Uploaded avatar: ${filePath}`);
// Update or create profile - store only file path
if (user.profile) {
await prisma.userProfile.update({
@ -272,7 +270,7 @@ export class UserService {
return await getPresignedUrl(avatarPath, 3600); // 1 hour expiry
} catch (error) {
logger.warn(`Failed to generate presigned URL for avatar: ${error}`);
return '';
throw error;
}
}