From 026028b5a9ea81f177257dd57d6e37c0addf58cd Mon Sep 17 00:00:00 2001 From: Oscar Beaumont Date: Thu, 30 May 2024 20:13:11 +0800 Subject: [PATCH] serve thumbnails over p2p --- apps/desktop/src/platform.ts | 4 ++-- apps/web/src/App.tsx | 4 ++-- core/src/custom_uri/mod.rs | 21 +++++++++++++++---- .../$libraryId/Explorer/FilePath/Thumb.tsx | 4 ++-- interface/app/index.tsx | 4 ++-- interface/util/Platform.tsx | 2 +- 6 files changed, 26 insertions(+), 13 deletions(-) diff --git a/apps/desktop/src/platform.ts b/apps/desktop/src/platform.ts index 8956eff29..3d70296c5 100644 --- a/apps/desktop/src/platform.ts +++ b/apps/desktop/src/platform.ts @@ -45,9 +45,9 @@ function constructServerUrl(urlSuffix: string) { export const platform = { platform: 'tauri', - getThumbnailUrlByThumbKey: (keyParts) => + getThumbnailUrlByThumbKey: (library_id, cas_id) => constructServerUrl( - `/thumbnail/${keyParts.map((i) => encodeURIComponent(i)).join('/')}.webp` + `/thumbnail/${encodeURIComponent(library_id)}/${encodeURIComponent(cas_id)}` ), getFileUrl: (libraryId, locationLocalId, filePathId) => constructServerUrl(`/file/${libraryId}/${locationLocalId}/${filePathId}`), diff --git a/apps/web/src/App.tsx b/apps/web/src/App.tsx index 1619416a8..9e4715e60 100644 --- a/apps/web/src/App.tsx +++ b/apps/web/src/App.tsx @@ -42,8 +42,8 @@ const spacedriveURL = (() => { const platform: Platform = { platform: 'web', - getThumbnailUrlByThumbKey: (keyParts) => - `${spacedriveURL}/thumbnail/${keyParts.map((i) => encodeURIComponent(i)).join('/')}.webp`, + getThumbnailUrlByThumbKey: (library_id, cas_id) => + `${spacedriveURL}/thumbnail/${encodeURIComponent(library_id)}/${encodeURIComponent(cas_id)}`, getFileUrl: (libraryId, locationLocalId, filePathId) => `${spacedriveURL}/file/${encodeURIComponent(libraryId)}/${encodeURIComponent( locationLocalId diff --git a/core/src/custom_uri/mod.rs b/core/src/custom_uri/mod.rs index a57184990..3d7ac95b0 100644 --- a/core/src/custom_uri/mod.rs +++ b/core/src/custom_uri/mod.rs @@ -1,7 +1,9 @@ use crate::{ api::{utils::InvalidateOperationEvent, CoreEvent}, library::Library, - object::media::old_thumbnail::WEBP_EXTENSION, + object::media::old_thumbnail::{ + get_ephemeral_thumb_key, get_indexed_thumb_key, WEBP_EXTENSION, + }, p2p::operations::{self, request_file}, util::InfallibleResponse, Node, @@ -222,13 +224,24 @@ async fn get_or_init_lru_entry( pub fn base_router() -> Router { Router::new() .route( - "/thumbnail/*path", + "/thumbnail/:lib_id/:cas_id", get( |State(state): State, - extract::Path(path): extract::Path, + extract::Path((library_id, cas_id)): extract::Path<(String, String)>, request: Request| async move { + let path = if library_id == "ephemeral" { + get_ephemeral_thumb_key(&cas_id) + } else { + get_indexed_thumb_key(&cas_id, Uuid::from_str(&library_id).map_err(bad_request)?) + }; + let thumbnail_path = state.node.config.data_directory().join("thumbnails"); - let path = thumbnail_path.join(path); + let path = { + let mut p = thumbnail_path.clone(); + p.extend(path); + p.set_extension("webp"); + p + }; // Prevent directory traversal attacks (Eg. requesting `../../../etc/passwd`) // For now we only support `webp` thumbnails. diff --git a/interface/app/$libraryId/Explorer/FilePath/Thumb.tsx b/interface/app/$libraryId/Explorer/FilePath/Thumb.tsx index f533fe7ea..0081c3b61 100644 --- a/interface/app/$libraryId/Explorer/FilePath/Thumb.tsx +++ b/interface/app/$libraryId/Explorer/FilePath/Thumb.tsx @@ -87,8 +87,8 @@ export const FileThumb = forwardRef((props, ref) = break; case 'thumbnail': - if (itemData.thumbnailKey.length > 0) - return platform.getThumbnailUrlByThumbKey(itemData.thumbnailKey); + if (itemData.casId) + return platform.getThumbnailUrlByThumbKey(library.uuid, itemData.casId); break; case 'icon': diff --git a/interface/app/index.tsx b/interface/app/index.tsx index 2b7cec6fd..34dc54bcb 100644 --- a/interface/app/index.tsx +++ b/interface/app/index.tsx @@ -235,10 +235,10 @@ function RemoteLayout() { () => ({ ...platform, - getThumbnailUrlByThumbKey: (thumbKey) => + getThumbnailUrlByThumbKey: (library_id, cas_id) => platform.constructRemoteRspcPath( params.node, - `thumbnail/${thumbKey.map((i) => encodeURIComponent(i)).join('/')}.webp` + `thumbnail/${encodeURIComponent(library_id)}/${encodeURIComponent(cas_id)}` ), getFileUrl: (libraryId, locationLocalId, filePathId) => platform.constructRemoteRspcPath( diff --git a/interface/util/Platform.tsx b/interface/util/Platform.tsx index dc73def1c..989bc7a20 100644 --- a/interface/util/Platform.tsx +++ b/interface/util/Platform.tsx @@ -17,7 +17,7 @@ export type OpenWithApplication = { url: string; name: string }; // This could be Tauri or web. export type Platform = { platform: 'web' | 'tauri'; // This represents the specific platform implementation - getThumbnailUrlByThumbKey: (thumbKey: string[]) => string; + getThumbnailUrlByThumbKey: (library_id: 'ephemeral' | string, cas_id: string) => string; getFileUrl: (libraryId: string, locationLocalId: number, filePathId: number) => string; getFileUrlByPath: (path: string) => string; getRemoteRspcEndpoint: (remote_identity: string) => {