warehouse/src/download.rs
2022-08-02 12:33:50 -05:00

30 lines
712 B
Rust

use std::sync::Arc;
use axum::{
body::{BoxBody, StreamBody},
extract::Path,
http::StatusCode,
Extension,
};
use semver::Version;
use tokio::fs::File;
use tokio_util::io::ReaderStream;
use crate::State;
pub async fn download(
Path((crate_name, version)): Path<(String, Version)>,
Extension(state): Extension<Arc<State>>,
) -> Result<BoxBody, StatusCode> {
let mut path = state.crate_dir.clone();
path.push(&crate_name);
path.push(version.to_string());
path.push("crate.crate");
let file = match File::open(path).await {
Ok(v) => v,
Err(_) => return Err(StatusCode::NOT_FOUND),
};
Ok(BoxBody::new(StreamBody::new(ReaderStream::new(file))))
}