@@ -4,7 +4,7 @@ import { ForbiddenError, NotFoundError } from "@/errors/http-errors.js";
44import { canUpdateUser } from "./user.policy.js" ;
55import { AuthUser } from "@/@types/auth.js" ;
66import { cloudinary } from "@/config/cloudinary.js" ;
7- import crypto from "node:crypto " ;
7+ import { buildAvatarUrl } from "./user.utils.js " ;
88
99export async function updateMe (
1010 user : AuthUser ,
@@ -32,32 +32,51 @@ export async function adminUpdateUser(
3232 return user ;
3333}
3434
35- export async function updateAvatar ( userId : string , avatarUrl : string ) {
35+ export async function updateAvatar ( userId : string , publicId : string ) {
36+ const expectedPublicId = `blog/avatars/avatar_${ userId } ` ;
37+ if ( publicId !== expectedPublicId ) {
38+ throw new ForbiddenError ( "Invalid avatar reference" ) ;
39+ }
40+
41+ let resource ;
42+ try {
43+ resource = await cloudinary . api . resource ( publicId , {
44+ resource_type : "image" ,
45+ } ) ;
46+ } catch {
47+ throw new NotFoundError ( "Uploaded avatar not found" ) ;
48+ }
49+
50+ const avatarUrl = buildAvatarUrl ( resource , 256 ) ;
3651 const [ user ] = await UserRepo . updateUser ( userId , { avatarUrl } ) ;
3752 return user ;
3853}
3954
4055export function getAvatarUploadSignature ( userId : string ) {
56+ const { cloud_name, api_key, api_secret } = cloudinary . config ( ) ;
57+
4158 const timestamp = Math . floor ( Date . now ( ) / 1000 ) ;
59+ const publicId = `avatar_${ userId } ` ;
4260
43- const publicId = `avatars/${ userId } -${ crypto . randomUUID ( ) } ` ;
61+ const paramsToSign = {
62+ timestamp,
63+ folder : "blog/avatars" ,
64+ public_id : publicId ,
65+ } ;
4466
4567 const signature = cloudinary . utils . api_sign_request (
46- {
47- timestamp,
48- public_id : publicId ,
49- folder : "avatars" ,
50- } ,
51- cloudinary . config ( ) . api_secret ! ,
68+ paramsToSign ,
69+ api_secret ! ,
5270 ) ;
5371
5472 return {
55- cloudName : cloudinary . config ( ) . cloud_name ,
56- apiKey : cloudinary . config ( ) . api_key ,
57- timestamp,
58- signature,
73+ uploadUrl : `https://api.cloudinary.com/v1_1/${ cloud_name } /image/upload` ,
74+ fields : {
75+ api_key,
76+ signature,
77+ ...paramsToSign ,
78+ } ,
5979 publicId,
60- uploadPreset : undefined ,
6180 } ;
6281}
6382
0 commit comments