rieter/server/src/repo/mod.rs

62 lines
1.9 KiB
Rust
Raw Normal View History

mod manager;
2023-07-12 21:51:07 +00:00
mod package;
2023-07-12 09:04:31 +00:00
use axum::extract::{BodyStream, Path, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use axum::routing::{get_service, post};
2023-07-11 11:41:56 +00:00
use axum::Router;
2023-07-12 09:04:31 +00:00
use futures::StreamExt;
use futures::TryFutureExt;
use futures::TryStreamExt;
use libarchive::archive::Entry;
use libarchive::read::Archive;
use libarchive::read::Builder;
use std::io::Read;
2023-07-12 09:04:31 +00:00
use tokio::{fs, io, io::AsyncWriteExt};
2023-07-11 11:41:56 +00:00
use tower_http::services::ServeDir;
2023-07-12 09:04:31 +00:00
use uuid::Uuid;
2023-07-11 11:41:56 +00:00
pub fn router(config: &crate::Config) -> Router<crate::Config> {
// Try to serve packages by default, and try the database files instead if not found
2023-07-12 09:04:31 +00:00
let serve_repos =
get_service(ServeDir::new(&config.pkg_dir).fallback(ServeDir::new(&config.repo_dir)));
2023-07-11 11:41:56 +00:00
Router::new()
2023-07-12 09:04:31 +00:00
.route(
"/:repo",
post(post_package_archive).get(serve_repos.clone()),
)
.fallback(serve_repos)
2023-07-11 11:41:56 +00:00
.with_state(config.clone())
}
2023-07-12 09:04:31 +00:00
async fn post_package_archive(
State(config): State<crate::Config>,
Path(repo): Path<String>,
body: BodyStream,
) -> Result<(), StatusCode> {
// let mut body_reader = tokio_util::io::StreamReader::new(
// body.map_err(|err| io::Error::new(io::ErrorKind::Other, err)),
// );
let mut body = body.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR);
// We first stream the uploaded file to disk
let uuid: uuid::fmt::Simple = Uuid::new_v4().into();
let path = config.pkg_dir.join(uuid.to_string());
let mut f = fs::File::create(&path)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
.await?;
while let Some(chunk) = body.next().await {
f.write_all(&chunk?)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)
.await?;
}
2023-07-13 09:31:31 +00:00
let pkg = tokio::task::spawn_blocking(move || package::Package::open(&path)).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR).await?;
2023-07-13 09:31:31 +00:00
println!("{:?}", pkg);
2023-07-12 09:04:31 +00:00
Ok(())
}