* Thumbnails are now included in api response

This commit is contained in:
shockrah 2021-10-05 13:47:37 -07:00
parent 4a45ef5c97
commit 6b8e7de31a
4 changed files with 45 additions and 19 deletions

20
Cargo.lock generated
View File

@ -159,6 +159,7 @@ dependencies = [
name = "clippable"
version = "0.1.0"
dependencies = [
"base64 0.13.0",
"clap",
"rocket",
"rocket_contrib",
@ -178,6 +179,7 @@ dependencies = [
name = "clippable-server"
version = "0.1.0"
dependencies = [
"base64 0.13.0",
"rocket",
"rocket_contrib",
"serde",
@ -709,9 +711,9 @@ dependencies = [
[[package]]
name = "quote"
version = "1.0.9"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
checksum = "38bc8cc6a5f2e3655e0899c1b848643b2562f853f114bfec7be120678e3ace05"
dependencies = [
"proc-macro2 1.0.29",
]
@ -868,8 +870,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7bc1a1ab1961464eae040d96713baa5a724a8152c1222492465b54322ec508b"
dependencies = [
"proc-macro2 1.0.29",
"quote 1.0.9",
"syn 1.0.77",
"quote 1.0.10",
"syn 1.0.78",
]
[[package]]
@ -904,9 +906,9 @@ checksum = "c307a32c1c5c437f38c7fd45d753050587732ba8628319fbdf12a7e289ccc590"
[[package]]
name = "smallvec"
version = "1.6.1"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e"
checksum = "1ecab6c735a6bb4139c0caafd0cc3635748bbb3acf4550e8138122099251f309"
[[package]]
name = "state"
@ -939,12 +941,12 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.77"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5239bc68e0fef57495900cfea4e8dc75596d9a319d7e16b1e0a440d24e6fe0a0"
checksum = "a4eac2e6c19f5c3abc0c229bea31ff0b9b091c7b14990e8924b92902a303a0c0"
dependencies = [
"proc-macro2 1.0.29",
"quote 1.0.9",
"quote 1.0.10",
"unicode-xid 0.2.2",
]

View File

@ -18,13 +18,16 @@ path = "api/src/main.rs"
# For json hehexd
serde = { version = "1.0", features = ["derive"] }
# This is primarily for turning images(thumbnails) in the api
# into base64 strings
base64 = "0.13.0"
# For commanline parsing
clap = "2.33.3"
# For the backend server we just use rocket
rocket = "0.4"
[dependencies.rocket_contrib]
version = "0.4.10"
default-features = false

View File

@ -8,6 +8,7 @@ edition = "2018"
[dependencies]
rocket = "0.4"
serde = { version = "1.0", features = [ "derive" ] }
base64 = "0.13.0"
[dependencies.rocket_contrib]
version = "0.4.10"

View File

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