refactor: simplify avatar deletion logic and improve error handling in user service.
This commit is contained in:
parent
70d2dfa4c7
commit
b28dd410e2
1 changed files with 4 additions and 6 deletions
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue