use ::session::AuthedUser; use actix_web::http::header::CONTENT_TYPE; use api::http::{Bytes, HeaderMap, State}; use api::rest::{AppResponseData, NoData}; use crate::errors::ApiError; use crate::services::avatar::AvatarDto; use crate::services::service::UserService; #[utoipa::path( put, path = "/me/avatar", tag = "user", description = "上传原始图片字节作为当前用户头像。", request_body( content = Vec, description = "raw image bytes (image/jpeg, image/png, image/webp, image/gif)", content_type = "image/png" ), responses( (status = 200, description = "avatar uploaded", body = AppResponseData), (status = 400, description = "bad request", body = AppResponseData), (status = 401, description = "authentication required", body = AppResponseData) ) )] pub async fn upload_avatar( State(service): State, AuthedUser(uid): AuthedUser, headers: HeaderMap, body: Bytes, ) -> Result, ApiError> { let content_type = headers .get(CONTENT_TYPE) .and_then(|v| v.to_str().ok()) .unwrap_or("application/octet-stream") .to_string(); let dto = service .avatars .upload_avatar_stream(uid, &content_type, &body) .await?; Ok(AppResponseData::ok(dto)) }