feat(api): expand API endpoints for repo, PR, user, workspace management
- Add git operation endpoints: archive, compare branches, diff, tree, repository extras - Add repo endpoints: contributors, delete fork, get branch/commit status/deploy key/invitation/member/release/tag/webhook, topics, release assets, webhook deliveries/retry - Add PR endpoints: review requests, templates - Add user endpoints: block/unblock, follow/unfollow, presence, personal access tokens, account restore - Add workspace endpoints: billing history, approvals, domains, integrations, invitations, members, webhooks, restore - Add internal API, notification API, IM API modules - Update route configuration and OpenAPI spec
This commit is contained in:
+43
-14
@@ -1,20 +1,46 @@
|
||||
use actix_multipart::Multipart;
|
||||
use actix_web::{HttpResponse, web};
|
||||
use futures_util::StreamExt;
|
||||
use serde::Serialize;
|
||||
|
||||
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;
|
||||
|
||||
#[derive(Serialize, utoipa::ToSchema)]
|
||||
pub struct AvatarData {
|
||||
pub avatar_url: String,
|
||||
pub storage_key: String,
|
||||
}
|
||||
|
||||
pub async fn parse_avatar_field(
|
||||
mut payload: Multipart,
|
||||
) -> Result<(Vec<u8>, Option<String>, Option<String>), AppError> {
|
||||
while let Some(Ok(mut field)) = payload.next().await {
|
||||
if field.name() == Some("avatar") {
|
||||
let content_type = field.content_type().map(|m| m.to_string());
|
||||
let file_name = field
|
||||
.content_disposition()
|
||||
.and_then(|cd| cd.get_filename().map(|s| s.to_string()));
|
||||
let mut data: Vec<u8> = Vec::new();
|
||||
while let Some(Ok(chunk)) = field.next().await {
|
||||
data.extend_from_slice(&chunk);
|
||||
}
|
||||
return Ok((data, content_type, file_name));
|
||||
}
|
||||
}
|
||||
Err(AppError::BadRequest(
|
||||
"missing 'avatar' field in multipart form".into(),
|
||||
))
|
||||
}
|
||||
|
||||
/// Upload user avatar
|
||||
///
|
||||
/// Uploads a new avatar image for the authenticated user.
|
||||
/// Requires authentication.
|
||||
/// Requires authentication. Accepts multipart/form-data with a single "avatar" field.
|
||||
///
|
||||
/// 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)
|
||||
/// Supported formats: PNG, JPEG, WebP, GIF (max 5MB).
|
||||
///
|
||||
/// Effects:
|
||||
/// - Avatar image is stored in S3-compatible object storage
|
||||
@@ -28,12 +54,11 @@ use crate::session::Session;
|
||||
tag = "User",
|
||||
operation_id = "userUploadAvatar",
|
||||
request_body(
|
||||
content = UploadUserAvatarParams,
|
||||
description = "Avatar upload parameters",
|
||||
content_type = "application/json"
|
||||
content_type = "multipart/form-data",
|
||||
description = "Avatar image file in a multipart form field named 'avatar'."
|
||||
),
|
||||
responses(
|
||||
(status = 200, description = "Avatar uploaded successfully. Returns the new avatar URL and storage key.", body = ApiResponse<UserAvatarResponse>),
|
||||
(status = 200, description = "Avatar uploaded successfully", body = ApiResponse<AvatarData>),
|
||||
(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),
|
||||
@@ -46,11 +71,15 @@ use crate::session::Session;
|
||||
pub async fn upload_avatar(
|
||||
service: web::Data<AppService>,
|
||||
session: Session,
|
||||
params: web::Json<UploadUserAvatarParams>,
|
||||
payload: Multipart,
|
||||
) -> Result<HttpResponse, AppError> {
|
||||
let response = service
|
||||
let (data, content_type, file_name) = parse_avatar_field(payload).await?;
|
||||
let (avatar_url, storage_key) = service
|
||||
.user
|
||||
.user_upload_avatar(&session, params.into_inner())
|
||||
.user_upload_avatar(&session, data, content_type, file_name)
|
||||
.await?;
|
||||
Ok(HttpResponse::Ok().json(ApiResponse::new(response)))
|
||||
Ok(HttpResponse::Ok().json(ApiResponse::new(AvatarData {
|
||||
avatar_url,
|
||||
storage_key,
|
||||
})))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user