allow overriding mime for thumbs

feat/vaults
Tomáš Mládek 2022-02-10 14:58:00 +01:00
parent 76e9439f40
commit 0dd5616e9c
No known key found for this signature in database
GPG Key ID: 65E225C8B3E2ED8A
2 changed files with 21 additions and 3 deletions

View File

@ -52,7 +52,10 @@ impl PreviewStore {
}
}
pub fn get(&self, hash: Hash) -> Result<Option<PathBuf>> {
pub fn get<S>(&self, hash: Hash, mime_type: S) -> Result<Option<PathBuf>>
where
S: Into<Option<String>>,
{
debug!("Preview for {hash:?} requested...");
let path_mutex = self.get_path(&hash);
let thumbpath = path_mutex.lock().unwrap();
@ -64,7 +67,15 @@ impl PreviewStore {
let connection = self.db.connection()?;
let files = connection.retrieve_file(&hash)?;
if let Some(file) = files.get(0) {
let preview = match tree_magic_mini::from_filepath(&file.path) {
let mime_type = mime_type.into();
let mime_type: Option<String> = if mime_type.is_some() {
mime_type
} else {
tree_magic_mini::from_filepath(&file.path).map(|m| m.into())
};
let preview = match mime_type {
Some(tm) if tm.starts_with("text") => Ok(TextPath(&file.path).get_thumbnail()?),
Some(tm) if tm.starts_with("video") || tm == "application/x-matroska" => {
Ok(VideoPath(&file.path).get_thumbnail()?)

View File

@ -128,10 +128,16 @@ pub async fn get_raw(
}
}
#[derive(Deserialize)]
pub struct ThumbRequest {
mime: Option<String>,
}
#[get("/api/thumb/{hash}")]
pub async fn get_thumbnail(
state: web::Data<State>,
hash: web::Path<String>,
web::Query(query): web::Query<ThumbRequest>,
) -> Result<Either<NamedFile, HttpResponse>, Error> {
#[cfg(feature = "previews")]
if let Some(preview_store) = &state.preview_store {
@ -140,7 +146,8 @@ pub async fn get_thumbnail(
.map_err(ErrorInternalServerError)?;
if let Address::Hash(address_hash) = address {
let preview_store = preview_store.clone();
let preview_result = web::block(move || preview_store.get(address_hash)).await?;
let preview_result =
web::block(move || preview_store.get(address_hash, query.mime)).await?;
if let Some(preview_path) = preview_result {
let mut file = NamedFile::open(&preview_path)?.disable_content_disposition();