From a43a9d6caf3f66ab03475938331433d1b7626b5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Mon, 27 Dec 2021 12:40:02 +0100 Subject: [PATCH] thumbnails -> previews --- Cargo.toml | 4 ++-- src/main.rs | 17 ++++++++--------- src/{thumbnails => previews}/mod.rs | 8 ++++---- src/{thumbnails => previews}/text.rs | 4 ++-- src/{thumbnails => previews}/video.rs | 4 ++-- src/routes.rs | 16 ++++++++-------- 6 files changed, 26 insertions(+), 27 deletions(-) rename src/{thumbnails => previews}/mod.rs (95%) rename src/{thumbnails => previews}/text.rs (84%) rename src/{thumbnails => previews}/video.rs (95%) diff --git a/Cargo.toml b/Cargo.toml index ae648a0..89e78e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,6 @@ webbrowser = { version = "^0.5.5", optional = true } nonempty = "0.6.0" [features] -default = ["desktop", "thumbnails"] +default = ["desktop", "previews"] desktop = ["webbrowser", "opener", "is_executable"] -thumbnails = [] +previews = [] diff --git a/src/main.rs b/src/main.rs index c3778d1..43b91f6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -16,7 +16,6 @@ use log::{info, warn}; use std::sync::{Arc, RwLock}; use crate::database::UpEndDatabase; -use crate::thumbnails::ThumbnailStore; mod addressing; mod database; @@ -24,8 +23,8 @@ mod filesystem; mod routes; mod util; -#[cfg(feature = "thumbnails")] -mod thumbnails; +#[cfg(feature = "previews")] +mod previews; const VERSION: &str = env!("CARGO_PKG_VERSION"); @@ -100,14 +99,14 @@ fn main() -> Result<()> { let upend = Arc::new(open_result.db); - #[cfg(feature = "thumbnails")] - let thumbnail_store = Some(Arc::new(ThumbnailStore::new( - upend.db_path.join("thumbnails"), + #[cfg(feature = "previews")] + let preview_store = Some(Arc::new(crate::previews::PreviewStore::new( + upend.db_path.join("previews"), upend.clone(), ))); - #[cfg(not(feature = "thumbnails"))] - let thumbnail_store = None; + #[cfg(not(feature = "previews"))] + let preview_store = None; let mut bind: SocketAddr = matches .value_of("BIND") @@ -131,7 +130,7 @@ fn main() -> Result<()> { }), ), job_container: job_container.clone(), - thumbnail_store, + preview_store, }; // Start HTTP server diff --git a/src/thumbnails/mod.rs b/src/previews/mod.rs similarity index 95% rename from src/thumbnails/mod.rs rename to src/previews/mod.rs index 94d7259..1c25879 100644 --- a/src/thumbnails/mod.rs +++ b/src/previews/mod.rs @@ -16,19 +16,19 @@ use self::video::VideoPath; pub mod text; pub mod video; -pub trait Thumbnailable { +pub trait Previewable { fn get_thumbnail(&self) -> Result>; } -pub struct ThumbnailStore { +pub struct PreviewStore { path: PathBuf, db: Arc, locks: Mutex>>>, } -impl ThumbnailStore { +impl PreviewStore { pub fn new>(path: P, db: Arc) -> Self { - ThumbnailStore { + PreviewStore { path: PathBuf::from(path.as_ref()), db, locks: Mutex::new(HashMap::new()), diff --git a/src/thumbnails/text.rs b/src/previews/text.rs similarity index 84% rename from src/thumbnails/text.rs rename to src/previews/text.rs index 7b7ba74..6858de1 100644 --- a/src/thumbnails/text.rs +++ b/src/previews/text.rs @@ -1,11 +1,11 @@ use anyhow::Result; use std::{cmp::min, convert::TryInto, fs::File, io::Read, path::Path}; -use super::Thumbnailable; +use super::Previewable; pub struct TextPath<'a>(pub &'a Path); -impl<'a> Thumbnailable for TextPath<'a> { +impl<'a> Previewable for TextPath<'a> { fn get_thumbnail(&self) -> Result> { let mut f = File::open(self.0)?; let mut buffer = vec![0u8; min(1024, f.metadata()?.len().try_into()?)]; diff --git a/src/thumbnails/video.rs b/src/previews/video.rs similarity index 95% rename from src/thumbnails/video.rs rename to src/previews/video.rs index e2dc394..d5b0242 100644 --- a/src/thumbnails/video.rs +++ b/src/previews/video.rs @@ -5,11 +5,11 @@ use std::process::Command; use anyhow::Result; -use super::Thumbnailable; +use super::Previewable; pub struct VideoPath<'a>(pub &'a Path); -impl<'a> Thumbnailable for VideoPath<'a> { +impl<'a> Previewable for VideoPath<'a> { fn get_thumbnail(&self) -> Result> { let duration_cmd = Command::new("ffprobe") .args(["-v", "error"]) diff --git a/src/routes.rs b/src/routes.rs index 4ce0d90..f24db0d 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -3,7 +3,7 @@ use crate::database::entry::{Entry, InEntry}; use crate::database::hierarchies::{list_roots, resolve_path, UHierPath}; use crate::database::lang::Query; use crate::database::UpEndDatabase; -use crate::thumbnails::ThumbnailStore; +use crate::previews::PreviewStore; use crate::util::hash::{decode, encode}; use crate::util::jobs::JobContainer; use actix_files::NamedFile; @@ -29,7 +29,7 @@ pub struct State { pub upend: Arc, pub vault_name: Option, pub job_container: Arc>, - pub thumbnail_store: Option>, + pub preview_store: Option>, } #[derive(Deserialize)] @@ -315,17 +315,17 @@ pub async fn get_thumbnail( state: web::Data, hash: web::Path, ) -> Result { - if let Some(thumbnail_store) = &state.thumbnail_store { + if let Some(preview_store) = &state.preview_store { let address = Address::decode(&decode(hash.into_inner()).map_err(ErrorInternalServerError)?) .map_err(ErrorInternalServerError)?; if let Address::Hash(hash) = address { - let thumbnail_path = thumbnail_store + let preview_path = preview_store .get(hash) .map_err(error::ErrorInternalServerError)?; - let mut file = NamedFile::open(&thumbnail_path)?.disable_content_disposition(); - if let Some(mime_type) = tree_magic_mini::from_filepath(&thumbnail_path) { + let mut file = NamedFile::open(&preview_path)?.disable_content_disposition(); + if let Some(mime_type) = tree_magic_mini::from_filepath(&preview_path) { if let Ok(mime) = mime_type.parse() { file = file.set_content_type(mime); } @@ -333,10 +333,10 @@ pub async fn get_thumbnail( Ok(file) } else { Err(ErrorBadRequest( - "Address does not refer to a thumbnailable object.", + "Address does not refer to a previewable object.", )) } } else { - Err(error::ErrorNotImplemented("Thumbnails not enabled.")) + Err(error::ErrorNotImplemented("Previews not enabled.")) } }