serve thumbnails over p2p

This commit is contained in:
Oscar Beaumont 2024-05-30 20:13:11 +08:00
parent 0392c781d7
commit 026028b5a9
6 changed files with 26 additions and 13 deletions

View file

@ -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}`),

View file

@ -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

View file

@ -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<LocalState> {
Router::new()
.route(
"/thumbnail/*path",
"/thumbnail/:lib_id/:cas_id",
get(
|State(state): State<LocalState>,
extract::Path(path): extract::Path<String>,
extract::Path((library_id, cas_id)): extract::Path<(String, String)>,
request: Request<Body>| 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.

View file

@ -87,8 +87,8 @@ export const FileThumb = forwardRef<HTMLImageElement, ThumbProps>((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':

View file

@ -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(

View file

@ -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) => {