Skip to content
Clone
main
71 lines 2.2 KB Rust
use ::session::AuthedUser;
use api::http::{Json, Path, State};
use api::rest::AppResponseData;
use crate::errors::{ApiError, GitApiError};
use crate::handler::{RepoPath, require_writer};
use crate::service::GitService;
use crate::service::archives::{ArchiveOptionsDto, ArchiveResultDto};
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Deserialize, utoipa::ToSchema)]
#[serde(rename_all = "snake_case")]
pub enum ArchiveFormatDto {
Tar,
TarGz,
TarBz2,
Zip,
}
impl From<ArchiveFormatDto> for crate::rpcgit::archive::types::ArchiveFormat {
fn from(v: ArchiveFormatDto) -> Self {
match v {
ArchiveFormatDto::Tar => crate::rpcgit::archive::types::ArchiveFormat::Tar,
ArchiveFormatDto::TarGz => crate::rpcgit::archive::types::ArchiveFormat::TarGz,
ArchiveFormatDto::TarBz2 => crate::rpcgit::archive::types::ArchiveFormat::TarBz2,
ArchiveFormatDto::Zip => crate::rpcgit::archive::types::ArchiveFormat::Zip,
}
}
}
#[derive(Debug, Clone, serde::Deserialize, utoipa::ToSchema)]
pub struct ArchiveCreateCmd {
pub revision: String,
pub format: ArchiveFormatDto,
#[serde(default)]
pub path_filter: Option<String>,
}
#[utoipa::path(
post,
path = "/repos/{space_name}/{repo_name}/archives",
tag = "archives",
description = "为指定修订生成归档并上传存储,返回下载地址。",
params(RepoPath),
request_body = ArchiveCreateCmd,
responses(
(status = 200, description = "build archive and return download link", body = AppResponseData<ArchiveResultDto>)
)
)]
pub async fn create_archive(
State(service): State<GitService>,
AuthedUser(uid): AuthedUser,
Path(path): Path<RepoPath>,
Json(cmd): Json<ArchiveCreateCmd>,
) -> Result<AppResponseData<ArchiveResultDto>, ApiError> {
let (ctx, _) = require_writer(
&service,
uid,
path.space_name.clone(),
path.repo_name.clone(),
)
.await?;
let opts = ArchiveOptionsDto {
format: cmd.format.into(),
path_filter: cmd.path_filter,
};
let out = service
.archive_create(&ctx, cmd.revision, opts)
.await
.map_err(GitApiError::from)?;
Ok(AppResponseData::ok(out))
}