136 lines
4.7 KiB
Rust
136 lines
4.7 KiB
Rust
use crate::{ApiResponse, error::ApiError};
|
|
use actix_web::{HttpResponse, Result, web};
|
|
use service::AppService;
|
|
use service::git::init::GitInitRequest;
|
|
use session::Session;
|
|
|
|
fn sanitize_repo_path(path: &str) -> Result<String, ApiError> {
|
|
if path.contains("..") || path.contains('~') {
|
|
return Err(ApiError(service::error::AppError::BadRequest(
|
|
"Invalid repository path".to_string()
|
|
)));
|
|
}
|
|
if path.starts_with('/') || path.starts_with('\\') ||
|
|
(path.len() >= 3 && path.as_bytes()[1] == b':' && path.as_bytes()[2] == b'\\') {
|
|
return Err(ApiError(service::error::AppError::BadRequest(
|
|
"Absolute paths are not allowed".to_string()
|
|
)));
|
|
}
|
|
Ok(path.to_string())
|
|
}
|
|
|
|
#[utoipa::path(
|
|
post,
|
|
path = "/api/git/init",
|
|
request_body = GitInitRequest,
|
|
responses(
|
|
(status = 200, description = "Bare repository initialized", body = ApiResponse<service::git::init::GitInitResponse>),
|
|
(status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
|
|
(status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
|
|
),
|
|
tag = "Git"
|
|
)]
|
|
pub async fn git_init_bare(
|
|
service: web::Data<AppService>,
|
|
session: Session,
|
|
body: web::Json<GitInitRequest>,
|
|
) -> Result<HttpResponse, ApiError> {
|
|
let resp = service.git_init_bare(body.into_inner(), &session).await?;
|
|
Ok(ApiResponse::ok(resp).to_response())
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/api/git/open/{path}",
|
|
params(
|
|
("path" = String, Path, description = "Repository path"),
|
|
),
|
|
responses(
|
|
(status = 200, description = "Open repository", body = ApiResponse<service::git::init::GitInitResponse>),
|
|
(status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
|
|
(status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
|
|
),
|
|
tag = "Git"
|
|
)]
|
|
pub async fn git_open(
|
|
service: web::Data<AppService>,
|
|
session: Session,
|
|
path: web::Path<String>,
|
|
) -> Result<HttpResponse, ApiError> {
|
|
let path = sanitize_repo_path(&path.into_inner())?;
|
|
let resp = service.git_open(path, &session).await?;
|
|
Ok(ApiResponse::ok(resp).to_response())
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/api/git/open/{path}/bare",
|
|
params(
|
|
("path" = String, Path, description = "Repository path"),
|
|
),
|
|
responses(
|
|
(status = 200, description = "Open bare repository", body = ApiResponse<service::git::init::GitInitResponse>),
|
|
(status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
|
|
(status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
|
|
),
|
|
tag = "Git"
|
|
)]
|
|
pub async fn git_open_bare(
|
|
service: web::Data<AppService>,
|
|
session: Session,
|
|
path: web::Path<String>,
|
|
) -> Result<HttpResponse, ApiError> {
|
|
let path = sanitize_repo_path(&path.into_inner())?;
|
|
let resp = service.git_open_bare(path, &session).await?;
|
|
Ok(ApiResponse::ok(resp).to_response())
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/api/git/is-repo/{path}",
|
|
params(
|
|
("path" = String, Path, description = "Repository path"),
|
|
),
|
|
responses(
|
|
(status = 200, description = "Check if path is a repository", body = ApiResponse<bool>),
|
|
(status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
|
|
(status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
|
|
),
|
|
tag = "Git"
|
|
)]
|
|
pub async fn git_is_repo(
|
|
service: web::Data<AppService>,
|
|
session: Session,
|
|
path: web::Path<String>,
|
|
) -> Result<HttpResponse, ApiError> {
|
|
let path = sanitize_repo_path(&path.into_inner())?;
|
|
let resp = service.git_is_repo(path, &session).await?;
|
|
Ok(ApiResponse::ok(resp).to_response())
|
|
}
|
|
|
|
#[utoipa::path(
|
|
get,
|
|
path = "/api/repos/{namespace}/{repo}/git/path",
|
|
params(
|
|
("namespace" = String, Path, description = "Repository namespace"),
|
|
("repo" = String, Path, description = "Repository name"),
|
|
),
|
|
responses(
|
|
(status = 200, description = "Repository path", body = ApiResponse<String>),
|
|
(status = 401, description = "Unauthorized", body = ApiResponse<crate::error::ApiError>),
|
|
(status = 404, description = "Not found", body = ApiResponse<crate::error::ApiError>),
|
|
),
|
|
tag = "Git"
|
|
)]
|
|
pub async fn git_repo_path(
|
|
service: web::Data<AppService>,
|
|
session: Session,
|
|
path: web::Path<(String, String)>,
|
|
) -> Result<HttpResponse, ApiError> {
|
|
let (namespace, repo_name) = path.into_inner();
|
|
let resp = service
|
|
.git_repo_path(namespace, repo_name, &session)
|
|
.await?;
|
|
Ok(HttpResponse::Ok().json(serde_json::json!({ "path": resp })))
|
|
}
|