* Sanity refactor to clean up common warnings

! ALso using the new common module
This commit is contained in:
shockrah 2021-10-12 21:09:20 -07:00
parent ba7cd26f3a
commit e049512c03
4 changed files with 23 additions and 11 deletions

View File

@ -1,7 +1,8 @@
use serde::Serialize; use serde::Serialize;
use std::fs::DirEntry; use std::fs::DirEntry;
use std::{io, env}; use std::io;
use rocket::serde::json::Json; use rocket::serde::json::Json;
use crate::common::get_clips_dir;
#[derive(Serialize)] #[derive(Serialize)]
pub struct Category { pub struct Category {
@ -15,7 +16,7 @@ pub struct Category {
struct List { struct List {
categories: Vec<Category> categories: Vec<Category>
} }
fn get_category_dirs(path: &str) -> std::io::Result<Vec<DirEntry>> { pub fn get_category_dirs(path: &str) -> std::io::Result<Vec<DirEntry>> {
let path = std::path::Path::new(path); let path = std::path::Path::new(path);
if !path.is_dir() { if !path.is_dir() {
let e = io::Error::new(io::ErrorKind::NotFound, "Unable to open"); let e = io::Error::new(io::ErrorKind::NotFound, "Unable to open");
@ -35,10 +36,7 @@ fn get_category_dirs(path: &str) -> std::io::Result<Vec<DirEntry>> {
pub fn list() -> Json<Vec<Category>> { pub fn list() -> Json<Vec<Category>> {
// WARN: misconfigured servers are just going to get shafted and serve up // WARN: misconfigured servers are just going to get shafted and serve up
// a tonne of 500's // a tonne of 500's
let dir = match env::var("CLIPS_DIR") { let dir = get_clips_dir();
Ok(val) => val,
Err(_) => "/media/clips/".to_string()
};
let mut cats: Vec<Category> = Vec::new(); let mut cats: Vec<Category> = Vec::new();
if let Ok(dirs) = get_category_dirs(&dir) { if let Ok(dirs) = get_category_dirs(&dir) {

15
api/src/common.rs Normal file
View File

@ -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())
}
}

View File

@ -7,6 +7,7 @@ mod page;
mod category; mod category;
mod video; mod video;
mod thumbnail; mod thumbnail;
mod common;
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {

View File

@ -1,13 +1,11 @@
// THis module basically takes care of the thumnail
use rocket::fs::NamedFile; use rocket::fs::NamedFile;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use crate::common::thumbs_dir;
#[get("/<file..>")] #[get("/<file..>")]
pub async fn get(file: PathBuf) -> Option<NamedFile> { pub async fn get(file: PathBuf) -> Option<NamedFile> {
let clips_dir = match std::env::var("THUMBS_DIR") { let clips_dir = thumbs_dir();
Ok(val) => val,
Err(_) => "/media/thumbnails/".to_string()
};
// Only serve jpg's and png's through this route // Only serve jpg's and png's through this route
return match file.extension() { return match file.extension() {
Some(ext) => { Some(ext) => {