Skip to content
Clone
main
42 lines 1.4 KB Rust
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<u8>,
description = "raw image bytes (image/jpeg, image/png, image/webp, image/gif)",
content_type = "image/png"
),
responses(
(status = 200, description = "avatar uploaded", body = AppResponseData<AvatarDto>),
(status = 400, description = "bad request", body = AppResponseData<NoData>),
(status = 401, description = "authentication required", body = AppResponseData<NoData>)
)
)]
pub async fn upload_avatar(
State(service): State<UserService>,
AuthedUser(uid): AuthedUser,
headers: HeaderMap,
body: Bytes,
) -> Result<AppResponseData<AvatarDto>, 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))
}