Skip to content

Commit

Permalink
Move on disk upload to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
sneakycrow committed Dec 24, 2024
1 parent 0eadd51 commit 5ccff38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
20 changes: 7 additions & 13 deletions services/silo-api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@ mod middleware;
mod routes;

use axum::{
extract::DefaultBodyLimit,
middleware as axum_mw,
response::IntoResponse,
routing::{delete, get, post},
Router,
};
use config::Config;
use queue::{PostgresQueue, Queue};
use routes::upload::UPLOAD_CHUNK_SIZE;
use sqlx::PgPool;
use std::sync::Arc;
use tower_http::{
Expand Down Expand Up @@ -55,7 +53,6 @@ async fn main() {
let queue = Arc::new(PostgresQueue::new(db.clone()));
// Store shared data as state between routes
let state = Arc::new(AppState { db, config, queue });
routes::upload::init_cleanup().await;
// Initialize our router with the shared state and required routes
let app = Router::new()
.route("/", get(index))
Expand Down Expand Up @@ -88,16 +85,13 @@ async fn main() {
middleware::auth::auth_middleware,
)),
)
// TODO: Update this to use Backblaze instead
// .route(
// "/upload",
// post(routes::upload::upload_video)
// .layer(DefaultBodyLimit::max(UPLOAD_CHUNK_SIZE * 8))
// .layer(axum_mw::from_fn_with_state(
// state.clone(),
// middleware::auth::auth_middleware,
// )),
// )
.route(
"/upload",
post(routes::upload::on_disk::upload_video).layer(axum_mw::from_fn_with_state(
state.clone(),
middleware::auth::auth_middleware,
)),
)
.nest(
"/video",
Router::new()
Expand Down
1 change: 1 addition & 0 deletions services/silo-api/src/routes/upload/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod on_disk;
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ lazy_static::lazy_static! {
static ref UPLOAD_STATES: Mutex<HashMap<String, UploadState>> = Mutex::new(HashMap::new());
}

/// Upload video directly to the server
/// DEPRECATED: Favor using Cloudflare-R2 instead
pub async fn upload_video(
State(state): State<Arc<AppState>>,
Extension(user): Extension<Option<User>>,
Expand Down Expand Up @@ -396,6 +398,8 @@ pub async fn upload_video(
}
}

/// Removes temp files that are more than an hour old. This is useful for cleaning up
/// partially uploaded files that were never completed.
async fn cleanup_temp_files() -> Result<(), std::io::Error> {
let upload_dir = std::env::current_dir()?.join("uploads");
let mut read_dir = tokio::fs::read_dir(&upload_dir).await?;
Expand All @@ -416,6 +420,7 @@ async fn cleanup_temp_files() -> Result<(), std::io::Error> {
Ok(())
}

/// Cleans up temporary upload files by removing those older than 1 hour
pub async fn init_cleanup() {
tokio::spawn(async {
loop {
Expand Down

0 comments on commit 5ccff38

Please sign in to comment.