Dynamic file sizes
This commit is contained in:
1 parent
2814a287fb
commit
4da454819d
10 files changed
+35
-18
No files matched your search
@@ -2,9 +2,6 @@
|
||||
# Ignore the heavy PDFs during build
|
||||
public/files/*
|
||||
|
||||
# (Optional) Keep a placeholder file so the directory structure isn't completely lost locally
|
||||
!public/files/.gitkeep
|
||||
|
||||
# Dependency directories
|
||||
node_modules
|
||||
npm-debug.log
|
||||
|
||||
+6
-12
@@ -3,38 +3,32 @@
|
||||
{
|
||||
"title": "Turistkarta",
|
||||
"icon": "Map",
|
||||
"file": "/files/Turistkarta - Kullaberg.pdf",
|
||||
"size": "2.51 MB"
|
||||
"file": "/files/Turistkarta - Kullaberg.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Orienteringskarta",
|
||||
"icon": "MapPlus",
|
||||
"file": "/files/Orienteringskarta - Kullaberg.pdf",
|
||||
"size": "3.74 MB"
|
||||
"file": "/files/Orienteringskarta - Kullaberg.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Badplatser att besöka",
|
||||
"icon": "WavesLadder",
|
||||
"file": "/files/Badplatser på Kullaberg.pdf",
|
||||
"size": "1.88 MB"
|
||||
"file": "/files/Badplatser på Kullaberg.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Vandringsrutter",
|
||||
"icon": "MapPinned",
|
||||
"file": "/files/Vandringsrutter.pdf",
|
||||
"size": "4.42 MB"
|
||||
"file": "/files/Vandringsrutter.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Underlag för guidediplomering",
|
||||
"icon": "BookCopy",
|
||||
"file": "/files/Underlag för guidediplomering.pdf",
|
||||
"size": "3.18 MB"
|
||||
"file": "/files/Underlag för guidediplomering.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Destinationskunskap Kullahalvön",
|
||||
"icon": "BookCopy",
|
||||
"file": "/files/Destinationskunskap.pdf",
|
||||
"size": "1.97 MB"
|
||||
"file": "/files/Destinationskunskap.pdf"
|
||||
}
|
||||
],
|
||||
"links": [
|
||||
|
||||
+29
-3
@@ -18,6 +18,20 @@ const IconMap: Record<string, any> = {
|
||||
"FileText": FileText
|
||||
};
|
||||
|
||||
const fetchFileSize = async (url: string) => {
|
||||
try {
|
||||
const res = await fetch(url, { method: 'HEAD' });
|
||||
const bytes = res.headers.get('content-length');
|
||||
if (bytes) {
|
||||
const mb = (parseInt(bytes) / (1024 * 1024)).toFixed(2);
|
||||
return `${mb} MB`;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Kunde inte hämta filstorlek för", url);
|
||||
}
|
||||
return "Okänd";
|
||||
};
|
||||
|
||||
export default function Documents() {
|
||||
const [isHydrated, setIsHydrated] = useState(false);
|
||||
const [isOffline, setIsOffline] = useState(false);
|
||||
@@ -48,10 +62,22 @@ export default function Documents() {
|
||||
if (navigator.onLine) {
|
||||
const res = await readJsonFile('documents.json');
|
||||
if (res.success && res.data) {
|
||||
setDocs(res.data.docs || []);
|
||||
const docsWithSizes = await Promise.all(
|
||||
(res.data.docs || []).map(async (doc: DocumentItem) => {
|
||||
if (!doc.size || doc.size === "") {
|
||||
const calculatedSize = await fetchFileSize(doc.file);
|
||||
return { ...doc, size: calculatedSize };
|
||||
}
|
||||
return doc;
|
||||
})
|
||||
);
|
||||
|
||||
setDocs(docsWithSizes);
|
||||
setLinks(res.data.links || []);
|
||||
localStorage.setItem('kullaberg_documents_cache', JSON.stringify(res.data));
|
||||
checkCacheStatus(res.data.docs || []);
|
||||
const dataToCache = { ...res.data, docs: docsWithSizes };
|
||||
localStorage.setItem('kullaberg_documents_cache', JSON.stringify(dataToCache));
|
||||
|
||||
checkCacheStatus(docsWithSizes);
|
||||
}
|
||||
} else {
|
||||
const cachedData = localStorage.getItem('kullaberg_documents_cache');
|
||||
|
||||
Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user