From e049512c03247bfa85b9d6671152677a7bf3048f Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 12 Oct 2021 21:09:20 -0700 Subject: [PATCH] * Sanity refactor to clean up common warnings ! ALso using the new common module --- api/src/category.rs | 10 ++++------ api/src/common.rs | 15 +++++++++++++++ api/src/main.rs | 1 + api/src/thumbnail.rs | 8 +++----- 4 files changed, 23 insertions(+), 11 deletions(-) create mode 100644 api/src/common.rs diff --git a/api/src/category.rs b/api/src/category.rs index 54b5ef2..556d85c 100644 --- a/api/src/category.rs +++ b/api/src/category.rs @@ -1,7 +1,8 @@ use serde::Serialize; use std::fs::DirEntry; -use std::{io, env}; +use std::io; use rocket::serde::json::Json; +use crate::common::get_clips_dir; #[derive(Serialize)] pub struct Category { @@ -15,7 +16,7 @@ pub struct Category { struct List { categories: Vec } -fn get_category_dirs(path: &str) -> std::io::Result> { +pub fn get_category_dirs(path: &str) -> std::io::Result> { let path = std::path::Path::new(path); if !path.is_dir() { let e = io::Error::new(io::ErrorKind::NotFound, "Unable to open"); @@ -35,10 +36,7 @@ fn get_category_dirs(path: &str) -> std::io::Result> { pub fn list() -> Json> { // WARN: misconfigured servers are just going to get shafted and serve up // a tonne of 500's - let dir = match env::var("CLIPS_DIR") { - Ok(val) => val, - Err(_) => "/media/clips/".to_string() - }; + let dir = get_clips_dir(); let mut cats: Vec = Vec::new(); if let Ok(dirs) = get_category_dirs(&dir) { diff --git a/api/src/common.rs b/api/src/common.rs new file mode 100644 index 0000000..5dfd574 --- /dev/null +++ b/api/src/common.rs @@ -0,0 +1,15 @@ +use std::env; + +pub fn get_clips_dir() -> String { + return match env::var("CLIPS_DIR") { + Ok(val) => val, + Err(_) => String::from("/media/clips/".to_string()) + } +} + +pub fn thumbs_dir() -> String { + return match env::var("THUMBS_DIR") { + Ok(val) => val, + Err(_) => String::from("/media/thumbnails/".to_string()) + } +} diff --git a/api/src/main.rs b/api/src/main.rs index d83b438..4229b29 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -7,6 +7,7 @@ mod page; mod category; mod video; mod thumbnail; +mod common; #[tokio::main] async fn main() { diff --git a/api/src/thumbnail.rs b/api/src/thumbnail.rs index 0673b17..8fbb308 100644 --- a/api/src/thumbnail.rs +++ b/api/src/thumbnail.rs @@ -1,13 +1,11 @@ -// THis module basically takes care of the thumnail use rocket::fs::NamedFile; use std::path::{Path, PathBuf}; +use crate::common::thumbs_dir; #[get("/")] pub async fn get(file: PathBuf) -> Option { - let clips_dir = match std::env::var("THUMBS_DIR") { - Ok(val) => val, - Err(_) => "/media/thumbnails/".to_string() - }; + let clips_dir = thumbs_dir(); + // Only serve jpg's and png's through this route return match file.extension() { Some(ext) => {