clippable/api/src/video.rs

51 lines
1.3 KiB
Rust

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::*;
#[derive(Serialize)]
struct Category {
name: 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() {
let mut buf = Vec::new();
let mut file = File::open(t_path)?;
file.read_to_end(&mut buf)?;
return Ok(Some(base64::encode(buf).to_string()));
}
Ok(None)
}
#[get("/categories")]
pub fn list_categories() -> JsonValue {
let dir = match env::var("CLIPS_DIR") {
Ok(val) => val,
Err(e) => "/media/clips/".to_string()
};
// 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
}
}
json!({"categories": cats})
}