BlueGit

timfa filemanager

Expand file icon thumbnails to text, video, and generated type badges

timfa Tim Falken · pushed by timfa

Authored 2026-05-03 · 6 months ago

Hash
39828d75d235d04b6da8a983118e07f7b5d2ddac
Parent
7b957c4d
2 files changed +229 −20
.github/technical-reference.md +3 −1
@@ -34,7 +34,9 @@
34 34 - showPrompt(title, message, defaultValue) - In-page text-input dialog; returns Promise<string|null>. Use instead of window.prompt().
35 35 - showOverwrite(filename) - In-page overwrite-decision dialog for file conflicts; returns Promise<'yes'|'no'|'all'|'none'>.
36 36 - loadThumbnails() - Loads per-file image thumbnails into file SVG icons after grid rendering.
37 - loadOneThumbnail(iconEl) - Fetches authenticated image blob and injects clipped thumbnail into a file icon SVG.
37 - loadOneThumbnail(iconEl) - Injects clipped in-icon thumbnails: real previews for image/video/text where feasible and generated badges for other file types.
38 - getThumbnailKind(name) - Classifies files into thumbnail strategy buckets (image/video/text/audio/pdf/badge).
39 - buildThumbnailSource(item, blob, kind) - Produces thumbnail image source (blob URL/data URL) for icon injection.
38 40 - detectCodeLanguage(name, mime) - Determines preview language for text/script files by MIME and extension.
39 41 - prettyPrintCode(raw, lang) - Best-effort formatter for JSON/markup/script previews before rendering.
40 42 - highlightCode(raw, lang) - Escapes and syntax-highlights preview text into HTML spans.
@@ -34,7 +34,9 @@
34- showPrompt(title, message, defaultValue) - In-page text-input dialog; returns Promise<string|null>. Use instead of window.prompt().
34- showPrompt(title, message, defaultValue) - In-page text-input dialog; returns Promise<string|null>. Use instead of window.prompt().
35- showOverwrite(filename) - In-page overwrite-decision dialog for file conflicts; returns Promise<'yes'|'no'|'all'|'none'>.
35- showOverwrite(filename) - In-page overwrite-decision dialog for file conflicts; returns Promise<'yes'|'no'|'all'|'none'>.
36- loadThumbnails() - Loads per-file image thumbnails into file SVG icons after grid rendering.
36- loadThumbnails() - Loads per-file image thumbnails into file SVG icons after grid rendering.
37- loadOneThumbnail(iconEl) - Fetches authenticated image blob and injects clipped thumbnail into a file icon SVG.
37- loadOneThumbnail(iconEl) - Injects clipped in-icon thumbnails: real previews for image/video/text where feasible and generated badges for other file types.
38- getThumbnailKind(name) - Classifies files into thumbnail strategy buckets (image/video/text/audio/pdf/badge).
39- buildThumbnailSource(item, blob, kind) - Produces thumbnail image source (blob URL/data URL) for icon injection.
38- detectCodeLanguage(name, mime) - Determines preview language for text/script files by MIME and extension.
40- detectCodeLanguage(name, mime) - Determines preview language for text/script files by MIME and extension.
39- prettyPrintCode(raw, lang) - Best-effort formatter for JSON/markup/script previews before rendering.
41- prettyPrintCode(raw, lang) - Best-effort formatter for JSON/markup/script previews before rendering.
40- highlightCode(raw, lang) - Escapes and syntax-highlights preview text into HTML spans.
42- highlightCode(raw, lang) - Escapes and syntax-highlights preview text into HTML spans.
public/app.js +226 −19
@@ -891,7 +891,7 @@ function makeFileItem (item)
891 891 const icon = document.createElement('div');
892 892 icon.className = 'icon';
893 893 icon.innerHTML = item.isDir ? iconFolder() : iconFile(item.name);
894 if (!item.isDir && IMAGE_EXTS.has(extOf(item.name)))
894 if (!item.isDir)
895 895 {
896 896 icon.dataset.previewPath = item.path;
897 897 }
@@ -1908,6 +1908,189 @@ function revokeThumbnails ()
1908 1908 STATE.thumbnailUrls = [];
1909 1909 }
1910 1910
1911 function getThumbnailKind (name)
1912 {
1913 const ext = extOf(name || '');
1914 if (IMAGE_EXTS.has(ext)) return 'image';
1915 if (VIDEO_EXTS.has(ext)) return 'video';
1916 if (CODE_EXTS.has(ext)) return 'text';
1917 if (AUDIO_EXTS.has(ext)) return 'audio';
1918 if (ext === 'pdf') return 'pdf';
1919 return 'badge';
1920 }
1921
1922 function maxThumbBytesForKind (kind)
1923 {
1924 if (kind === 'image') return 15 * 1024 * 1024;
1925 if (kind === 'video') return 25 * 1024 * 1024;
1926 if (kind === 'text') return 1024 * 1024;
1927 return 0;
1928 }
1929
1930 function drawTokenizedLine (ctx, line, x, y)
1931 {
1932 const tokenRe = /(\/\/.*$|\/\*[\s\S]*?\*\/|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`|\b(?:function|return|if|else|for|while|switch|case|break|continue|const|let|var|class|new|import|from|export|default|try|catch|finally|throw|await|async|true|false|null|undefined)\b|\b\d+(?:\.\d+)?\b)/g;
1933 let last = 0;
1934 let cursor = x;
1935 for (const m of line.matchAll(tokenRe))
1936 {
1937 const i = m.index == null ? 0 : m.index;
1938 if (i > last)
1939 {
1940 const plain = line.slice(last, i);
1941 ctx.fillStyle = '#dce4ff';
1942 ctx.fillText(plain, cursor, y);
1943 cursor += ctx.measureText(plain).width;
1944 }
1945
1946 const tok = m[0];
1947 if (/^\/\//.test(tok) || /^\/\*/.test(tok)) ctx.fillStyle = '#6f7f9a';
1948 else if (/^['"`]/.test(tok)) ctx.fillStyle = '#9ee37d';
1949 else if (/^\d/.test(tok)) ctx.fillStyle = '#f8c46a';
1950 else ctx.fillStyle = '#8bb4ff';
1951
1952 ctx.fillText(tok, cursor, y);
1953 cursor += ctx.measureText(tok).width;
1954 last = i + tok.length;
1955 }
1956
1957 if (last < line.length)
1958 {
1959 const tail = line.slice(last);
1960 ctx.fillStyle = '#dce4ff';
1961 ctx.fillText(tail, cursor, y);
1962 }
1963 }
1964
1965 function textThumbnailDataUrl (text)
1966 {
1967 const canvas = document.createElement('canvas');
1968 canvas.width = 160;
1969 canvas.height = 200;
1970 const ctx = canvas.getContext('2d');
1971 if (!ctx) return null;
1972
1973 ctx.fillStyle = '#0d111a';
1974 ctx.fillRect(0, 0, canvas.width, canvas.height);
1975 ctx.font = '8px ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace';
1976 ctx.textBaseline = 'top';
1977
1978 const lines = String(text || '')
1979 .replace(/\r\n/g, '\n')
1980 .split('\n')
1981 .slice(0, 17);
1982
1983 let y = 8;
1984 for (const line of lines)
1985 {
1986 drawTokenizedLine(ctx, line.slice(0, 42), 6, y);
1987 y += 11;
1988 if (y > 190) break;
1989 }
1990 return canvas.toDataURL('image/png');
1991 }
1992
1993 function badgeThumbnailDataUrl (name, kind)
1994 {
1995 const canvas = document.createElement('canvas');
1996 canvas.width = 160;
1997 canvas.height = 200;
1998 const ctx = canvas.getContext('2d');
1999 if (!ctx) return null;
2000
2001 const ext = (extOf(name || '') || 'file').toUpperCase().slice(0, 5);
2002 const color = kind === 'audio' ? '#c084fc'
2003 : kind === 'pdf' ? '#f97316'
2004 : '#5b7fff';
2005
2006 ctx.fillStyle = '#111826';
2007 ctx.fillRect(0, 0, 160, 200);
2008 ctx.fillStyle = color;
2009 ctx.fillRect(0, 0, 160, 28);
2010 ctx.fillStyle = '#ffffff';
2011 ctx.font = 'bold 12px sans-serif';
2012 ctx.textAlign = 'center';
2013 ctx.fillText(ext, 80, 8);
2014 ctx.textAlign = 'left';
2015 ctx.fillStyle = '#9fb1d8';
2016 ctx.font = '10px sans-serif';
2017 ctx.fillText('Preview', 8, 48);
2018 ctx.fillStyle = '#dce4ff';
2019 ctx.font = 'bold 11px sans-serif';
2020 ctx.fillText(ext, 8, 66);
2021 return canvas.toDataURL('image/png');
2022 }
2023
2024 async function videoThumbnailDataUrl (blob)
2025 {
2026 const src = URL.createObjectURL(blob);
2027 try
2028 {
2029 const video = document.createElement('video');
2030 video.src = src;
2031 video.muted = true;
2032 video.preload = 'metadata';
2033
2034 await new Promise((resolve, reject) =>
2035 {
2036 const fail = () => reject(new Error('video load failed'));
2037 video.addEventListener('error', fail, { once: true });
2038 video.addEventListener('loadeddata', resolve, { once: true });
2039 });
2040
2041 const targetTime = Number.isFinite(video.duration) && video.duration > 1 ? 1 : 0;
2042 await new Promise((resolve) =>
2043 {
2044 video.currentTime = targetTime;
2045 video.addEventListener('seeked', resolve, { once: true });
2046 });
2047
2048 const canvas = document.createElement('canvas');
2049 canvas.width = 160;
2050 canvas.height = 200;
2051 const ctx = canvas.getContext('2d');
2052 if (!ctx) return null;
2053
2054 ctx.fillStyle = '#0d111a';
2055 ctx.fillRect(0, 0, canvas.width, canvas.height);
2056 const rw = video.videoWidth || 1;
2057 const rh = video.videoHeight || 1;
2058 const scale = Math.max(canvas.width / rw, canvas.height / rh);
2059 const dw = rw * scale;
2060 const dh = rh * scale;
2061 ctx.drawImage(video, (canvas.width - dw) / 2, (canvas.height - dh) / 2, dw, dh);
2062
2063 // subtle play badge
2064 ctx.fillStyle = 'rgba(0,0,0,0.45)';
2065 ctx.beginPath();
2066 ctx.arc(80, 100, 18, 0, Math.PI * 2);
2067 ctx.fill();
2068 ctx.fillStyle = '#ffffff';
2069 ctx.beginPath();
2070 ctx.moveTo(75, 90);
2071 ctx.lineTo(75, 110);
2072 ctx.lineTo(90, 100);
2073 ctx.closePath();
2074 ctx.fill();
2075
2076 return canvas.toDataURL('image/png');
2077 } catch
2078 {
2079 return null;
2080 } finally
2081 {
2082 URL.revokeObjectURL(src);
2083 }
2084 }
2085
2086 async function buildThumbnailSource (item, blob, kind)
2087 {
2088 if (kind === 'image') return URL.createObjectURL(blob);
2089 if (kind === 'video') return await videoThumbnailDataUrl(blob);
2090 if (kind === 'text') return textThumbnailDataUrl(await blob.text());
2091 return badgeThumbnailDataUrl(item.name, kind);
2092 }
2093
1911 2094 async function loadThumbnails ()
1912 2095 {
1913 2096 const icons = Array.from(document.querySelectorAll('.icon[data-preview-path]'));
@@ -1928,29 +2111,52 @@ async function loadThumbnails ()
1928 2111 async function loadOneThumbnail (iconEl)
1929 2112 {
1930 2113 if (!iconEl.isConnected || !STATE.token) return;
2114 if (iconEl.dataset.thumbReady === '1') return;
1931 2115 const filePath = iconEl.dataset.previewPath;
1932 const url = `/api/files/download?file=${encodeURIComponent(filePath)}`;
2116 const item = STATE.items.find(i => i.path === filePath);
2117 if (!item) return;
1933 2118
1934 let res;
1935 try
2119 const kind = getThumbnailKind(item.name);
2120 let srcUrl = null;
2121
2122 if (kind === 'badge' || kind === 'audio' || kind === 'pdf')
1936 2123 {
1937 res = await fetch(url, {
1938 headers: {
1939 Authorization: `Bearer ${STATE.token}`,
1940 'X-Fp': getFingerprint(),
1941 },
1942 });
1943 } catch { return; }
1944 if (!res.ok || !iconEl.isConnected) return;
2124 srcUrl = badgeThumbnailDataUrl(item.name, kind);
2125 } else
2126 {
2127 const maxBytes = maxThumbBytesForKind(kind);
2128 if (maxBytes > 0 && typeof item.size === 'number' && item.size > maxBytes)
2129 {
2130 srcUrl = badgeThumbnailDataUrl(item.name, kind);
2131 } else
2132 {
2133 const url = `/api/files/download?file=${encodeURIComponent(filePath)}`;
1945 2134
1946 const blob = await res.blob();
1947 if (!blob.type.startsWith('image/') || !iconEl.isConnected) return;
2135 let res;
2136 try
2137 {
2138 res = await fetch(url, {
2139 headers: {
2140 Authorization: `Bearer ${STATE.token}`,
2141 'X-Fp': getFingerprint(),
2142 },
2143 });
2144 } catch { return; }
2145 if (!res.ok || !iconEl.isConnected) return;
1948 2146
1949 const objectUrl = URL.createObjectURL(blob);
1950 STATE.thumbnailUrls.push(objectUrl);
2147 const blob = await res.blob();
2148 srcUrl = await buildThumbnailSource(item, blob, kind);
2149 if (kind === 'image' && srcUrl) STATE.thumbnailUrls.push(srcUrl);
2150 }
2151 }
2152 if (!srcUrl || !iconEl.isConnected) return;
1951 2153
1952 2154 const svg = iconEl.querySelector('svg');
1953 if (!svg) { URL.revokeObjectURL(objectUrl); return; }
2155 if (!svg)
2156 {
2157 if (srcUrl.startsWith('blob:')) URL.revokeObjectURL(srcUrl);
2158 return;
2159 }
1954 2160
1955 2161 const NS = 'http://www.w3.org/2000/svg';
1956 2162 const id = `fcp-${Math.random().toString(36).slice(2, 9)}`;
@@ -1967,8 +2173,8 @@ async function loadOneThumbnail (iconEl)
1967 2173
1968 2174 // Image fills the file bounding box; clip keeps it inside the outline
1969 2175 const img = document.createElementNS(NS, 'image');
1970 img.setAttribute('href', objectUrl);
1971 img.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', objectUrl);
2176 img.setAttribute('href', srcUrl);
2177 img.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', srcUrl);
1972 2178 img.setAttribute('x', '4');
1973 2179 img.setAttribute('y', '2');
1974 2180 img.setAttribute('width', '16');
@@ -1979,6 +2185,7 @@ async function loadOneThumbnail (iconEl)
1979 2185 // Prepend so existing path/polyline elements (the outline) render on top
1980 2186 svg.prepend(img);
1981 2187 svg.prepend(defs);
2188 iconEl.dataset.thumbReady = '1';
1982 2189 }
1983 2190
1984 2191 function wireEvents ()
@@ -891,7 +891,7 @@ function makeFileItem (item)
891 const icon = document.createElement('div');
891 const icon = document.createElement('div');
892 icon.className = 'icon';
892 icon.className = 'icon';
893 icon.innerHTML = item.isDir ? iconFolder() : iconFile(item.name);
893 icon.innerHTML = item.isDir ? iconFolder() : iconFile(item.name);
894 if (!item.isDir && IMAGE_EXTS.has(extOf(item.name)))
894 if (!item.isDir)
895 {
895 {
896 icon.dataset.previewPath = item.path;
896 icon.dataset.previewPath = item.path;
897 }
897 }
@@ -1908,6 +1908,189 @@ function revokeThumbnails ()
1908 STATE.thumbnailUrls = [];
1908 STATE.thumbnailUrls = [];
1909}
1909}
1910
1910
1911function getThumbnailKind (name)
1912{
1913 const ext = extOf(name || '');
1914 if (IMAGE_EXTS.has(ext)) return 'image';
1915 if (VIDEO_EXTS.has(ext)) return 'video';
1916 if (CODE_EXTS.has(ext)) return 'text';
1917 if (AUDIO_EXTS.has(ext)) return 'audio';
1918 if (ext === 'pdf') return 'pdf';
1919 return 'badge';
1920}
1921
1922function maxThumbBytesForKind (kind)
1923{
1924 if (kind === 'image') return 15 * 1024 * 1024;
1925 if (kind === 'video') return 25 * 1024 * 1024;
1926 if (kind === 'text') return 1024 * 1024;
1927 return 0;
1928}
1929
1930function drawTokenizedLine (ctx, line, x, y)
1931{
1932 const tokenRe = /(\/\/.*$|\/\*[\s\S]*?\*\/|"(?:\\.|[^"\\])*"|'(?:\\.|[^'\\])*'|`(?:\\.|[^`\\])*`|\b(?:function|return|if|else|for|while|switch|case|break|continue|const|let|var|class|new|import|from|export|default|try|catch|finally|throw|await|async|true|false|null|undefined)\b|\b\d+(?:\.\d+)?\b)/g;
1933 let last = 0;
1934 let cursor = x;
1935 for (const m of line.matchAll(tokenRe))
1936 {
1937 const i = m.index == null ? 0 : m.index;
1938 if (i > last)
1939 {
1940 const plain = line.slice(last, i);
1941 ctx.fillStyle = '#dce4ff';
1942 ctx.fillText(plain, cursor, y);
1943 cursor += ctx.measureText(plain).width;
1944 }
1945
1946 const tok = m[0];
1947 if (/^\/\//.test(tok) || /^\/\*/.test(tok)) ctx.fillStyle = '#6f7f9a';
1948 else if (/^['"`]/.test(tok)) ctx.fillStyle = '#9ee37d';
1949 else if (/^\d/.test(tok)) ctx.fillStyle = '#f8c46a';
1950 else ctx.fillStyle = '#8bb4ff';
1951
1952 ctx.fillText(tok, cursor, y);
1953 cursor += ctx.measureText(tok).width;
1954 last = i + tok.length;
1955 }
1956
1957 if (last < line.length)
1958 {
1959 const tail = line.slice(last);
1960 ctx.fillStyle = '#dce4ff';
1961 ctx.fillText(tail, cursor, y);
1962 }
1963}
1964
1965function textThumbnailDataUrl (text)
1966{
1967 const canvas = document.createElement('canvas');
1968 canvas.width = 160;
1969 canvas.height = 200;
1970 const ctx = canvas.getContext('2d');
1971 if (!ctx) return null;
1972
1973 ctx.fillStyle = '#0d111a';
1974 ctx.fillRect(0, 0, canvas.width, canvas.height);
1975 ctx.font = '8px ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace';
1976 ctx.textBaseline = 'top';
1977
1978 const lines = String(text || '')
1979 .replace(/\r\n/g, '\n')
1980 .split('\n')
1981 .slice(0, 17);
1982
1983 let y = 8;
1984 for (const line of lines)
1985 {
1986 drawTokenizedLine(ctx, line.slice(0, 42), 6, y);
1987 y += 11;
1988 if (y > 190) break;
1989 }
1990 return canvas.toDataURL('image/png');
1991}
1992
1993function badgeThumbnailDataUrl (name, kind)
1994{
1995 const canvas = document.createElement('canvas');
1996 canvas.width = 160;
1997 canvas.height = 200;
1998 const ctx = canvas.getContext('2d');
1999 if (!ctx) return null;
2000
2001 const ext = (extOf(name || '') || 'file').toUpperCase().slice(0, 5);
2002 const color = kind === 'audio' ? '#c084fc'
2003 : kind === 'pdf' ? '#f97316'
2004 : '#5b7fff';
2005
2006 ctx.fillStyle = '#111826';
2007 ctx.fillRect(0, 0, 160, 200);
2008 ctx.fillStyle = color;
2009 ctx.fillRect(0, 0, 160, 28);
2010 ctx.fillStyle = '#ffffff';
2011 ctx.font = 'bold 12px sans-serif';
2012 ctx.textAlign = 'center';
2013 ctx.fillText(ext, 80, 8);
2014 ctx.textAlign = 'left';
2015 ctx.fillStyle = '#9fb1d8';
2016 ctx.font = '10px sans-serif';
2017 ctx.fillText('Preview', 8, 48);
2018 ctx.fillStyle = '#dce4ff';
2019 ctx.font = 'bold 11px sans-serif';
2020 ctx.fillText(ext, 8, 66);
2021 return canvas.toDataURL('image/png');
2022}
2023
2024async function videoThumbnailDataUrl (blob)
2025{
2026 const src = URL.createObjectURL(blob);
2027 try
2028 {
2029 const video = document.createElement('video');
2030 video.src = src;
2031 video.muted = true;
2032 video.preload = 'metadata';
2033
2034 await new Promise((resolve, reject) =>
2035 {
2036 const fail = () => reject(new Error('video load failed'));
2037 video.addEventListener('error', fail, { once: true });
2038 video.addEventListener('loadeddata', resolve, { once: true });
2039 });
2040
2041 const targetTime = Number.isFinite(video.duration) && video.duration > 1 ? 1 : 0;
2042 await new Promise((resolve) =>
2043 {
2044 video.currentTime = targetTime;
2045 video.addEventListener('seeked', resolve, { once: true });
2046 });
2047
2048 const canvas = document.createElement('canvas');
2049 canvas.width = 160;
2050 canvas.height = 200;
2051 const ctx = canvas.getContext('2d');
2052 if (!ctx) return null;
2053
2054 ctx.fillStyle = '#0d111a';
2055 ctx.fillRect(0, 0, canvas.width, canvas.height);
2056 const rw = video.videoWidth || 1;
2057 const rh = video.videoHeight || 1;
2058 const scale = Math.max(canvas.width / rw, canvas.height / rh);
2059 const dw = rw * scale;
2060 const dh = rh * scale;
2061 ctx.drawImage(video, (canvas.width - dw) / 2, (canvas.height - dh) / 2, dw, dh);
2062
2063 // subtle play badge
2064 ctx.fillStyle = 'rgba(0,0,0,0.45)';
2065 ctx.beginPath();
2066 ctx.arc(80, 100, 18, 0, Math.PI * 2);
2067 ctx.fill();
2068 ctx.fillStyle = '#ffffff';
2069 ctx.beginPath();
2070 ctx.moveTo(75, 90);
2071 ctx.lineTo(75, 110);
2072 ctx.lineTo(90, 100);
2073 ctx.closePath();
2074 ctx.fill();
2075
2076 return canvas.toDataURL('image/png');
2077 } catch
2078 {
2079 return null;
2080 } finally
2081 {
2082 URL.revokeObjectURL(src);
2083 }
2084}
2085
2086async function buildThumbnailSource (item, blob, kind)
2087{
2088 if (kind === 'image') return URL.createObjectURL(blob);
2089 if (kind === 'video') return await videoThumbnailDataUrl(blob);
2090 if (kind === 'text') return textThumbnailDataUrl(await blob.text());
2091 return badgeThumbnailDataUrl(item.name, kind);
2092}
2093
1911async function loadThumbnails ()
2094async function loadThumbnails ()
1912{
2095{
1913 const icons = Array.from(document.querySelectorAll('.icon[data-preview-path]'));
2096 const icons = Array.from(document.querySelectorAll('.icon[data-preview-path]'));
@@ -1928,29 +2111,52 @@ async function loadThumbnails ()
1928async function loadOneThumbnail (iconEl)
2111async function loadOneThumbnail (iconEl)
1929{
2112{
1930 if (!iconEl.isConnected || !STATE.token) return;
2113 if (!iconEl.isConnected || !STATE.token) return;
2114 if (iconEl.dataset.thumbReady === '1') return;
1931 const filePath = iconEl.dataset.previewPath;
2115 const filePath = iconEl.dataset.previewPath;
1932 const url = `/api/files/download?file=${encodeURIComponent(filePath)}`;
2116 const item = STATE.items.find(i => i.path === filePath);
2117 if (!item) return;
1933
2118
1934 let res;
2119 const kind = getThumbnailKind(item.name);
1935 try
2120 let srcUrl = null;
2121
2122 if (kind === 'badge' || kind === 'audio' || kind === 'pdf')
1936 {
2123 {
1937 res = await fetch(url, {
2124 srcUrl = badgeThumbnailDataUrl(item.name, kind);
1938 headers: {
2125 } else
1939 Authorization: `Bearer ${STATE.token}`,
2126 {
1940 'X-Fp': getFingerprint(),
2127 const maxBytes = maxThumbBytesForKind(kind);
1941 },
2128 if (maxBytes > 0 && typeof item.size === 'number' && item.size > maxBytes)
1942 });
2129 {
1943 } catch { return; }
2130 srcUrl = badgeThumbnailDataUrl(item.name, kind);
1944 if (!res.ok || !iconEl.isConnected) return;
2131 } else
2132 {
2133 const url = `/api/files/download?file=${encodeURIComponent(filePath)}`;
1945
2134
1946 const blob = await res.blob();
2135 let res;
1947 if (!blob.type.startsWith('image/') || !iconEl.isConnected) return;
2136 try
2137 {
2138 res = await fetch(url, {
2139 headers: {
2140 Authorization: `Bearer ${STATE.token}`,
2141 'X-Fp': getFingerprint(),
2142 },
2143 });
2144 } catch { return; }
2145 if (!res.ok || !iconEl.isConnected) return;
1948
2146
1949 const objectUrl = URL.createObjectURL(blob);
2147 const blob = await res.blob();
1950 STATE.thumbnailUrls.push(objectUrl);
2148 srcUrl = await buildThumbnailSource(item, blob, kind);
2149 if (kind === 'image' && srcUrl) STATE.thumbnailUrls.push(srcUrl);
2150 }
2151 }
2152 if (!srcUrl || !iconEl.isConnected) return;
1951
2153
1952 const svg = iconEl.querySelector('svg');
2154 const svg = iconEl.querySelector('svg');
1953 if (!svg) { URL.revokeObjectURL(objectUrl); return; }
2155 if (!svg)
2156 {
2157 if (srcUrl.startsWith('blob:')) URL.revokeObjectURL(srcUrl);
2158 return;
2159 }
1954
2160
1955 const NS = 'http://www.w3.org/2000/svg';
2161 const NS = 'http://www.w3.org/2000/svg';
1956 const id = `fcp-${Math.random().toString(36).slice(2, 9)}`;
2162 const id = `fcp-${Math.random().toString(36).slice(2, 9)}`;
@@ -1967,8 +2173,8 @@ async function loadOneThumbnail (iconEl)
1967
2173
1968 // Image fills the file bounding box; clip keeps it inside the outline
2174 // Image fills the file bounding box; clip keeps it inside the outline
1969 const img = document.createElementNS(NS, 'image');
2175 const img = document.createElementNS(NS, 'image');
1970 img.setAttribute('href', objectUrl);
2176 img.setAttribute('href', srcUrl);
1971 img.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', objectUrl);
2177 img.setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', srcUrl);
1972 img.setAttribute('x', '4');
2178 img.setAttribute('x', '4');
1973 img.setAttribute('y', '2');
2179 img.setAttribute('y', '2');
1974 img.setAttribute('width', '16');
2180 img.setAttribute('width', '16');
@@ -1979,6 +2185,7 @@ async function loadOneThumbnail (iconEl)
1979 // Prepend so existing path/polyline elements (the outline) render on top
2185 // Prepend so existing path/polyline elements (the outline) render on top
1980 svg.prepend(img);
2186 svg.prepend(img);
1981 svg.prepend(defs);
2187 svg.prepend(defs);
2188 iconEl.dataset.thumbReady = '1';
1982}
2189}
1983
2190
1984function wireEvents ()
2191function wireEvents ()