From b763329abdb3163ab9c57b90579faeca52c5c913 Mon Sep 17 00:00:00 2001 From: shockrah Date: Mon, 11 Oct 2021 14:34:50 -0700 Subject: [PATCH] + api::list_categories now re-added and working as intended ! This patch is massive but idc right now im sprinting to finish this conversion --- api/src/main.rs | 2 + api/src/video.rs | 109 ++++++++++++++++++++++++----------------------- 2 files changed, 58 insertions(+), 53 deletions(-) diff --git a/api/src/main.rs b/api/src/main.rs index 2d34581..6a69399 100644 --- a/api/src/main.rs +++ b/api/src/main.rs @@ -4,6 +4,7 @@ use std::env; use rocket_dyn_templates::Template; mod page; +mod video; #[tokio::main] async fn main() { @@ -15,6 +16,7 @@ async fn main() { */ let _ = rocket::build() .mount("/", routes![page::home, page::category, page::video, page::files]) + .mount("/api", routes![video::list_categories]) .attach(Template::fairing()) .launch().await; } diff --git a/api/src/video.rs b/api/src/video.rs index 71f0a17..8ff9fed 100644 --- a/api/src/video.rs +++ b/api/src/video.rs @@ -1,78 +1,81 @@ -use rocket_contrib::json; -use rocket_contrib::json::JsonValue; -use serde::Serialize; use base64; -use std::path::Path; -use std::{fs, env}; -use std::fs::File; -use std::io::prelude::*; +use serde::Serialize; +use std::fs::DirEntry; +use std::{io, env}; +use rocket::serde::json::Json; #[derive(Serialize)] -struct Category { +pub struct Category { name: String, thumbnail: Option } #[derive(Serialize)] -struct Video { - name: String, - category: String, - thumbnail: Option +struct List { + categories: Vec } -fn get_nail(dirpath: &Path) -> std::io::Result> { - let mut t_path = dirpath.to_path_buf(); t_path.push(".thumbnail.jpg"); - if t_path.is_file() { + +fn get_category_metadata(entry: DirEntry) -> io::Result<(String, Option)> { + use std::io::Read; + let name = entry.file_name().to_string_lossy().to_string(); + let mut p = entry.path(); p.push(".thumbnail.jpg"); + if p.is_file() { let mut buf = Vec::new(); - let mut file = File::open(t_path)?; + let mut file = std::fs::File::open(p)?; file.read_to_end(&mut buf)?; - return Ok(Some(base64::encode(buf).to_string())); + + let nail = base64::encode(buf).to_string(); + return match nail.len() { + 0 => Ok((name, None)), + _ => Ok((name, Some(nail))) + }; } - Ok(None) + return Ok((String::new(), None)); +} + +fn get_category_dirs(path: &str) -> std::io::Result> { + let path = std::path::Path::new(path); + if !path.is_dir() { + panic!("<{:?}> is not a valid directory", path); + } + + let mut ret: Vec = Vec::new(); + for entry in std::fs::read_dir(path)? { + if let Ok(entry) = entry { + ret.push(entry) + } + } + return Ok(ret); } #[get("/categories")] -pub fn list_categories() -> JsonValue { +pub async fn list_categories() -> 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() }; - - // Fucking hell this is going to be dumb - let mut cats: Vec = Vec::new(); - for file in fs::read_dir(dir).unwrap() { - match file { - Ok(file) => { - let name = file.file_name() - .into_string().unwrap_or(String::new()); - let nail = get_nail(file.path().as_ref()); - cats.push(Category { name, thumbnail: nail.unwrap_or(None) }); - }, - _ => continue + match get_category_dirs(&dir) { + Ok(entries) => { + let mut cats: Vec = Vec::new(); + for ent in entries { + match get_category_metadata(ent) { + Ok((name, thumbnail)) => { + cats.push(Category {name, thumbnail}); + }, + _ => continue + } + + } + return Json(cats); + }, + Err(e) => { + eprintln!("ERROR: {}", e); } } - - json!({"categories": cats}) + Json(Vec::new()) } -#[get("/category/")] -pub fn list_videos(cat: String) -> JsonValue { - let dir = match env::var("CLIPS_DIR") { - Ok(val) => val, - Err(_) => "/media/clips".to_string() - }; - - let mut vids: Vec