+ api::list_categories now re-added and working as intended

! This patch is massive but idc right now im sprinting to finish this conversion
This commit is contained in:
shockrah 2021-10-11 14:34:50 -07:00
parent e4373560a4
commit b763329abd
2 changed files with 58 additions and 53 deletions

View File

@ -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;
}

View File

@ -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<String>
}
#[derive(Serialize)]
struct Video {
name: String,
category: String,
thumbnail: Option<String>
struct List {
categories: Vec<Category>
}
fn get_nail(dirpath: &Path) -> std::io::Result<Option<String>> {
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<String>)> {
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<Vec<DirEntry>> {
let path = std::path::Path::new(path);
if !path.is_dir() {
panic!("<{:?}> is not a valid directory", path);
}
let mut ret: Vec<DirEntry> = 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<Vec<Category>> {
// 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()
};
match get_category_dirs(&dir) {
Ok(entries) => {
let mut cats: Vec<Category> = Vec::new();
for ent in entries {
match get_category_metadata(ent) {
Ok((name, thumbnail)) => {
cats.push(Category {name, thumbnail});
},
_ => continue
}
// Fucking hell this is going to be dumb
let mut cats: Vec<Category> = 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
}
return Json(cats);
},
Err(e) => {
eprintln!("ERROR: {}", e);
}
}
json!({"categories": cats})
Json(Vec::new())
}
#[get("/category/<cat>")]
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<Video> = 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());
vids.push(Video {name, category: cat.clone(), thumbnail: None})
},
_ => continue
}
}
return json!({"videos": vids})
}