* Thumbnails are now included in api response
This commit is contained in:
@@ -1,8 +1,29 @@
|
||||
use rocket_contrib::json;
|
||||
use rocket_contrib::json::JsonValue;
|
||||
use std::env;
|
||||
use std::fs;
|
||||
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 {
|
||||
@@ -10,19 +31,18 @@ pub fn list_categories() -> JsonValue {
|
||||
Ok(val) => val,
|
||||
Err(e) => "/media/clips/".to_string()
|
||||
};
|
||||
println!("Directory to search: {}", dir);
|
||||
|
||||
// Fucking hell this is going to be dumb
|
||||
let mut cats: Vec<String> = Vec::new();
|
||||
let mut cats: Vec<Category> = Vec::new();
|
||||
for file in fs::read_dir(dir).unwrap() {
|
||||
match file {
|
||||
Ok(file) => {
|
||||
match file.file_name().into_string() {
|
||||
Ok(s) => cats.push(s),
|
||||
_ => {}
|
||||
}
|
||||
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) });
|
||||
},
|
||||
Err(_) => continue
|
||||
_ => continue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user