Compare commits
No commits in common. "5f4158e27d132e889adedf541b10c0ef93e7d52b" and "ec6b1b6477cfc947061e717d80821db3118a1bde" have entirely different histories.
5f4158e27d
...
ec6b1b6477
18 changed files with 220 additions and 133 deletions
|
@ -1,12 +0,0 @@
|
|||
{
|
||||
"db_name": "SQLite",
|
||||
"query": "UPDATE records\nSET downloads = downloads + 1\nWHERE\n cache_name = ?\n AND downloads < max_downloads\n AND julianday('now') - julianday(uploaded) > 5;\n",
|
||||
"describe": {
|
||||
"columns": [],
|
||||
"parameters": {
|
||||
"Right": 1
|
||||
},
|
||||
"nullable": []
|
||||
},
|
||||
"hash": "a2f3905f8f8e14c95404f88f2e60dcf13e7b7caf5307bd19240655621251f7c9"
|
||||
}
|
|
@ -1,6 +1,8 @@
|
|||
UPDATE records
|
||||
UPDATE record
|
||||
SET downloads = downloads + 1
|
||||
WHERE
|
||||
cache_name = ?
|
||||
AND downloads < max_downloads
|
||||
AND julianday('now') - julianday(uploaded) > 5;
|
||||
WHERE cache_name = ?
|
||||
RETURNING
|
||||
cache_name,
|
||||
uploaded,
|
||||
downloads,
|
||||
max_downloads;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
SELECT
|
||||
cache_name,
|
||||
uploaded AS "uploaded: _",
|
||||
downloads AS "downloads: i32",
|
||||
max_downloads AS "max_downloads: i32"
|
||||
uploaded,
|
||||
downloads,
|
||||
max_downloads
|
||||
FROM records
|
||||
WHERE cache_name = ?;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
SELECT
|
||||
cache_name,
|
||||
uploaded AS "uploaded: _",
|
||||
downloads AS "downloads: i32",
|
||||
max_downloads AS "max_downloads: i32"
|
||||
uploaded,
|
||||
downloads,
|
||||
max_downloads
|
||||
FROM records;
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
SELECT
|
||||
cache_name,
|
||||
uploaded AS "uploaded: _",
|
||||
downloads AS "downloads: i32",
|
||||
max_downloads AS "max_downloads: i32"
|
||||
uploaded,
|
||||
downloads,
|
||||
max_downloads
|
||||
FROM records
|
||||
LIMIT ? OFFSET ?;
|
||||
|
|
|
@ -1,7 +1,2 @@
|
|||
INSERT INTO records (cache_name, max_downloads)
|
||||
VALUES (?, ?)
|
||||
RETURNING
|
||||
cache_name,
|
||||
uploaded AS "uploaded: _",
|
||||
downloads AS "downloads: i32",
|
||||
max_downloads AS "max_downloads: i32";
|
||||
VALUES (?, ?);
|
||||
|
|
38
src/cache.rs
Normal file
38
src/cache.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
use crate::state::AppState;
|
||||
|
||||
use serde::Serialize;
|
||||
use tokio::io::AsyncReadExt;
|
||||
|
||||
use std::io;
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub async fn write_to_cache<T, Y>(records: &HashMap<T, Y>) -> io::Result<()>
|
||||
where
|
||||
T: Serialize,
|
||||
Y: Serialize,
|
||||
{
|
||||
let mut records_cache = tokio::fs::File::create(".cache/data").await.unwrap();
|
||||
|
||||
let mut buf: Vec<u8> = Vec::with_capacity(200);
|
||||
bincode::serialize_into(&mut buf, records).map_err(io::Error::other)?;
|
||||
|
||||
let bytes_written = tokio::io::copy(&mut buf.as_slice(), &mut records_cache).await?;
|
||||
|
||||
tracing::debug!("state cache size: {}", bytes_written);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn fetch_cache() -> AppState {
|
||||
let records = if let Ok(file) = tokio::fs::File::open(".cache/data").await.as_mut() {
|
||||
let mut buf: Vec<u8> = Vec::with_capacity(200);
|
||||
file.read_to_end(&mut buf).await.unwrap();
|
||||
|
||||
bincode::deserialize_from(&mut buf.as_slice()).unwrap()
|
||||
} else {
|
||||
HashMap::new()
|
||||
};
|
||||
|
||||
AppState::new(records)
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
|
||||
#[derive(sqlx::Type)]
|
||||
pub struct CacheRecord {
|
||||
pub cache_name: String,
|
||||
pub uploaded: DateTime<Utc>,
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
pub mod cache;
|
||||
pub mod db;
|
||||
pub mod router;
|
||||
pub mod state;
|
||||
|
@ -7,7 +8,3 @@ pub mod views;
|
|||
pub use router::*;
|
||||
pub use state::*;
|
||||
pub use views::*;
|
||||
|
||||
use std::{path::PathBuf, sync::LazyLock};
|
||||
|
||||
pub static CACHE_DIR: LazyLock<PathBuf> = LazyLock::new(|| PathBuf::from("./.cache/serve"));
|
||||
|
|
|
@ -22,7 +22,7 @@ async fn main() -> io::Result<()> {
|
|||
// uses create_dir_all to create both .cache and serve inside it in one go
|
||||
util::make_dir(".cache/serve").await?;
|
||||
|
||||
let state = AppState::new();
|
||||
let state = cache::fetch_cache().await;
|
||||
|
||||
sweeper::spawn(state.clone());
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ use axum::{
|
|||
use reqwest::StatusCode;
|
||||
use tokio_util::io::ReaderStream;
|
||||
|
||||
use crate::{AppState, CACHE_DIR};
|
||||
use crate::{AppState, AsyncRemoveRecord};
|
||||
|
||||
pub fn get_download_router() -> Router<AppState> {
|
||||
Router::new().route("/:id", get(download))
|
||||
|
@ -18,25 +18,23 @@ async fn download(
|
|||
axum::extract::Path(id): axum::extract::Path<String>,
|
||||
State(state): State<AppState>,
|
||||
) -> 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 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()))?;
|
||||
let file = tokio::fs::File::open(&record.file).await.unwrap();
|
||||
|
||||
return Ok(axum::response::Response::builder()
|
||||
.header("Content-Type", "application/zip")
|
||||
.body(Body::from_stream(ReaderStream::new(file)))
|
||||
.unwrap());
|
||||
} else {
|
||||
records.remove_record(&id).await.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
Ok(Redirect::to("/404.html").into_response())
|
||||
|
|
|
@ -8,10 +8,9 @@ use axum_extra::TypedHeader;
|
|||
use reqwest::StatusCode;
|
||||
|
||||
use crate::{
|
||||
db::CacheRecord,
|
||||
templates::{DownloadLinkTemplate, DownloadsRemainingFragment},
|
||||
templates::{self, DownloadLinkTemplate},
|
||||
util::headers::HxRequest,
|
||||
AppState,
|
||||
AppState, AsyncRemoveRecord,
|
||||
};
|
||||
|
||||
pub fn get_link_router() -> Router<AppState> {
|
||||
|
@ -23,28 +22,47 @@ pub fn get_link_router() -> Router<AppState> {
|
|||
|
||||
async fn link(
|
||||
axum::extract::Path(id): axum::extract::Path<String>,
|
||||
State(state): State<AppState>,
|
||||
State(mut state): State<AppState>,
|
||||
) -> Result<impl IntoResponse, Redirect> {
|
||||
let mut conn = state.pool.acquire().await.unwrap();
|
||||
{
|
||||
let mut records = state.records.lock().await;
|
||||
|
||||
sqlx::query_file_as!(CacheRecord, "queries/records/get_record.sql", id)
|
||||
.fetch_one(&mut *conn)
|
||||
.await
|
||||
.map(|record| DownloadLinkTemplate { record })
|
||||
.map_err(|_| Redirect::to("/404.html"))
|
||||
if let Some(record) = records
|
||||
.get_mut(&id)
|
||||
.filter(|record| record.can_be_downloaded())
|
||||
{
|
||||
return Ok(DownloadLinkTemplate {
|
||||
id,
|
||||
record: record.clone(),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: This....
|
||||
state.remove_record(&id).await.unwrap();
|
||||
|
||||
Err(Redirect::to("/404.html"))
|
||||
}
|
||||
|
||||
async fn link_delete(
|
||||
axum::extract::Path(id): axum::extract::Path<String>,
|
||||
State(state): State<AppState>,
|
||||
State(mut state): State<AppState>,
|
||||
) -> Result<Html<String>, (StatusCode, String)> {
|
||||
state
|
||||
.remove_record(&id)
|
||||
.await
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
|
||||
|
||||
{
|
||||
let mut conn = state.pool.acquire().await.unwrap();
|
||||
|
||||
sqlx::query_file!("queries/records/remove_record.sql", id)
|
||||
.execute(&mut *conn)
|
||||
.await
|
||||
.map(|_| Html("".to_string()))
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
|
||||
}
|
||||
|
||||
Ok(Html("".to_string()))
|
||||
}
|
||||
|
||||
async fn remaining(
|
||||
|
@ -59,11 +77,12 @@ async fn remaining(
|
|||
));
|
||||
}
|
||||
|
||||
let mut conn = state.pool.acquire().await.unwrap();
|
||||
let records = state.records.lock().await;
|
||||
|
||||
sqlx::query_file_as!(CacheRecord, "queries/records/get_record.sql", id)
|
||||
.fetch_one(&mut *conn)
|
||||
.await
|
||||
.map(|record| DownloadsRemainingFragment { record })
|
||||
.map_err(|err| (StatusCode::NOT_FOUND, err.to_string()))
|
||||
Ok(Html(
|
||||
records
|
||||
.get(&id)
|
||||
.map(templates::get_downloads_remaining_text)
|
||||
.unwrap_or_else(|| "?".to_string()),
|
||||
))
|
||||
}
|
||||
|
|
|
@ -6,13 +6,13 @@ use crate::{db::CacheRecordName, templates::LinkListTemplate, AppState};
|
|||
pub fn get_records_router() -> Router<AppState> {
|
||||
// Records views
|
||||
Router::new()
|
||||
// .route("/", get(records))
|
||||
.route("/", get(records))
|
||||
.route("/links", get(records_links))
|
||||
}
|
||||
|
||||
// pub(crate) async fn records(State(state): State<AppState>) -> impl IntoResponse {
|
||||
// Json(state.records.lock().await.clone())
|
||||
// }
|
||||
pub(crate) async fn records(State(state): State<AppState>) -> impl IntoResponse {
|
||||
Json(state.records.lock().await.clone())
|
||||
}
|
||||
|
||||
// This function is to remain ugly until that time in which I properly hide
|
||||
// this behind some kind of authentication
|
||||
|
|
|
@ -14,7 +14,7 @@ use tokio::io;
|
|||
use tokio_util::{compat::FuturesAsyncWriteCompatExt, io::StreamReader};
|
||||
use tower_http::limit::RequestBodyLimitLayer;
|
||||
|
||||
use crate::{db::CacheRecord, templates::DownloadLinkFragment, util, AppState};
|
||||
use crate::{cache, templates::DownloadLinkFragment, util, AppState, UploadRecord};
|
||||
|
||||
pub fn get_upload_router() -> Router<AppState> {
|
||||
// Upload needs a subrouter to increase the body limit
|
||||
|
@ -28,44 +28,21 @@ pub fn get_upload_router() -> Router<AppState> {
|
|||
|
||||
async fn upload_to_zip(
|
||||
State(state): State<AppState>,
|
||||
body: Multipart,
|
||||
mut body: Multipart,
|
||||
) -> Result<Response, (StatusCode, String)> {
|
||||
tracing::debug!("{:?}", *state.records.lock().await);
|
||||
|
||||
let cache_name = util::get_random_name(10);
|
||||
|
||||
let archive_path = Path::new(".cache/serve").join(format!("{}.zip", &cache_name));
|
||||
|
||||
tracing::debug!("Zipping: {:?}", &archive_path);
|
||||
|
||||
zip_body(&archive_path, body).await?;
|
||||
|
||||
let mut conn = state.pool.acquire().await.unwrap();
|
||||
|
||||
let record = sqlx::query_file_as!(CacheRecord, "queries/records/new_record.sql", cache_name, 5)
|
||||
.fetch_one(&mut *conn)
|
||||
.await
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
|
||||
|
||||
let id = cache_name;
|
||||
let impl_response = (
|
||||
StatusCode::OK,
|
||||
[
|
||||
("Content-Type", "text/html"),
|
||||
("HX-Push-Url", &format!("/link/{}", &id)),
|
||||
],
|
||||
DownloadLinkFragment { record },
|
||||
);
|
||||
|
||||
Ok(impl_response.into_response())
|
||||
}
|
||||
|
||||
async fn zip_body(
|
||||
archive_path: &std::path::PathBuf,
|
||||
mut body: Multipart,
|
||||
) -> Result<(), (StatusCode, String)> {
|
||||
let mut archive = tokio::fs::File::create(archive_path)
|
||||
let mut archive = tokio::fs::File::create(&archive_path)
|
||||
.await
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
|
||||
let mut writer = ZipFileWriter::with_tokio(&mut archive);
|
||||
|
||||
while let Some(field) = body.next_field().await.unwrap() {
|
||||
let file_name = match field.file_name() {
|
||||
Some(file_name) => sanitize(file_name),
|
||||
|
@ -95,6 +72,41 @@ async fn zip_body(
|
|||
.await
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
|
||||
}
|
||||
|
||||
let record = UploadRecord::new(archive_path);
|
||||
let mut records = state.records.lock().await;
|
||||
|
||||
records.insert(cache_name.clone(), record.clone());
|
||||
|
||||
let records_cache = records.clone();
|
||||
// Manually drop the records mutex guard
|
||||
drop(records);
|
||||
|
||||
{
|
||||
let mut conn = state.pool.acquire().await.unwrap();
|
||||
let cache_name = cache_name.clone();
|
||||
|
||||
sqlx::query_file!("queries/records/new_record.sql", cache_name, 5)
|
||||
.execute(&mut *conn)
|
||||
.await
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
|
||||
}
|
||||
|
||||
cache::write_to_cache(&records_cache)
|
||||
.await
|
||||
.map_err(|err| (StatusCode::INTERNAL_SERVER_ERROR, err.to_string()))?;
|
||||
|
||||
writer.close().await.unwrap();
|
||||
Ok(())
|
||||
|
||||
let id = cache_name;
|
||||
let impl_response = (
|
||||
StatusCode::OK,
|
||||
[
|
||||
("Content-Type", "text/html"),
|
||||
("HX-Push-Url", &format!("/link/{}", &id)),
|
||||
],
|
||||
DownloadLinkFragment { id, record },
|
||||
);
|
||||
|
||||
Ok(impl_response.into_response())
|
||||
}
|
||||
|
|
43
src/state.rs
43
src/state.rs
|
@ -1,11 +1,18 @@
|
|||
use std::{
|
||||
collections::{hash_map::Entry, HashMap},
|
||||
io::ErrorKind,
|
||||
path::{Path, PathBuf},
|
||||
str::FromStr,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use async_trait::async_trait;
|
||||
use chrono::{DateTime, Duration, Utc};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{sqlite::SqliteConnectOptions, SqlitePool};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::cache;
|
||||
|
||||
#[allow(dead_code)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
|
@ -48,12 +55,14 @@ impl Default for UploadRecord {
|
|||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub records: Arc<Mutex<HashMap<String, UploadRecord>>>,
|
||||
pub pool: SqlitePool,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
pub fn new() -> Self {
|
||||
pub fn new(records: HashMap<String, UploadRecord>) -> Self {
|
||||
Self {
|
||||
records: Arc::new(Mutex::new(records)),
|
||||
pool: SqlitePool::connect_lazy_with(
|
||||
SqliteConnectOptions::from_str("sqlite:testing.db")
|
||||
.expect("Invalid Database String"),
|
||||
|
@ -62,8 +71,34 @@ impl AppState {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for AppState {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
#[async_trait]
|
||||
pub trait AsyncRemoveRecord {
|
||||
async fn remove_record(&mut self, id: &str) -> Result<(), std::io::Error>;
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AsyncRemoveRecord for AppState {
|
||||
async fn remove_record(&mut self, id: &str) -> Result<(), std::io::Error> {
|
||||
let mut records = self.records.lock().await;
|
||||
records.remove_record(id).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl AsyncRemoveRecord for HashMap<String, UploadRecord> {
|
||||
async fn remove_record(&mut self, id: &str) -> Result<(), std::io::Error> {
|
||||
match self.entry(id.to_string()) {
|
||||
Entry::Occupied(entry) => {
|
||||
tokio::fs::remove_file(&entry.get().file).await?;
|
||||
entry.remove_entry();
|
||||
cache::write_to_cache(self).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Entry::Vacant(_) => Err(std::io::Error::new(
|
||||
ErrorKind::Other,
|
||||
"No UploadRecord Found",
|
||||
)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use crate::state::AppState;
|
||||
use crate::state::{AppState, AsyncRemoveRecord};
|
||||
|
||||
/// Spawn a repeating task that will clean files periodically
|
||||
pub fn spawn(_state: AppState) {
|
||||
pub fn spawn(state: AppState) {
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
tokio::time::sleep(Duration::from_secs(15 * 60)).await;
|
||||
tracing::info!("Cleaning Sweep!");
|
||||
|
||||
// let mut records = state.records.lock().await;
|
||||
//
|
||||
// for (key, record) in records.clone().into_iter() {
|
||||
// if !record.can_be_downloaded() {
|
||||
// tracing::info!("culling: {:?}", record);
|
||||
// records.remove_record(&key).await.unwrap();
|
||||
// }
|
||||
// }
|
||||
let mut records = state.records.lock().await;
|
||||
|
||||
for (key, record) in records.clone().into_iter() {
|
||||
if !record.can_be_downloaded() {
|
||||
tracing::info!("culling: {:?}", record);
|
||||
records.remove_record(&key).await.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use rinja_axum::Template;
|
||||
|
||||
use crate::db::CacheRecord;
|
||||
use crate::UploadRecord;
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "welcome.html")]
|
||||
|
@ -15,7 +15,7 @@ impl WelcomeTemplate {
|
|||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_downloads_remaining_text(record: &CacheRecord) -> String {
|
||||
pub fn get_downloads_remaining_text(record: &UploadRecord) -> String {
|
||||
let downloads_remaining = record.max_downloads - record.downloads;
|
||||
let plural = if downloads_remaining > 1 { "s" } else { "" };
|
||||
|
||||
|
@ -28,19 +28,21 @@ pub fn get_downloads_remaining_text(record: &CacheRecord) -> String {
|
|||
#[derive(Template)]
|
||||
#[template(path = "link.html")]
|
||||
pub struct DownloadLinkTemplate {
|
||||
pub record: CacheRecord,
|
||||
pub id: String,
|
||||
pub record: UploadRecord,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "link.html", block = "content")]
|
||||
pub struct DownloadLinkFragment {
|
||||
pub record: CacheRecord,
|
||||
pub id: String,
|
||||
pub record: UploadRecord,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
#[template(path = "link.html", block = "downloads_remaining")]
|
||||
pub struct DownloadsRemainingFragment {
|
||||
pub record: CacheRecord,
|
||||
pub record: UploadRecord,
|
||||
}
|
||||
|
||||
#[derive(Template)]
|
||||
|
|
|
@ -4,12 +4,12 @@
|
|||
<div class="form-wrapper">
|
||||
<div class="column-container">
|
||||
<div class="link-wrapper">
|
||||
<a id="link" href="/download/{{ record.cache_name }}">Download Now!</a>
|
||||
<a id="link" href="/download/{{ id }}">Download Now!</a>
|
||||
</div>
|
||||
|
||||
<div
|
||||
class="link-wrapper"
|
||||
hx-get="/link/{{ record.cache_name }}/remaining"
|
||||
hx-get="/link/{{ id }}/remaining"
|
||||
hx-trigger="click from:#link delay:0.2s, every 60s"
|
||||
>
|
||||
{% block downloads_remaining %}
|
||||
|
|
Loading…
Reference in a new issue