refactor
All checks were successful
ci/woodpecker/push/build Pipeline was successful

This commit is contained in:
JMARyA 2024-12-29 09:09:54 +01:00
parent 376e4f4eb8
commit 6eb3678c1c
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
7 changed files with 252 additions and 134 deletions

View file

@ -5,6 +5,7 @@ use rocket::tokio::io::AsyncReadExt;
use rocket::{FromForm, get, post};
use serde_json::json;
use crate::pkg::arch::Architecture;
use crate::pkg::{Package, Repository};
// /pkg/<repo>/<arch>/<pkg_name>
@ -39,11 +40,17 @@ pub async fn upload_pkg(
upload: Form<PkgUpload<'_>>,
user: based::auth::APIUser,
) -> FallibleApiResponse {
// TODO : Permission System
if !user.0.is_admin() {
return Err(api_error("Forbidden"));
}
let pkg = Package::new(repo, &upload.arch, &upload.name, &upload.version);
let pkg = Package::new(
repo,
Architecture::parse(&upload.arch).ok_or_else(|| api_error("Invalid architecture"))?,
&upload.name,
&upload.version,
);
pkg.save(
tmp_file_to_vec(&upload.pkg).await,
@ -54,11 +61,13 @@ pub async fn upload_pkg(
},
);
Ok(json!({"ok": 1}))
Ok(json!({"ok": format!("Added '{}' to '{}'", pkg.file_name(), repo)}))
}
#[get("/pkg/<repo>/<arch>/<pkg_name>")]
pub async fn pkg_route(repo: &str, arch: &str, pkg_name: &str, ctx: RequestContext) -> RawResponse {
let arch = Architecture::parse(arch).unwrap();
if let Some(repo) = Repository::new(repo) {
if pkg_name.ends_with("db.tar.gz")
|| pkg_name.ends_with("db")