BlueGit

timfa filemanager

Add image thumbnail previews inside file icons

timfa Tim Falken · pushed by timfa

Authored 2026-05-03 · 6 months ago

Hash
787f405340694073f453187035ed416386fd7bc5
Parent
d000f83c
1 file changed +78 −0
public/app.js +78 −0
@@ -41,6 +41,8 @@ const STATE = {
41 41 // Background refresh state
42 42 autoRefreshTimer: null,
43 43 uploadInProgress: false,
44 // Blob URLs for thumbnail previews — revoked on each grid rebuild
45 thumbnailUrls: [],
44 46 };
45 47
46 48 const SESSION_KEY = 'filemanager.session.v1';
@@ -855,6 +857,7 @@ function stopAutoRefresh ()
855 857
856 858 function renderFileGrid ()
857 859 {
860 revokeThumbnails();
858 861 const grid = document.getElementById('file-grid');
859 862 grid.innerHTML = '';
860 863 const empty = document.getElementById('empty-notice');
@@ -871,6 +874,8 @@ function renderFileGrid ()
871 874 {
872 875 grid.appendChild(makeFileItem(item));
873 876 }
877
878 loadThumbnails();
874 879 }
875 880
876 881 function makeFileItem (item)
@@ -882,6 +887,9 @@ function makeFileItem (item)
882 887 const icon = document.createElement('div');
883 888 icon.className = 'icon';
884 889 icon.innerHTML = item.isDir ? iconFolder() : iconFile(item.name);
890 if (!item.isDir && IMAGE_EXTS.has(extOf(item.name))) {
891 icon.dataset.previewPath = item.path;
892 }
885 893 el.appendChild(icon);
886 894
887 895 const name = document.createElement('div');
@@ -1631,6 +1639,76 @@ function iconFile (name, size = 52)
1631 1639 // EVENT WIRING
1632 1640 // ───────────────────────────────────────────────────────────────────────────
1633 1641
1642 // ───────────────────────────────────────────────────────────────────────────
1643 // THUMBNAIL PREVIEWS
1644 // ───────────────────────────────────────────────────────────────────────────
1645
1646 function revokeThumbnails () {
1647 STATE.thumbnailUrls.forEach(u => URL.revokeObjectURL(u));
1648 STATE.thumbnailUrls = [];
1649 }
1650
1651 async function loadThumbnails () {
1652 const icons = Array.from(document.querySelectorAll('.icon[data-preview-path]'));
1653 if (!icons.length) return;
1654
1655 const queue = [...icons];
1656 async function worker () {
1657 while (queue.length) {
1658 await loadOneThumbnail(queue.shift());
1659 }
1660 }
1661 // Four concurrent fetches — keeps the Pi's network from flooding
1662 await Promise.all(Array.from({ length: Math.min(4, icons.length) }, worker));
1663 }
1664
1665 async function loadOneThumbnail (iconEl) {
1666 if (!iconEl.isConnected || !STATE.token) return;
1667 const filePath = iconEl.dataset.previewPath;
1668 const url = `/api/files/download?file=${encodeURIComponent(filePath)}`;
1669
1670 let res;
1671 try {
1672 res = await fetch(url, { headers: { Authorization: `Bearer ${STATE.token}` } });
1673 } catch { return; }
1674 if (!res.ok || !iconEl.isConnected) return;
1675
1676 const blob = await res.blob();
1677 if (!blob.type.startsWith('image/') || !iconEl.isConnected) return;
1678
1679 const objectUrl = URL.createObjectURL(blob);
1680 STATE.thumbnailUrls.push(objectUrl);
1681
1682 const svg = iconEl.querySelector('svg');
1683 if (!svg) { URL.revokeObjectURL(objectUrl); return; }
1684
1685 const NS = 'http://www.w3.org/2000/svg';
1686 const id = `fcp-${Math.random().toString(36).slice(2, 9)}`;
1687
1688 // clipPath matches the main file body outline exactly (viewBox 0 0 24 24)
1689 const defs = document.createElementNS(NS, 'defs');
1690 const clipPath = document.createElementNS(NS, 'clipPath');
1691 clipPath.setAttribute('id', id);
1692 const clipShape = document.createElementNS(NS, 'path');
1693 clipShape.setAttribute('d', 'M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z');
1694 clipPath.appendChild(clipShape);
1695 defs.appendChild(clipPath);
1696
1697 // Image fills the file bounding box; clip keeps it inside the outline
1698 const img = document.createElementNS(NS, 'image');
1699 img.setAttribute('href', objectUrl);
1700 img.setAttribute('x', '4');
1701 img.setAttribute('y', '2');
1702 img.setAttribute('width', '16');
1703 img.setAttribute('height', '20');
1704 img.setAttribute('clip-path', `url(#${id})`);
1705 img.setAttribute('preserveAspectRatio', 'xMidYMid slice');
1706
1707 // Prepend so existing path/polyline elements (the outline) render on top
1708 svg.prepend(img);
1709 svg.prepend(defs);
1710 }
1711
1634 1712 function wireEvents ()
1635 1713 {
1636 1714 // Auth form
@@ -41,6 +41,8 @@ const STATE = {
41 // Background refresh state
41 // Background refresh state
42 autoRefreshTimer: null,
42 autoRefreshTimer: null,
43 uploadInProgress: false,
43 uploadInProgress: false,
44 // Blob URLs for thumbnail previews — revoked on each grid rebuild
45 thumbnailUrls: [],
44};
46};
45
47
46const SESSION_KEY = 'filemanager.session.v1';
48const SESSION_KEY = 'filemanager.session.v1';
@@ -855,6 +857,7 @@ function stopAutoRefresh ()
855
857
856function renderFileGrid ()
858function renderFileGrid ()
857{
859{
860 revokeThumbnails();
858 const grid = document.getElementById('file-grid');
861 const grid = document.getElementById('file-grid');
859 grid.innerHTML = '';
862 grid.innerHTML = '';
860 const empty = document.getElementById('empty-notice');
863 const empty = document.getElementById('empty-notice');
@@ -871,6 +874,8 @@ function renderFileGrid ()
871 {
874 {
872 grid.appendChild(makeFileItem(item));
875 grid.appendChild(makeFileItem(item));
873 }
876 }
877
878 loadThumbnails();
874}
879}
875
880
876function makeFileItem (item)
881function makeFileItem (item)
@@ -882,6 +887,9 @@ function makeFileItem (item)
882 const icon = document.createElement('div');
887 const icon = document.createElement('div');
883 icon.className = 'icon';
888 icon.className = 'icon';
884 icon.innerHTML = item.isDir ? iconFolder() : iconFile(item.name);
889 icon.innerHTML = item.isDir ? iconFolder() : iconFile(item.name);
890 if (!item.isDir && IMAGE_EXTS.has(extOf(item.name))) {
891 icon.dataset.previewPath = item.path;
892 }
885 el.appendChild(icon);
893 el.appendChild(icon);
886
894
887 const name = document.createElement('div');
895 const name = document.createElement('div');
@@ -1631,6 +1639,76 @@ function iconFile (name, size = 52)
1631// EVENT WIRING
1639// EVENT WIRING
1632// ───────────────────────────────────────────────────────────────────────────
1640// ───────────────────────────────────────────────────────────────────────────
1633
1641
1642// ───────────────────────────────────────────────────────────────────────────
1643// THUMBNAIL PREVIEWS
1644// ───────────────────────────────────────────────────────────────────────────
1645
1646function revokeThumbnails () {
1647 STATE.thumbnailUrls.forEach(u => URL.revokeObjectURL(u));
1648 STATE.thumbnailUrls = [];
1649}
1650
1651async function loadThumbnails () {
1652 const icons = Array.from(document.querySelectorAll('.icon[data-preview-path]'));
1653 if (!icons.length) return;
1654
1655 const queue = [...icons];
1656 async function worker () {
1657 while (queue.length) {
1658 await loadOneThumbnail(queue.shift());
1659 }
1660 }
1661 // Four concurrent fetches — keeps the Pi's network from flooding
1662 await Promise.all(Array.from({ length: Math.min(4, icons.length) }, worker));
1663}
1664
1665async function loadOneThumbnail (iconEl) {
1666 if (!iconEl.isConnected || !STATE.token) return;
1667 const filePath = iconEl.dataset.previewPath;
1668 const url = `/api/files/download?file=${encodeURIComponent(filePath)}`;
1669
1670 let res;
1671 try {
1672 res = await fetch(url, { headers: { Authorization: `Bearer ${STATE.token}` } });
1673 } catch { return; }
1674 if (!res.ok || !iconEl.isConnected) return;
1675
1676 const blob = await res.blob();
1677 if (!blob.type.startsWith('image/') || !iconEl.isConnected) return;
1678
1679 const objectUrl = URL.createObjectURL(blob);
1680 STATE.thumbnailUrls.push(objectUrl);
1681
1682 const svg = iconEl.querySelector('svg');
1683 if (!svg) { URL.revokeObjectURL(objectUrl); return; }
1684
1685 const NS = 'http://www.w3.org/2000/svg';
1686 const id = `fcp-${Math.random().toString(36).slice(2, 9)}`;
1687
1688 // clipPath matches the main file body outline exactly (viewBox 0 0 24 24)
1689 const defs = document.createElementNS(NS, 'defs');
1690 const clipPath = document.createElementNS(NS, 'clipPath');
1691 clipPath.setAttribute('id', id);
1692 const clipShape = document.createElementNS(NS, 'path');
1693 clipShape.setAttribute('d', 'M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z');
1694 clipPath.appendChild(clipShape);
1695 defs.appendChild(clipPath);
1696
1697 // Image fills the file bounding box; clip keeps it inside the outline
1698 const img = document.createElementNS(NS, 'image');
1699 img.setAttribute('href', objectUrl);
1700 img.setAttribute('x', '4');
1701 img.setAttribute('y', '2');
1702 img.setAttribute('width', '16');
1703 img.setAttribute('height', '20');
1704 img.setAttribute('clip-path', `url(#${id})`);
1705 img.setAttribute('preserveAspectRatio', 'xMidYMid slice');
1706
1707 // Prepend so existing path/polyline elements (the outline) render on top
1708 svg.prepend(img);
1709 svg.prepend(defs);
1710}
1711
1634function wireEvents ()
1712function wireEvents ()
1635{
1713{
1636 // Auth form
1714 // Auth form