source has_thumbnail correctly for the recent file query (#797)

* source `has_thumbnail` correctly

* correctly check if thumbnail exists in watcher

* regen bindings

* add helper fn for getting thumbnail directory and cleanup old thumbnails if the watcher creates a new one
This commit is contained in:
jake authored and GitHub committed 2023-05-05 22:01:34 +00:00
1 parent 66f1034587
commit 4f327db611
5 files changed
+42 -29

No files matched your search

+18 -7
View File
@@ -148,13 +148,24 @@ pub(crate) fn mount() -> AlphaRouter<Ctx> {
.exec()
.await?;
Ok(file_paths
.into_iter()
.map(|x| ExplorerItem::Path {
has_thumbnail: x.object.clone().map_or(false, |x| x.has_thumbnail),
item: x,
})
.collect::<Vec<ExplorerItem>>())
let mut items = vec![];
for path in file_paths.into_iter() {
let has_thumbnail = if let Some(cas_id) = &path.cas_id {
library.thumbnail_exists(cas_id).await.map_err(|e| {
rspc::Error::new(ErrorCode::InternalServerError, e.to_string())
})?
} else {
false
};
items.push(ExplorerItem::Path {
has_thumbnail,
item: path,
});
}
Ok(items)
})
})
.procedure("encryptFiles", {
+2 -7
View File
@@ -3,7 +3,7 @@ use crate::{
job::{IntoJob, JobInitData, JobManagerError, StatefulJob},
location::{file_path_helper::MaterializedPath, LocationManager},
node::NodeConfigManager,
object::preview::THUMBNAIL_CACHE_DIR_NAME,
object::preview::get_thumbnail_path,
prisma::{file_path, location, PrismaClient},
sync::SyncManager,
NodeContext,
@@ -85,12 +85,7 @@ impl Library {
}
pub async fn thumbnail_exists(&self, cas_id: &str) -> tokio::io::Result<bool> {
let thumb_path = self
.config()
.data_directory()
.join(THUMBNAIL_CACHE_DIR_NAME)
.join(cas_id)
.with_extension("webp");
let thumb_path = get_thumbnail_path(self, cas_id);
match tokio::fs::metadata(thumb_path).await {
Ok(_) => Ok(true),
+6 -10
View File
@@ -16,9 +16,7 @@ use crate::{
object::{
file_identifier::FileMetadata,
object_just_id_has_thumbnail,
preview::{
can_generate_thumbnail_for_image, generate_image_thumbnail, THUMBNAIL_CACHE_DIR_NAME,
},
preview::{can_generate_thumbnail_for_image, generate_image_thumbnail, get_thumbnail_path},
validation::hash::file_checksum,
},
prisma::{file_path, location, object},
@@ -422,8 +420,11 @@ async fn inner_update_file(
if let Some(ref object) = file_path.object {
// if this file had a thumbnail previously, we update it to match the new content
if object.has_thumbnail && !file_path.extension.is_empty() {
if library.thumbnail_exists(old_cas_id).await? && !file_path.extension.is_empty() {
generate_thumbnail(&file_path.extension, &cas_id, full_path, library).await;
// remove the old thumbnail as we're generating a new one
fs::remove_file(get_thumbnail_path(library, old_cas_id)).await?;
}
let int_kind = kind as i32;
@@ -657,12 +658,7 @@ async fn generate_thumbnail(
library: &Library,
) {
let path = path.as_ref();
let output_path = library
.config()
.data_directory()
.join(THUMBNAIL_CACHE_DIR_NAME)
.join(cas_id)
.with_extension("webp");
let output_path = get_thumbnail_path(library, cas_id);
if let Err(e) = fs::metadata(&output_path).await {
if e.kind() != ErrorKind::NotFound {
+14 -3
View File
@@ -2,6 +2,7 @@ use crate::{
api::CoreEvent,
invalidate_query,
job::{JobError, JobReportUpdate, JobResult, WorkerContext},
library::Library,
location::{
file_path_helper::{
file_path_just_materialized_path_cas_id, FilePathError, MaterializedPath,
@@ -32,9 +33,19 @@ use webp::Encoder;
pub mod shallow_thumbnailer_job;
pub mod thumbnailer_job;
static THUMBNAIL_SIZE_FACTOR: f32 = 0.2;
static THUMBNAIL_QUALITY: f32 = 30.0;
pub static THUMBNAIL_CACHE_DIR_NAME: &str = "thumbnails";
const THUMBNAIL_SIZE_FACTOR: f32 = 0.2;
const THUMBNAIL_QUALITY: f32 = 30.0;
pub const THUMBNAIL_CACHE_DIR_NAME: &str = "thumbnails";
/// This does not check if a thumbnail exists, it just returns the path that it would exist at
pub fn get_thumbnail_path(library: &Library, cas_id: &str) -> PathBuf {
library
.config()
.data_directory()
.join(THUMBNAIL_CACHE_DIR_NAME)
.join(cas_id)
.with_extension("webp")
}
#[cfg(feature = "ffmpeg")]
static FILTERED_VIDEO_EXTENSIONS: Lazy<Vec<Extension>> = Lazy::new(|| {
+2 -2
View File
@@ -224,8 +224,6 @@ export type LocationExplorerArgs = { location_id: number; path?: string | null;
export type TagAssignArgs = { object_id: number; tag_id: number; unassign: boolean }
export type RenameFileArgs = { location_id: number; file_name: string; new_file_name: string }
export type FileDeleterJobInit = { location_id: number; path_id: number }
/**
@@ -282,6 +280,8 @@ export type LibraryArgs<T> = { library_id: string; arg: T }
export type IdentifyUniqueFilesArgs = { id: number; path: string }
export type RenameFileArgs = { location_id: number; file_name: string; new_file_name: string }
export type ExplorerContext = ({ type: "Location" } & Location) | ({ type: "Tag" } & Tag)
export type OwnedOperationData = { Create: { [key: string]: any } } | { CreateMany: { values: ([any, { [key: string]: any }])[]; skip_duplicates: boolean } } | { Update: { [key: string]: any } } | "Delete"