fix: cannot get file at root
This commit is contained in:
parent
59d0dd7ab4
commit
2dd165b451
1 changed files with 53 additions and 45 deletions
98
src/app.rs
98
src/app.rs
|
|
@ -41,59 +41,67 @@ async fn checkout_handler(State(state): State<AppState>, Query(param): Query<Che
|
|||
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 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))
|
||||
);
|
||||
}
|
||||
};
|
||||
let legit_path = param.path.as_str();
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let error_log = "requested path is unexpected";
|
||||
// match param.path.as_str() {
|
||||
// legit_path if param.path.contains("/") || state.check_country_existed(param.path.as_str()) || param.path.is_empty() => {
|
||||
|
||||
// }
|
||||
// _ => {
|
||||
// let error_log = "requested path is unexpected";
|
||||
// error!("{error_log}");
|
||||
// response.insert("error".to_string(), json!(error_log));
|
||||
// }
|
||||
// }
|
||||
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 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))
|
||||
)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue