2023-07-12 18:05:10 +00:00
|
|
|
mod manager;
|
2023-08-04 16:40:17 +00:00
|
|
|
pub mod package;
|
2023-07-12 18:05:10 +00:00
|
|
|
|
2023-07-13 12:58:27 +00:00
|
|
|
pub use manager::RepoGroupManager;
|
|
|
|
|
2023-08-03 19:25:42 +00:00
|
|
|
use std::path::PathBuf;
|
|
|
|
|
2024-05-19 07:47:39 +00:00
|
|
|
use axum::body::{Body, BodyDataStream};
|
|
|
|
use axum::extract::{Path, State};
|
2023-08-02 19:15:12 +00:00
|
|
|
use axum::http::Request;
|
2023-07-12 09:04:31 +00:00
|
|
|
use axum::http::StatusCode;
|
2023-08-02 19:15:12 +00:00
|
|
|
use axum::response::IntoResponse;
|
|
|
|
use axum::routing::{delete, post};
|
2023-07-11 11:41:56 +00:00
|
|
|
use axum::Router;
|
2023-07-12 09:04:31 +00:00
|
|
|
use futures::StreamExt;
|
2023-08-04 16:40:17 +00:00
|
|
|
use sea_orm::ModelTrait;
|
2023-07-13 18:24:45 +00:00
|
|
|
use std::sync::Arc;
|
2023-07-16 17:59:47 +00:00
|
|
|
use tokio::{fs, io::AsyncWriteExt};
|
2023-08-02 19:15:12 +00:00
|
|
|
use tower::util::ServiceExt;
|
|
|
|
use tower_http::services::{ServeDir, ServeFile};
|
2023-08-03 07:34:33 +00:00
|
|
|
use tower_http::validate_request::ValidateRequestHeaderLayer;
|
2023-07-12 09:04:31 +00:00
|
|
|
use uuid::Uuid;
|
2023-07-11 11:41:56 +00:00
|
|
|
|
2023-08-03 13:08:34 +00:00
|
|
|
const DB_FILE_EXTS: [&str; 4] = [".db", ".files", ".db.tar.gz", ".files.tar.gz"];
|
|
|
|
|
2023-08-03 09:08:38 +00:00
|
|
|
pub fn router(api_key: &str) -> Router<crate::Global> {
|
2023-07-11 11:41:56 +00:00
|
|
|
Router::new()
|
2023-08-03 07:34:33 +00:00
|
|
|
.route(
|
|
|
|
"/:repo",
|
|
|
|
post(post_package_archive)
|
|
|
|
.delete(delete_repo)
|
2023-08-03 09:08:38 +00:00
|
|
|
.route_layer(ValidateRequestHeaderLayer::bearer(api_key)),
|
2023-08-03 07:34:33 +00:00
|
|
|
)
|
|
|
|
.route(
|
|
|
|
"/:repo/:arch",
|
2023-08-03 09:08:38 +00:00
|
|
|
delete(delete_arch_repo).route_layer(ValidateRequestHeaderLayer::bearer(api_key)),
|
2023-08-03 07:34:33 +00:00
|
|
|
)
|
|
|
|
// Routes added after the layer do not get that layer applied, so the GET requests will not
|
|
|
|
// be authorized
|
2023-07-12 09:04:31 +00:00
|
|
|
.route(
|
2023-07-16 19:01:57 +00:00
|
|
|
"/:repo/:arch/:filename",
|
2023-08-03 07:34:33 +00:00
|
|
|
delete(delete_package)
|
2023-08-03 09:08:38 +00:00
|
|
|
.route_layer(ValidateRequestHeaderLayer::bearer(api_key))
|
2023-08-03 07:34:33 +00:00
|
|
|
.get(get_file),
|
2023-07-12 09:04:31 +00:00
|
|
|
)
|
2023-07-11 11:41:56 +00:00
|
|
|
}
|
2023-07-12 09:04:31 +00:00
|
|
|
|
2023-08-03 19:25:42 +00:00
|
|
|
/// Serve the package archive files and database archives. If files are requested for an
|
|
|
|
/// architecture that does not have any explicit packages, a repository containing only "any" files
|
|
|
|
/// is returned.
|
|
|
|
async fn get_file(
|
|
|
|
State(global): State<crate::Global>,
|
|
|
|
Path((repo, arch, mut file_name)): Path<(String, String, String)>,
|
|
|
|
req: Request<Body>,
|
|
|
|
) -> crate::Result<impl IntoResponse> {
|
|
|
|
let repo_dir = global.config.repo_dir.join(&repo).join(&arch);
|
|
|
|
let repo_exists = tokio::fs::try_exists(&repo_dir).await?;
|
|
|
|
|
|
|
|
let res = if DB_FILE_EXTS.iter().any(|ext| file_name.ends_with(ext)) {
|
|
|
|
// Append tar extension to ensure we find the file
|
|
|
|
if !file_name.ends_with(".tar.gz") {
|
|
|
|
file_name.push_str(".tar.gz");
|
|
|
|
};
|
|
|
|
|
|
|
|
if repo_exists {
|
|
|
|
ServeFile::new(repo_dir.join(file_name)).oneshot(req).await
|
|
|
|
} else {
|
|
|
|
let path = global
|
|
|
|
.config
|
|
|
|
.repo_dir
|
|
|
|
.join(repo)
|
|
|
|
.join(manager::ANY_ARCH)
|
|
|
|
.join(file_name);
|
|
|
|
|
|
|
|
ServeFile::new(path).oneshot(req).await
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let any_file = global
|
|
|
|
.config
|
|
|
|
.pkg_dir
|
|
|
|
.join(repo)
|
|
|
|
.join(manager::ANY_ARCH)
|
|
|
|
.join(file_name);
|
|
|
|
|
|
|
|
if repo_exists {
|
|
|
|
ServeDir::new(global.config.pkg_dir)
|
|
|
|
.fallback(ServeFile::new(any_file))
|
|
|
|
.oneshot(req)
|
|
|
|
.await
|
|
|
|
} else {
|
|
|
|
ServeFile::new(any_file).oneshot(req).await
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(res)
|
|
|
|
}
|
|
|
|
|
2023-07-12 09:04:31 +00:00
|
|
|
async fn post_package_archive(
|
2023-07-13 12:58:27 +00:00
|
|
|
State(global): State<crate::Global>,
|
2023-07-12 09:04:31 +00:00
|
|
|
Path(repo): Path<String>,
|
2024-05-19 07:47:39 +00:00
|
|
|
body: Body,
|
2023-07-14 11:46:53 +00:00
|
|
|
) -> crate::Result<()> {
|
2023-07-12 09:04:31 +00:00
|
|
|
// We first stream the uploaded file to disk
|
|
|
|
let uuid: uuid::fmt::Simple = Uuid::new_v4().into();
|
2023-07-13 12:58:27 +00:00
|
|
|
let path = global.config.pkg_dir.join(uuid.to_string());
|
2023-07-14 11:46:53 +00:00
|
|
|
let mut f = fs::File::create(&path).await?;
|
2023-07-12 09:04:31 +00:00
|
|
|
|
2024-05-19 07:47:39 +00:00
|
|
|
let mut body = body.into_data_stream();
|
|
|
|
|
2023-07-12 09:04:31 +00:00
|
|
|
while let Some(chunk) = body.next().await {
|
2023-07-14 11:46:53 +00:00
|
|
|
f.write_all(&chunk?).await?;
|
2023-07-12 09:04:31 +00:00
|
|
|
}
|
|
|
|
|
2023-07-13 18:24:45 +00:00
|
|
|
let clone = Arc::clone(&global.repo_manager);
|
2023-08-02 19:15:12 +00:00
|
|
|
let path_clone = path.clone();
|
2023-08-01 13:30:38 +00:00
|
|
|
let repo_clone = repo.clone();
|
2023-08-02 19:15:12 +00:00
|
|
|
let res = tokio::task::spawn_blocking(move || {
|
2023-08-02 20:41:23 +00:00
|
|
|
clone
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.add_pkg_from_path(&repo_clone, &path_clone)
|
2023-08-02 19:15:12 +00:00
|
|
|
})
|
|
|
|
.await?;
|
2023-07-14 11:46:53 +00:00
|
|
|
|
2023-08-02 20:41:23 +00:00
|
|
|
match res {
|
|
|
|
// Insert the newly added package into the database
|
|
|
|
Ok(pkg) => {
|
2023-08-03 07:34:33 +00:00
|
|
|
tracing::info!("Added '{}' to repository '{}'", pkg.file_name(), repo);
|
|
|
|
|
2023-08-02 20:41:23 +00:00
|
|
|
// Query the repo for its ID, or create it if it does not already exist
|
2023-08-07 15:45:57 +00:00
|
|
|
let res = global.db.repo.by_name(&repo).await?;
|
2023-08-02 20:41:23 +00:00
|
|
|
|
2023-08-03 19:25:42 +00:00
|
|
|
let repo_id = if let Some(repo_entity) = res {
|
2023-08-02 20:41:23 +00:00
|
|
|
repo_entity.id
|
|
|
|
} else {
|
2023-08-07 15:45:57 +00:00
|
|
|
global.db.repo.insert(&repo, None).await?.last_insert_id
|
2023-08-02 20:41:23 +00:00
|
|
|
};
|
|
|
|
|
2023-08-03 19:25:42 +00:00
|
|
|
// If the package already exists in the database, we remove it first
|
2023-08-04 15:33:03 +00:00
|
|
|
let res = global
|
|
|
|
.db
|
2023-08-07 15:45:57 +00:00
|
|
|
.pkg
|
|
|
|
.by_fields(repo_id, &pkg.info.name, None, &pkg.info.arch)
|
2023-08-03 19:25:42 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
if let Some(entry) = res {
|
|
|
|
entry.delete(&global.db).await?;
|
|
|
|
}
|
|
|
|
|
2023-08-07 15:45:57 +00:00
|
|
|
global.db.pkg.insert(repo_id, pkg).await?;
|
2023-08-02 20:41:23 +00:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
// Remove the uploaded file and return the error
|
|
|
|
Err(err) => {
|
|
|
|
tokio::fs::remove_file(path).await?;
|
|
|
|
|
|
|
|
Err(err.into())
|
|
|
|
}
|
2023-08-02 19:15:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-16 17:59:47 +00:00
|
|
|
async fn delete_repo(
|
|
|
|
State(global): State<crate::Global>,
|
|
|
|
Path(repo): Path<String>,
|
|
|
|
) -> crate::Result<StatusCode> {
|
|
|
|
let clone = Arc::clone(&global.repo_manager);
|
|
|
|
|
2023-08-03 09:03:01 +00:00
|
|
|
let repo_clone = repo.clone();
|
2023-07-16 17:59:47 +00:00
|
|
|
let repo_removed =
|
2023-08-03 09:08:38 +00:00
|
|
|
tokio::task::spawn_blocking(move || clone.write().unwrap().remove_repo(&repo_clone))
|
|
|
|
.await??;
|
2023-07-16 17:59:47 +00:00
|
|
|
|
|
|
|
if repo_removed {
|
2023-08-07 15:45:57 +00:00
|
|
|
let res = global.db.repo.by_name(&repo).await?;
|
2023-08-03 19:25:42 +00:00
|
|
|
|
|
|
|
if let Some(repo_entry) = res {
|
|
|
|
repo_entry.delete(&global.db).await?;
|
|
|
|
}
|
|
|
|
|
2023-08-03 09:08:38 +00:00
|
|
|
tracing::info!("Removed repository '{}'", repo);
|
2023-08-03 09:03:01 +00:00
|
|
|
|
2023-07-16 17:59:47 +00:00
|
|
|
Ok(StatusCode::OK)
|
|
|
|
} else {
|
|
|
|
Ok(StatusCode::NOT_FOUND)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn delete_arch_repo(
|
|
|
|
State(global): State<crate::Global>,
|
|
|
|
Path((repo, arch)): Path<(String, String)>,
|
|
|
|
) -> crate::Result<StatusCode> {
|
|
|
|
let clone = Arc::clone(&global.repo_manager);
|
|
|
|
|
2023-08-03 19:25:42 +00:00
|
|
|
let arch_clone = arch.clone();
|
|
|
|
let repo_clone = repo.clone();
|
|
|
|
let repo_removed = tokio::task::spawn_blocking(move || {
|
|
|
|
clone
|
|
|
|
.write()
|
|
|
|
.unwrap()
|
|
|
|
.remove_repo_arch(&repo_clone, &arch_clone)
|
|
|
|
})
|
|
|
|
.await??;
|
2023-07-16 17:59:47 +00:00
|
|
|
|
|
|
|
if repo_removed {
|
2023-08-07 15:45:57 +00:00
|
|
|
let res = global.db.repo.by_name(&repo).await?;
|
2023-08-03 19:25:42 +00:00
|
|
|
|
|
|
|
if let Some(repo_entry) = res {
|
2023-08-07 15:45:57 +00:00
|
|
|
global.db.pkg.delete_with_arch(repo_entry.id, &arch).await?;
|
2023-08-03 19:25:42 +00:00
|
|
|
}
|
|
|
|
tracing::info!("Removed architecture '{}' from repository '{}'", arch, repo);
|
2023-08-03 09:03:01 +00:00
|
|
|
|
2023-07-16 17:59:47 +00:00
|
|
|
Ok(StatusCode::OK)
|
|
|
|
} else {
|
|
|
|
Ok(StatusCode::NOT_FOUND)
|
|
|
|
}
|
|
|
|
}
|
2023-07-16 18:22:58 +00:00
|
|
|
|
|
|
|
async fn delete_package(
|
|
|
|
State(global): State<crate::Global>,
|
|
|
|
Path((repo, arch, file_name)): Path<(String, String, String)>,
|
|
|
|
) -> crate::Result<StatusCode> {
|
|
|
|
let clone = Arc::clone(&global.repo_manager);
|
2023-08-03 19:25:42 +00:00
|
|
|
let path = PathBuf::from(&repo).join(arch).join(&file_name);
|
2023-07-16 18:22:58 +00:00
|
|
|
|
2023-08-03 19:25:42 +00:00
|
|
|
let res = tokio::task::spawn_blocking(move || {
|
|
|
|
clone.write().unwrap().remove_pkg_from_path(path, true)
|
2023-07-16 18:22:58 +00:00
|
|
|
})
|
|
|
|
.await??;
|
|
|
|
|
2023-08-03 19:25:42 +00:00
|
|
|
if let Some((name, version, release, arch)) = res {
|
2023-08-07 15:45:57 +00:00
|
|
|
let res = global.db.repo.by_name(&repo).await?;
|
2023-08-03 19:25:42 +00:00
|
|
|
|
|
|
|
if let Some(repo_entry) = res {
|
2023-08-04 15:33:03 +00:00
|
|
|
let res = global
|
|
|
|
.db
|
2023-08-07 15:45:57 +00:00
|
|
|
.pkg
|
|
|
|
.by_fields(
|
2023-08-04 15:33:03 +00:00
|
|
|
repo_entry.id,
|
|
|
|
&name,
|
|
|
|
Some(&format!("{}-{}", version, release)),
|
|
|
|
&arch,
|
|
|
|
)
|
2023-08-03 19:25:42 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
if let Some(entry) = res {
|
|
|
|
entry.delete(&global.db).await?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
tracing::info!("Removed '{}' from repository '{}'", file_name, repo);
|
2023-08-03 09:03:01 +00:00
|
|
|
|
2023-07-16 18:22:58 +00:00
|
|
|
Ok(StatusCode::OK)
|
|
|
|
} else {
|
|
|
|
Ok(StatusCode::NOT_FOUND)
|
|
|
|
}
|
|
|
|
}
|