rieter/server/src/repo/mod.rs

79 lines
2.4 KiB
Rust
Raw Normal View History

mod manager;
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?;
}
let mut builder = Builder::new();
builder
.support_format(libarchive::archive::ReadFormat::Tar)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
builder
.support_filter(libarchive::archive::ReadFilter::All)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
let mut ar = builder
.open_file(&path)
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
for entry in ar.entries() {
let mut entry = entry.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
if entry.pathname() == ".PKGINFO" {
let mut buffer = String::new();
entry.read_to_string(&mut buffer);
println!("{}", buffer);
}
}
2023-07-12 09:04:31 +00:00
Ok(())
}