+ 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:
parent
e4373560a4
commit
b763329abd
@ -4,6 +4,7 @@ use std::env;
|
|||||||
use rocket_dyn_templates::Template;
|
use rocket_dyn_templates::Template;
|
||||||
|
|
||||||
mod page;
|
mod page;
|
||||||
|
mod video;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
@ -15,6 +16,7 @@ async fn main() {
|
|||||||
*/
|
*/
|
||||||
let _ = rocket::build()
|
let _ = rocket::build()
|
||||||
.mount("/", routes![page::home, page::category, page::video, page::files])
|
.mount("/", routes![page::home, page::category, page::video, page::files])
|
||||||
|
.mount("/api", routes![video::list_categories])
|
||||||
.attach(Template::fairing())
|
.attach(Template::fairing())
|
||||||
.launch().await;
|
.launch().await;
|
||||||
}
|
}
|
||||||
|
107
api/src/video.rs
107
api/src/video.rs
@ -1,78 +1,81 @@
|
|||||||
use rocket_contrib::json;
|
|
||||||
use rocket_contrib::json::JsonValue;
|
|
||||||
use serde::Serialize;
|
|
||||||
use base64;
|
use base64;
|
||||||
use std::path::Path;
|
use serde::Serialize;
|
||||||
use std::{fs, env};
|
use std::fs::DirEntry;
|
||||||
use std::fs::File;
|
use std::{io, env};
|
||||||
use std::io::prelude::*;
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Category {
|
pub struct Category {
|
||||||
name: String,
|
name: String,
|
||||||
thumbnail: Option<String>
|
thumbnail: Option<String>
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Video {
|
struct List {
|
||||||
name: String,
|
categories: Vec<Category>
|
||||||
category: String,
|
|
||||||
thumbnail: Option<String>
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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 buf = Vec::new();
|
||||||
let mut file = File::open(t_path)?;
|
let mut file = std::fs::File::open(p)?;
|
||||||
file.read_to_end(&mut buf)?;
|
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")]
|
#[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") {
|
let dir = match env::var("CLIPS_DIR") {
|
||||||
Ok(val) => val,
|
Ok(val) => val,
|
||||||
Err(_) => "/media/clips/".to_string()
|
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();
|
return Json(cats);
|
||||||
for file in fs::read_dir(dir).unwrap() {
|
},
|
||||||
match file {
|
Err(e) => {
|
||||||
Ok(file) => {
|
eprintln!("ERROR: {}", e);
|
||||||
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
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Json(Vec::new())
|
||||||
json!({"categories": cats})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[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})
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user