implement user pages + ui
All checks were successful
ci/woodpecker/push/build Pipeline was successful
All checks were successful
ci/woodpecker/push/build Pipeline was successful
This commit is contained in:
parent
67c31725c1
commit
3fabc91438
11 changed files with 1021 additions and 264 deletions
63
src/routes/push.rs
Normal file
63
src/routes/push.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
use based::request::api::{FallibleApiResponse, api_error};
|
||||
use rocket::tokio::io::AsyncReadExt;
|
||||
use rocket::{FromForm, post};
|
||||
use serde_json::json;
|
||||
|
||||
use pacco::pkg::Package;
|
||||
use pacco::pkg::arch::Architecture;
|
||||
|
||||
// /pkg/<repo>/<arch>/<pkg_name>
|
||||
// /pkg/<repo>/<arch>/
|
||||
|
||||
use rocket::form::Form;
|
||||
use rocket::fs::TempFile;
|
||||
|
||||
#[derive(FromForm)]
|
||||
pub struct PkgUpload<'r> {
|
||||
name: String,
|
||||
arch: String,
|
||||
version: String,
|
||||
pkg: TempFile<'r>,
|
||||
sig: Option<TempFile<'r>>,
|
||||
}
|
||||
|
||||
pub async fn tmp_file_to_vec<'r>(tmp: &TempFile<'r>) -> Vec<u8> {
|
||||
let mut buf = Vec::with_capacity(tmp.len() as usize);
|
||||
tmp.open()
|
||||
.await
|
||||
.unwrap()
|
||||
.read_to_end(&mut buf)
|
||||
.await
|
||||
.unwrap();
|
||||
buf
|
||||
}
|
||||
|
||||
#[post("/pkg/<repo>/upload", data = "<upload>")]
|
||||
pub async fn upload_pkg(
|
||||
repo: &str,
|
||||
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,
|
||||
Architecture::parse(&upload.arch).ok_or_else(|| api_error("Invalid architecture"))?,
|
||||
&upload.name,
|
||||
&upload.version,
|
||||
);
|
||||
|
||||
pkg.save(
|
||||
tmp_file_to_vec(&upload.pkg).await,
|
||||
if let Some(sig) = &upload.sig {
|
||||
Some(tmp_file_to_vec(sig).await)
|
||||
} else {
|
||||
None
|
||||
},
|
||||
);
|
||||
|
||||
Ok(json!({"ok": format!("Added '{}' to '{}'", pkg.file_name(), repo)}))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue