feat(server): route for removing packages

main
Jef Roosens 2023-07-16 20:22:58 +02:00
parent bcab4948e3
commit 44f27cf7d0
2 changed files with 38 additions and 1 deletions

View File

@ -131,7 +131,7 @@ impl RepoGroupManager {
.join(format!("{}-{}", pkg.info.name, pkg.info.version));
// We first remove the previous version of the package, if present
self.remove_pkg_from_arch_repo(repo, arch, &pkg.info.name)?;
self.remove_pkg_from_arch_repo(repo, arch, &pkg.info.name, false)?;
fs::create_dir_all(&pkg_dir)?;
@ -176,6 +176,7 @@ impl RepoGroupManager {
repo: &str,
arch: &str,
pkg_name: &str,
sync: bool,
) -> io::Result<bool> {
let arch_repo_dir = self.repo_dir.join(repo).join(arch);
@ -222,6 +223,10 @@ impl RepoGroupManager {
})
})?;
if sync {
self.sync(repo, arch)?;
}
return Ok(true);
}
}

View File

@ -26,6 +26,7 @@ pub fn router(global: &crate::Global) -> Router<crate::Global> {
.get(serve_repos.clone()),
)
.route("/:repo/:arch", delete(delete_arch_repo))
.route("/:repo/:arch/:filename", delete(delete_package))
.fallback(serve_repos)
.with_state(global.clone())
}
@ -83,3 +84,34 @@ async fn delete_arch_repo(
Ok(StatusCode::NOT_FOUND)
}
}
async fn delete_package(
State(global): State<crate::Global>,
Path((repo, arch, file_name)): Path<(String, String, String)>,
) -> crate::Result<StatusCode> {
let name_parts = file_name.split('-').collect::<Vec<_>>();
// Package archive files use the naming scheme pkgname-pkgver-pkgrel-arch, so a valid
// name contains at least 4 dash-separated sections
if name_parts.len() < 4 {
return Ok(StatusCode::NOT_FOUND);
}
let name = name_parts[..name_parts.len() - 3].join("-");
let clone = Arc::clone(&global.repo_manager);
let pkg_removed = tokio::task::spawn_blocking(move || {
clone
.write()
.unwrap()
.remove_pkg_from_arch_repo(&repo, &arch, &name, true)
})
.await??;
if pkg_removed {
Ok(StatusCode::OK)
} else {
Ok(StatusCode::NOT_FOUND)
}
}