fix: cannot get file at root

This commit is contained in:
Pakin 2026-01-09 11:36:50 +07:00
parent 59d0dd7ab4
commit 2dd165b451

View file

@ -41,59 +41,67 @@ async fn checkout_handler(State(state): State<AppState>, Query(param): Query<Che
Json(json!(response)) Json(json!(response))
); );
} }
match param.path.as_str() {
legit_path if param.path.contains("/") || state.check_country_existed(param.path.as_str()) || param.path.is_empty() => {
let rpath = repo_path.unwrap().clone();
let repo = match Repository::open_bare(rpath) {
Ok(repo) => repo,
Err(_) => {
let error_log = "unable to open repo as bare";
error!("{error_log}");
response.insert("error".to_string(), json!(error_log));
return (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
Json(json!(response))
);
}
};
let fpath = format!("master:{}", legit_path); let legit_path = param.path.as_str();
let obj = match repo.revparse_single(&fpath){
Ok(obj) => obj,
Err(e) => {
let error_log = format!("unexpected revparse single: {err}", err = e.message());
error!("{error_log}");
response.insert("error".to_string(), json!(error_log.clone()));
return (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
Json(json!(response))
);
}
};
if let Some(blob) = obj.as_blob() { // match param.path.as_str() {
let content = unsafe { // legit_path if param.path.contains("/") || state.check_country_existed(param.path.as_str()) || param.path.is_empty() => {
str::from_utf8_unchecked(blob.content())
}; // }
response.insert("result".to_string(), json!(content.to_string())); // _ => {
} else if let Some(tree) = obj.as_tree() { // let error_log = "requested path is unexpected";
let dir_list = tree.iter().map(|x| x.name().unwrap_or("").to_string()).collect::<Vec<String>>(); // error!("{error_log}");
response.insert("result".to_string(), json!(dir_list)); // response.insert("error".to_string(), json!(error_log));
} else { // }
let error_log = "not obj nor tree"; // }
error!("{error_log}"); let rpath = repo_path.unwrap().clone();
response.insert("error".to_string(), json!(error_log)); let repo = match Repository::open_bare(rpath) {
} Ok(repo) => repo,
} Err(_) => {
_ => { let error_log = "unable to open repo as bare";
let error_log = "requested path is unexpected";
error!("{error_log}"); error!("{error_log}");
response.insert("error".to_string(), json!(error_log)); response.insert("error".to_string(), json!(error_log));
return (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
Json(json!(response))
);
} }
};
let fpath = format!("master:{}", legit_path);
let obj = match repo.revparse_single(&fpath){
Ok(obj) => obj,
Err(e) => {
let error_log = format!("unexpected revparse single: {err}", err = e.message());
error!("{error_log}");
response.insert("error".to_string(), json!(error_log.clone()));
return (
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
Json(json!(response))
);
}
};
if let Some(blob) = obj.as_blob() {
let content = unsafe {
str::from_utf8_unchecked(blob.content())
};
response.insert("result".to_string(), json!(content.to_string()));
} else if let Some(tree) = obj.as_tree() {
let dir_list = tree.iter().map(|x| x.name().unwrap_or("").to_string()).collect::<Vec<String>>();
response.insert("result".to_string(), json!(dir_list));
} else {
let error_log = "not obj nor tree";
error!("{error_log}");
response.insert("error".to_string(), json!(error_log));
return (
axum::http::StatusCode::BAD_REQUEST,
Json(json!(response))
)
} }
( (
axum::http::StatusCode::BAD_REQUEST, axum::http::StatusCode::OK,
Json(json!(response)) Json(json!(response))
) )
} }