feat: db downloads
This commit is contained in:
parent
ec6b1b6477
commit
07273fad14
3 changed files with 27 additions and 23 deletions
|
@ -1,8 +1,6 @@
|
||||||
UPDATE record
|
UPDATE records
|
||||||
SET downloads = downloads + 1
|
SET downloads = downloads + 1
|
||||||
WHERE cache_name = ?
|
WHERE
|
||||||
RETURNING
|
cache_name = ?
|
||||||
cache_name,
|
AND downloads < max_downloads
|
||||||
uploaded,
|
AND julianday('now') - julianday(uploaded) > 5;
|
||||||
downloads,
|
|
||||||
max_downloads;
|
|
||||||
|
|
|
@ -8,3 +8,7 @@ pub mod views;
|
||||||
pub use router::*;
|
pub use router::*;
|
||||||
pub use state::*;
|
pub use state::*;
|
||||||
pub use views::*;
|
pub use views::*;
|
||||||
|
|
||||||
|
use std::{path::PathBuf, sync::LazyLock};
|
||||||
|
|
||||||
|
pub static CACHE_DIR: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("./.cache/serve"));
|
||||||
|
|
|
@ -8,7 +8,7 @@ use axum::{
|
||||||
use reqwest::StatusCode;
|
use reqwest::StatusCode;
|
||||||
use tokio_util::io::ReaderStream;
|
use tokio_util::io::ReaderStream;
|
||||||
|
|
||||||
use crate::{AppState, AsyncRemoveRecord};
|
use crate::{AppState, CACHE_DIR};
|
||||||
|
|
||||||
pub fn get_download_router() -> Router<AppState> {
|
pub fn get_download_router() -> Router<AppState> {
|
||||||
Router::new().route("/:id", get(download))
|
Router::new().route("/:id", get(download))
|
||||||
|
@ -18,23 +18,25 @@ async fn download(
|
||||||
axum::extract::Path(id): axum::extract::Path<String>,
|
axum::extract::Path(id): axum::extract::Path<String>,
|
||||||
State(state): State<AppState>,
|
State(state): State<AppState>,
|
||||||
) -> Result<axum::response::Response, (StatusCode, String)> {
|
) -> Result<axum::response::Response, (StatusCode, String)> {
|
||||||
{
|
let mut conn = state.pool.acquire().await.unwrap();
|
||||||
let mut records = state.records.lock().await;
|
|
||||||
if let Some(record) = records
|
|
||||||
.get_mut(&id)
|
|
||||||
.filter(|record| record.can_be_downloaded())
|
|
||||||
{
|
|
||||||
record.downloads += 1;
|
|
||||||
|
|
||||||
let file = tokio::fs::File::open(&record.file).await.unwrap();
|
let rows_affected = sqlx::query_file!("queries/records/get_and_update.sql", id)
|
||||||
|
.execute(&mut *conn)
|
||||||
|
.await
|
||||||
|
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?
|
||||||
|
.rows_affected();
|
||||||
|
|
||||||
|
drop(conn);
|
||||||
|
|
||||||
|
if rows_affected > 0 {
|
||||||
|
let file = tokio::fs::File::open(CACHE_DIR.join(id))
|
||||||
|
.await
|
||||||
|
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
|
||||||
|
|
||||||
return Ok(axum::response::Response::builder()
|
return Ok(axum::response::Response::builder()
|
||||||
.header("Content-Type", "application/zip")
|
.header("Content-Type", "application/zip")
|
||||||
.body(Body::from_stream(ReaderStream::new(file)))
|
.body(Body::from_stream(ReaderStream::new(file)))
|
||||||
.unwrap());
|
.unwrap());
|
||||||
} else {
|
|
||||||
records.remove_record(&id).await.unwrap()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(Redirect::to("/404.html").into_response())
|
Ok(Redirect::to("/404.html").into_response())
|
||||||
|
|
Loading…
Reference in a new issue