blob: d811c4f22a236797746731b4f24c2972e862075d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
<script lang="ts">
import { flashair } from '../flashair';
import type { FlashAirFileEntry } from '../flashair';
interface Props {
file: FlashAirFileEntry | undefined;
}
let { file }: Props = $props();
let thumbnailUrl = $derived(
file !== undefined ? flashair.thumbnailUrl(file.path) : undefined,
);
let fullObjectUrl = $state<string | undefined>(undefined);
let progress = $state(0);
let downloading = $state(false);
let loadError = $state<string | undefined>(undefined);
let currentAbort: AbortController | undefined;
/**
* Plain (non-reactive) mirror of fullObjectUrl so we can revoke it
* without reading the $state variable inside $effect (which would
* add it as a tracked dependency and cause an infinite loop).
*/
let rawObjectUrl: string | undefined;
$effect(() => {
const currentFile = file;
if (currentFile === undefined) {
cleanup();
return;
}
loadFullImage(currentFile);
return () => {
if (currentAbort !== undefined) {
currentAbort.abort();
currentAbort = undefined;
}
};
});
function cleanup() {
if (rawObjectUrl !== undefined) {
URL.revokeObjectURL(rawObjectUrl);
rawObjectUrl = undefined;
}
fullObjectUrl = undefined;
progress = 0;
downloading = false;
loadError = undefined;
}
async function loadFullImage(entry: FlashAirFileEntry) {
cleanup();
if (currentAbort !== undefined) {
currentAbort.abort();
}
const abort = new AbortController();
currentAbort = abort;
downloading = true;
progress = 0;
loadError = undefined;
const url = flashair.fileUrl(entry.path);
const totalBytes = entry.size;
try {
const res = await fetch(url, { signal: abort.signal });
if (!res.ok) {
throw new Error(`${res.status} ${res.statusText}`);
}
const reader = res.body?.getReader();
if (reader === undefined) {
const blob = await res.blob();
if (abort.signal.aborted) return;
const objectUrl = URL.createObjectURL(blob);
rawObjectUrl = objectUrl;
fullObjectUrl = objectUrl;
progress = 1;
downloading = false;
return;
}
const chunks: Uint8Array[] = [];
let received = 0;
while (true) {
const { done, value } = await reader.read();
if (done) break;
chunks.push(value);
received += value.byteLength;
progress = totalBytes > 0 ? received / totalBytes : 0;
}
if (abort.signal.aborted) return;
const blob = new Blob(chunks);
const objectUrl = URL.createObjectURL(blob);
rawObjectUrl = objectUrl;
fullObjectUrl = objectUrl;
progress = 1;
} catch (e) {
if (abort.signal.aborted) return;
loadError = e instanceof Error ? e.message : String(e);
} finally {
if (!abort.signal.aborted) {
downloading = false;
}
}
}
let progressPercent = $derived(Math.round(progress * 100));
let showThumbnail = $derived(fullObjectUrl === undefined && thumbnailUrl !== undefined);
</script>
<div class="h-full flex items-center justify-center bg-base-300 relative">
{#if file === undefined}
<div class="text-base-content/40 text-center p-8">
<p class="text-lg">Select a photo to preview</p>
</div>
{:else if loadError !== undefined}
<div class="text-center p-8">
<p class="text-error mb-2">Failed to load image</p>
<p class="text-sm text-base-content/60">{loadError}</p>
</div>
{:else}
{#if downloading && progress < 1}
<div class="absolute inset-x-0 top-0 z-10 p-3">
<div class="max-w-xs mx-auto">
<div class="flex items-center gap-2">
<progress class="progress progress-primary flex-1" value={progressPercent} max="100"></progress>
<span class="text-xs font-mono text-base-content/70 w-10 text-right">{progressPercent}%</span>
</div>
</div>
</div>
{/if}
{#if fullObjectUrl !== undefined}
{#key fullObjectUrl}
<img
src={fullObjectUrl}
alt={file.filename}
class="max-w-full max-h-full object-contain"
/>
{/key}
{:else if showThumbnail}
<img
src={thumbnailUrl}
alt={file.filename}
class="max-w-full max-h-full object-contain image-rendering-pixelated"
style="image-rendering: pixelated;"
/>
{:else}
<span class="loading loading-spinner loading-lg"></span>
{/if}
{/if}
</div>
|