diff options
| -rw-r--r-- | src/App.svelte | 183 | ||||
| -rw-r--r-- | src/lib/components/ImageList.svelte | 52 | ||||
| -rw-r--r-- | src/lib/components/ImagePreview.svelte | 40 | ||||
| -rw-r--r-- | src/lib/flashair/client.ts | 39 |
4 files changed, 203 insertions, 111 deletions
diff --git a/src/App.svelte b/src/App.svelte index bcac7cb..ea161bf 100644 --- a/src/App.svelte +++ b/src/App.svelte @@ -1,9 +1,11 @@ <script lang="ts"> import { flashair } from './lib/flashair'; import type { FlashAirFileEntry } from './lib/flashair'; + import ImageList from './lib/components/ImageList.svelte'; + import ImagePreview from './lib/components/ImagePreview.svelte'; - let files = $state<FlashAirFileEntry[]>([]); - let currentDir = $state('/DCIM'); + let images = $state<FlashAirFileEntry[]>([]); + let selectedFile = $state<FlashAirFileEntry | undefined>(undefined); let loading = $state(false); let error = $state<string | undefined>(undefined); let isDark = $state(false); @@ -12,134 +14,93 @@ document.documentElement.setAttribute('data-theme', isDark ? 'black' : 'cmyk'); }); - async function loadFiles(dir: string) { + async function loadAllImages() { loading = true; error = undefined; try { - currentDir = dir; - files = await flashair.listFiles(dir); + images = await flashair.listAllImages('/DCIM'); + if (images.length > 0 && selectedFile === undefined) { + selectedFile = images[0]; + } } catch (e) { error = e instanceof Error ? e.message : String(e); - files = []; + images = []; } finally { loading = false; } } - const imageFiles = $derived( - files.filter((f) => !f.isDirectory && /\.(jpe?g|png|bmp|gif)$/i.test(f.filename)), - ); - - const directories = $derived(files.filter((f) => f.isDirectory)); + function selectImage(file: FlashAirFileEntry) { + selectedFile = file; + } </script> -<div class="min-h-screen bg-base-200"> - <div class="navbar bg-base-100 shadow-sm"> - <div class="flex-1"> - <span class="text-xl font-bold px-4">SpeedSync</span> - </div> - <div class="flex-none gap-2"> - <button class="btn btn-ghost btn-sm" onclick={() => loadFiles(currentDir)}> - Refresh - </button> - <label class="swap swap-rotate btn btn-ghost btn-sm"> - <input type="checkbox" bind:checked={isDark} /> - <!-- sun icon --> - <svg class="swap-off fill-current w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> - <path d="M5.64,17l-.71.71a1,1,0,0,0,0,1.41,1,1,0,0,0,1.41,0l.71-.71A1,1,0,0,0,5.64,17ZM5,12a1,1,0,0,0-1-1H3a1,1,0,0,0,0,2H4A1,1,0,0,0,5,12Zm7-7a1,1,0,0,0,1-1V3a1,1,0,0,0-2,0V4A1,1,0,0,0,12,5ZM5.64,7.05a1,1,0,0,0,.7.29,1,1,0,0,0,.71-.29,1,1,0,0,0,0-1.41l-.71-.71A1,1,0,0,0,4.93,6.34Zm12,.29a1,1,0,0,0,.7-.29l.71-.71a1,1,0,1,0-1.41-1.41L17,5.64a1,1,0,0,0,0,1.41A1,1,0,0,0,17.66,7.34ZM21,11H20a1,1,0,0,0,0,2h1a1,1,0,0,0,0-2Zm-9,8a1,1,0,0,0-1,1v1a1,1,0,0,0,2,0V20A1,1,0,0,0,12,19ZM18.36,17A1,1,0,0,0,17,18.36l.71.71a1,1,0,0,0,1.41,0,1,1,0,0,0,0-1.41ZM12,6.5A5.5,5.5,0,1,0,17.5,12,5.51,5.51,0,0,0,12,6.5Zm0,9A3.5,3.5,0,1,1,15.5,12,3.5,3.5,0,0,1,12,15.5Z"/> - </svg> - <!-- moon icon --> - <svg class="swap-on fill-current w-5 h-5" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"> - <path d="M21.64,13a1,1,0,0,0-1.05-.14,8.05,8.05,0,0,1-3.37.73A8.15,8.15,0,0,1,9.08,5.49a8.59,8.59,0,0,1,.25-2A1,1,0,0,0,8,2.36,10.14,10.14,0,1,0,22,14.05,1,1,0,0,0,21.64,13Zm-9.5,6.69A8.14,8.14,0,0,1,7.08,5.22v.27A10.15,10.15,0,0,0,17.22,15.63a9.79,9.79,0,0,0,2.1-.22A8.11,8.11,0,0,1,12.14,19.73Z"/> - </svg> - </label> - </div> - </div> - - <div class="container mx-auto p-4"> - <div class="breadcrumbs text-sm mb-4"> - <ul> - <li> - <button class="link" onclick={() => loadFiles('/')}>/</button> - </li> - {#each currentDir.split('/').filter(Boolean) as segment, i} - <li> - <button - class="link" - onclick={() => - loadFiles( - '/' + - currentDir - .split('/') - .filter(Boolean) - .slice(0, i + 1) - .join('/'), - )} - > - {segment} - </button> - </li> - {/each} - </ul> - </div> - +<div class="flex h-screen bg-base-200"> + <!-- Left: Image preview --> + <div class="flex-1 min-w-0 h-full"> {#if loading} - <div class="flex justify-center py-12"> + <div class="flex items-center justify-center h-full"> <span class="loading loading-spinner loading-lg"></span> </div> {:else if error} - <div role="alert" class="alert alert-error"> - <span>{error}</span> - <button class="btn btn-sm" onclick={() => loadFiles(currentDir)}>Retry</button> + <div class="flex items-center justify-center h-full p-4"> + <div role="alert" class="alert alert-error max-w-md"> + <span>{error}</span> + <button class="btn btn-sm" onclick={() => loadAllImages()}>Retry</button> + </div> </div> - {:else if files.length === 0} - <div class="text-center py-12 text-base-content/60"> - <p>No files found. Connect to the FlashAir WiFi and tap Refresh.</p> - <button class="btn btn-primary mt-4" onclick={() => loadFiles(currentDir)}> - Load Files - </button> + {:else if images.length === 0} + <div class="flex items-center justify-center h-full text-base-content/60 p-8"> + <div class="text-center"> + <p>No photos found. Connect to FlashAir WiFi and tap the refresh button.</p> + <button class="btn btn-primary mt-4" onclick={() => loadAllImages()}>Load Photos</button> + </div> </div> {:else} - {#if directories.length > 0} - <div class="mb-4"> - <h3 class="font-semibold mb-2">Folders</h3> - <div class="flex flex-wrap gap-2"> - {#each directories as dir} - <button class="btn btn-outline btn-sm" onclick={() => loadFiles(dir.path)}> - 📁 {dir.filename} - </button> - {/each} - </div> - </div> - {/if} - - {#if imageFiles.length > 0} - <h3 class="font-semibold mb-2">Photos ({imageFiles.length})</h3> - <div class="grid grid-cols-2 sm:grid-cols-3 md:grid-cols-4 lg:grid-cols-5 gap-2"> - {#each imageFiles as file (file.path)} - {@const thumbUrl = flashair.thumbnailUrl(file.path)} - <div class="card bg-base-100 shadow-sm"> - {#if thumbUrl} - <figure class="aspect-square overflow-hidden"> - <img - src={thumbUrl} - alt={file.filename} - class="object-cover w-full h-full" - loading="lazy" - /> - </figure> - {:else} - <figure class="aspect-square bg-base-300 flex items-center justify-center"> - <span class="text-3xl">🖼️</span> - </figure> - {/if} - <div class="card-body p-2"> - <p class="text-xs truncate">{file.filename}</p> - </div> - </div> - {/each} - </div> - {/if} + <ImagePreview file={selectedFile} /> {/if} </div> + + <!-- Right: Image list --> + <div class="w-72 lg:w-80 shrink-0 border-l border-base-300 flex flex-col"> + <div class="px-3 py-2 bg-base-100 border-b border-base-300 shrink-0"> + <span class="text-sm font-semibold">Photos ({images.length})</span> + </div> + <div class="flex-1 min-h-0"> + <ImageList {images} selectedPath={selectedFile?.path} onSelect={selectImage} /> + </div> + </div> +</div> + +<!-- FAB Flower: bottom-right --> +<div class="fab fab-flower"> + <div tabindex="0" class="btn btn-lg btn-circle btn-neutral"> + <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-5"> + <path stroke-linecap="round" stroke-linejoin="round" d="M3.75 6.75h16.5M3.75 12h16.5m-16.5 5.25h16.5" /> + </svg> + </div> + <div class="fab-close"> + <span class="btn btn-circle btn-lg btn-neutral">✕</span> + </div> + <!-- Dark mode toggle --> + <button class="btn btn-lg btn-circle btn-neutral" onclick={() => (isDark = !isDark)}> + {#if isDark} + <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-5"> + <path stroke-linecap="round" stroke-linejoin="round" d="M12 3v2.25m6.364.386-1.591 1.591M21 12h-2.25m-.386 6.364-1.591-1.591M12 18.75V21m-4.773-4.227-1.591 1.591M5.25 12H3m4.227-4.773L5.636 5.636M15.75 12a3.75 3.75 0 1 1-7.5 0 3.75 3.75 0 0 1 7.5 0Z" /> + </svg> + {:else} + <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-5"> + <path stroke-linecap="round" stroke-linejoin="round" d="M21.752 15.002A9.72 9.72 0 0 1 18 15.75c-5.385 0-9.75-4.365-9.75-9.75 0-1.33.266-2.597.748-3.752A9.753 9.753 0 0 0 3 11.25C3 16.635 7.365 21 12.75 21a9.753 9.753 0 0 0 9.002-5.998Z" /> + </svg> + {/if} + </button> + <!-- Refresh --> + <button class="btn btn-lg btn-circle btn-neutral" onclick={() => loadAllImages()}> + <svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="size-5"> + <path stroke-linecap="round" stroke-linejoin="round" d="M4.5 12a7.5 7.5 0 0 1 12.57-5.55L19.5 8.87" /> + <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 4.5v4.37h-4.37" /> + <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 12a7.5 7.5 0 0 1-12.57 5.55L4.5 15.13" /> + <path stroke-linecap="round" stroke-linejoin="round" d="M4.5 19.5v-4.37h4.37" /> + </svg> + </button> </div> diff --git a/src/lib/components/ImageList.svelte b/src/lib/components/ImageList.svelte new file mode 100644 index 0000000..7e6f836 --- /dev/null +++ b/src/lib/components/ImageList.svelte @@ -0,0 +1,52 @@ +<script lang="ts"> + import { flashair } from '../flashair'; + import type { FlashAirFileEntry } from '../flashair'; + + interface Props { + images: FlashAirFileEntry[]; + selectedPath: string | undefined; + onSelect: (file: FlashAirFileEntry) => void; + } + + let { images, selectedPath, onSelect }: Props = $props(); +</script> + +<div class="h-full overflow-y-auto bg-base-100"> + {#if images.length === 0} + <div class="flex items-center justify-center h-full text-base-content/50 p-4"> + <p class="text-center text-sm">No images found</p> + </div> + {:else} + <ul class="flex flex-col gap-0.5 p-1"> + {#each images as file (file.path)} + {@const thumbUrl = flashair.thumbnailUrl(file.path)} + {@const isSelected = file.path === selectedPath} + <li> + <button + class="flex items-center gap-2 w-full rounded-lg p-1.5 text-left transition-colors {isSelected + ? 'bg-primary text-primary-content' + : 'hover:bg-base-200'}" + onclick={() => onSelect(file)} + > + {#if thumbUrl} + <img + src={thumbUrl} + alt={file.filename} + class="w-12 h-12 rounded object-cover shrink-0" + loading="lazy" + /> + {:else} + <div class="w-12 h-12 rounded bg-base-300 flex items-center justify-center shrink-0"> + <span class="text-lg">🖼️</span> + </div> + {/if} + <div class="min-w-0 flex-1"> + <p class="text-xs font-medium truncate">{file.filename}</p> + <p class="text-xs opacity-60">{file.date.toLocaleString()}</p> + </div> + </button> + </li> + {/each} + </ul> + {/if} +</div> diff --git a/src/lib/components/ImagePreview.svelte b/src/lib/components/ImagePreview.svelte new file mode 100644 index 0000000..82115e7 --- /dev/null +++ b/src/lib/components/ImagePreview.svelte @@ -0,0 +1,40 @@ +<script lang="ts"> + import { flashair } from '../flashair'; + import type { FlashAirFileEntry } from '../flashair'; + + interface Props { + file: FlashAirFileEntry | undefined; + } + + let { file }: Props = $props(); + + let imageUrl = $derived(file !== undefined ? flashair.fileUrl(file.path) : undefined); + let imageLoaded = $state(false); + + $effect(() => { + if (imageUrl !== undefined) { + imageLoaded = false; + } + }); +</script> + +<div class="h-full flex items-center justify-center bg-base-300 relative"> + {#if file === undefined || imageUrl === undefined} + <div class="text-base-content/40 text-center p-8"> + <p class="text-lg">Select a photo to preview</p> + </div> + {:else} + {#key file.path} + {#if !imageLoaded} + <span class="loading loading-spinner loading-lg absolute"></span> + {/if} + <img + src={imageUrl} + alt={file.filename} + class="max-w-full max-h-full object-contain" + class:opacity-0={!imageLoaded} + onload={() => { imageLoaded = true; }} + /> + {/key} + {/if} +</div> diff --git a/src/lib/flashair/client.ts b/src/lib/flashair/client.ts index 4e3a567..978eb60 100644 --- a/src/lib/flashair/client.ts +++ b/src/lib/flashair/client.ts @@ -77,6 +77,9 @@ function parseFileList(text: string): FlashAirFileEntry[] { /** Known image extensions the FlashAir thumbnail.cgi supports (JPEG only). */ const JPEG_EXTENSIONS = new Set(['.jpg', '.jpeg']); +/** Regex matching common image file extensions. */ +const IMAGE_EXTENSIONS = /\.(jpe?g|png|bmp|gif)$/i; + function isJpeg(filename: string): boolean { const ext = filename.slice(filename.lastIndexOf('.')).toLowerCase(); return JPEG_EXTENSIONS.has(ext); @@ -193,6 +196,42 @@ export const flashair = { return { blob, meta }; }, + /** + * Recursively list all image files on the card starting from a root directory. + * Returns files sorted by date, newest first. + */ + async listAllImages(rootDir: string): Promise<FlashAirFileEntry[]> { + const allImages: FlashAirFileEntry[] = []; + const dirsToVisit: string[] = [rootDir]; + + while (dirsToVisit.length > 0) { + const dir = dirsToVisit.pop(); + if (dir === undefined) break; + + let entries: FlashAirFileEntry[]; + try { + entries = await flashair.listFiles(dir); + } catch { + continue; + } + + for (const entry of entries) { + if (entry.isDirectory) { + dirsToVisit.push(entry.path); + } else if (IMAGE_EXTENSIONS.test(entry.filename)) { + allImages.push(entry); + } + } + } + + allImages.sort((a, b) => { + const dateCompare = b.rawDate - a.rawDate; + if (dateCompare !== 0) return dateCompare; + return b.rawTime - a.rawTime; + }); + + return allImages; + }, } as const; export type { FlashAirFileEntry, ThumbnailMeta } from './types'; |
