use actix_web::{HttpResponse, web}; use crate::api::response::{ApiErrorResponse, ApiResponse}; use crate::error::AppError; use crate::service::AppService; use crate::service::user::account::{UploadUserAvatarParams, UserAvatarResponse}; use crate::session::Session; /// Upload user avatar /// /// Uploads a new avatar image for the authenticated user. /// Requires authentication. /// /// Parameters: /// - data: Raw avatar image bytes (PNG, JPEG, or WebP, max 5MB) /// - content_type: MIME type of the image (e.g., "image/png") /// - file_name: Original file name (used to infer file extension) /// /// Effects: /// - Avatar image is stored in S3-compatible object storage /// - Previous avatar is deleted from storage /// - User's avatar URL is updated /// /// Returns the new avatar URL and storage key. #[utoipa::path( post, path = "/api/v1/user/account/avatar", tag = "User", operation_id = "userUploadAvatar", request_body( content = UploadUserAvatarParams, description = "Avatar upload parameters", content_type = "application/json" ), responses( (status = 200, description = "Avatar uploaded successfully. Returns the new avatar URL and storage key.", body = ApiResponse), (status = 400, description = "Invalid parameters: unsupported file type or image too large", body = ApiErrorResponse), (status = 401, description = "Authentication required or session expired", body = ApiErrorResponse), (status = 404, description = "User not found", body = ApiErrorResponse), (status = 500, description = "Internal server error or S3 storage failure", body = ApiErrorResponse), ), security( ("session_cookie" = []) ) )] pub async fn upload_avatar( service: web::Data, session: Session, params: web::Json, ) -> Result { let response = service .user .user_upload_avatar(&session, params.into_inner()) .await?; Ok(HttpResponse::Ok().json(ApiResponse::new(response))) }