* /api/categories/<cat> fully working and cleaned up

This commit is contained in:
shockrah 2021-10-11 23:39:06 -07:00
parent 7725ff5e1d
commit 69dfa488d9
2 changed files with 16 additions and 12 deletions

2
.gitignore vendored
View File

@ -10,4 +10,4 @@ frontend/themes/clippable/static/js/index.js
frontend/public/
frontend/themes/clippable/static/js/category.js
frontend/themes/clippable/static/js/collection.js
api/static/js/index.js
api/static/js/

View File

@ -26,7 +26,7 @@ fn vid_file_entries(path: &str) -> std::io::Result<Vec<DirEntry>> {
return Ok(entries);
}
pub fn get_thumbnail(clips_dir: &str, vid_file: &str) -> std::io::Result<Option<String>> {
pub fn get_video_preview(clips_dir: &str, vid_file: &str) -> std::io::Result<VideoPreview> {
use std::io::Read;
// Contruct the full path to the video file itself
// NOTE: thumbnail file names are basically file.mkv.jpg
@ -41,20 +41,25 @@ pub fn get_thumbnail(clips_dir: &str, vid_file: &str) -> std::io::Result<Option<
if !path.is_file() {
println!("File {:?} not found", path);
return Ok(None)
let e = std::io::Error::new(std::io::ErrorKind::NotFound, "Thumbnail file not found");
return Err(e);
}
println!("Found file {:?}", path);
// Read into a vec so that base64 can deal with it
let mut buf = Vec::new();
let mut file = std::fs::File::open(path)?;
file.read_to_end(&mut buf)?;
let nail = base64::encode(buf).to_string();
return match nail.len() {
0 => Ok(None),
_ => Ok(Some(nail))
let thumbnail = base64::encode(buf).to_string();
let thumbnail = match thumbnail.len() {
0 => None,
_ => Some(thumbnail)
};
let name = match path.file_name() {
Some(val) => val.to_string_lossy().to_string(),
None => String::new()
};
Ok(VideoPreview { name, thumbnail })
}
#[get("/category/<cat>")]
@ -77,11 +82,10 @@ pub fn list(cat: String) -> Json<Vec<VideoPreview>> {
for vf in vid_files {
// jfc this is rarted
let name = vf.file_name().to_string_lossy().to_string();
let thumbnail = match get_thumbnail(&thumbs_dir, &name) {
Ok(val) => val,
_ => None
match get_video_preview(&thumbs_dir, &name) {
Ok(val) => json.push(val),
_ => continue
};
json.push(VideoPreview { name, thumbnail })
}
return Json(json)