Compare commits
@@ -0,0 +1,37 @@
|
||||
// app/actions/files.ts
|
||||
'use server';
|
||||
|
||||
import crypto from 'crypto';
|
||||
import fs from 'fs';
|
||||
import fsPromises from 'fs/promises';
|
||||
import path from 'path';
|
||||
|
||||
const isProd = process.env.NODE_ENV === "production";
|
||||
const DATA_DIR = isProd ? "/app/data" : path.join(process.cwd(), "app/data");
|
||||
const FILES_DIR = path.join(DATA_DIR, "files");
|
||||
|
||||
const generateFileHash = (filePath: string): Promise<string> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const hash = crypto.createHash('md5');
|
||||
const stream = fs.createReadStream(filePath);
|
||||
|
||||
stream.on('error', (err) => reject(err));
|
||||
stream.on('data', (chunk) => hash.update(chunk));
|
||||
stream.on('end', () => resolve(hash.digest('hex').substring(0, 8)));
|
||||
});
|
||||
};
|
||||
|
||||
export async function getLocalFileMeta(fileUrl: string) {
|
||||
try {
|
||||
const cleanName = decodeURIComponent(fileUrl.split('/').pop() || '');
|
||||
const fullPath = path.join(FILES_DIR, cleanName);
|
||||
const stats = await fsPromises.stat(fullPath);
|
||||
const sizeMb = (stats.size / (1024 * 1024)).toFixed(2) + ' MB';
|
||||
const fileHash = await generateFileHash(fullPath);
|
||||
|
||||
return { size: sizeMb, version: fileHash };
|
||||
} catch (error) {
|
||||
console.error(`Kunde inte läsa filen (actions): ${fileUrl}`, error);
|
||||
return { size: "Okänd", version: "v1" };
|
||||
}
|
||||
}
|
||||
+54
-5
@@ -19,27 +19,76 @@ export const NoticeTab = ({ isOffline }: { isOffline: boolean }) => {
|
||||
const [saveStatus, setSaveStatus] = useState<'idle' | 'success' | 'error'>('idle');
|
||||
|
||||
useEffect(() => {
|
||||
let isMounted = true;
|
||||
|
||||
const fetchNotice = async () => {
|
||||
const res = await readJsonFile('notice.json');
|
||||
if (res.success && res.data) {
|
||||
setNotice(res.data);
|
||||
}
|
||||
const cached = localStorage.getItem('admin_notice_cache');
|
||||
if (cached) {
|
||||
setNotice(JSON.parse(cached));
|
||||
setIsLoading(false);
|
||||
}
|
||||
|
||||
const fetchWithTimeout = new Promise<any>((resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new Error('Timeout')), 5000);
|
||||
readJsonFile('notice.json').then(res => {
|
||||
clearTimeout(timer);
|
||||
resolve(res);
|
||||
}).catch(err => {
|
||||
clearTimeout(timer);
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
try {
|
||||
if (navigator.onLine) {
|
||||
const res = await fetchWithTimeout;
|
||||
if (res.success && res.data && isMounted) {
|
||||
setNotice(res.data);
|
||||
localStorage.setItem('admin_notice_cache', JSON.stringify(res.data));
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("Kunde inte hämta färsk notice.json (Liar-Fi), använder cache.");
|
||||
} finally {
|
||||
if (isMounted) setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
fetchNotice();
|
||||
return () => { isMounted = false; };
|
||||
}, []);
|
||||
|
||||
const handleSave = async () => {
|
||||
setIsSaving(true);
|
||||
setSaveStatus('idle');
|
||||
const res = await writeJsonFile('notice.json', notice);
|
||||
|
||||
try {
|
||||
const saveWithTimeout = new Promise<any>((resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new Error('Timeout')), 8000);
|
||||
writeJsonFile('notice.json', notice).then(res => {
|
||||
clearTimeout(timer);
|
||||
resolve(res);
|
||||
}).catch(err => {
|
||||
clearTimeout(timer);
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
const res = await saveWithTimeout;
|
||||
|
||||
if (res.success) {
|
||||
setSaveStatus('success');
|
||||
localStorage.setItem('admin_notice_cache', JSON.stringify(notice));
|
||||
setTimeout(() => setSaveStatus('idle'), 3000);
|
||||
} else {
|
||||
setSaveStatus('error');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Liar-Fi: Sparande tog för lång tid", error);
|
||||
setSaveStatus('error');
|
||||
} finally {
|
||||
setIsSaving(false);
|
||||
}
|
||||
};
|
||||
|
||||
if (isLoading) return <div className="flex justify-center py-10"><Loader2 className="animate-spin text-slate-teal" size={32} /></div>;
|
||||
|
||||
+20
-6
@@ -3,32 +3,42 @@
|
||||
{
|
||||
"title": "Turistkarta",
|
||||
"icon": "Map",
|
||||
"file": "/files/Turistkarta - Kullaberg.pdf"
|
||||
"file": "Turistkarta - Kullaberg.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Orienteringskarta",
|
||||
"icon": "MapPlus",
|
||||
"file": "/files/Orienteringskarta - Kullaberg.pdf"
|
||||
"file": "Orienteringskarta - Kullaberg.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Badplatser att besöka",
|
||||
"icon": "WavesLadder",
|
||||
"file": "/files/Badplatser på Kullaberg.pdf"
|
||||
"file": "Badplatser på Kullaberg.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Vandringsrutter",
|
||||
"icon": "MapPinned",
|
||||
"file": "/files/Vandringsrutter.pdf"
|
||||
"file": "Vandringsrutter.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Underlag för guidediplomering",
|
||||
"icon": "BookCopy",
|
||||
"file": "/files/Underlag för guidediplomering.pdf"
|
||||
"file": "Underlag för guidediplomering.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Destinationskunskap Kullahalvön",
|
||||
"icon": "BookCopy",
|
||||
"file": "/files/Destinationskunskap.pdf"
|
||||
"file": "Destinationskunskap.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Tjänstgöringsrapport",
|
||||
"icon": "BookCopy",
|
||||
"file": "Tjänstgöringsrapport 2026.pdf"
|
||||
},
|
||||
{
|
||||
"title": "Information om ditt sommarjobb",
|
||||
"icon": "BookCopy",
|
||||
"file": "Information om ditt sommarjobb 2026.pdf"
|
||||
}
|
||||
],
|
||||
"links": [
|
||||
@@ -48,6 +58,10 @@
|
||||
"title": "Skåneleden",
|
||||
"url": "https://www.skaneleden.se/en"
|
||||
},
|
||||
{
|
||||
"title": "RSNV Brandriskprognos",
|
||||
"url": "https://rsnv.se/brandriskprognos/"
|
||||
},
|
||||
{
|
||||
"title": "Väderprognos Mölle",
|
||||
"url": "https://www.smhi.se/vader/prognoser-och-varningar/vaderprognos/q/H%C3%B6gan%C3%A4s/M%C3%B6lle/2691501"
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
+15
-23
@@ -8,6 +8,7 @@ import { readJsonFile } from '../actions/jsonEditor';
|
||||
import { DocumentCard, DocumentItem, ExternalLinkCard, LinkItem } from '../components/ui/Cards';
|
||||
import { OfflineBadge } from '../components/ui/OfflineBadge';
|
||||
import { PageHeader } from '../components/ui/PageHeader';
|
||||
import { getLocalFileMeta } from '../actions/files';
|
||||
|
||||
const IconMap: Record<string, any> = {
|
||||
"Map": Map,
|
||||
@@ -18,20 +19,6 @@ 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);
|
||||
@@ -62,22 +49,27 @@ export default function Documents() {
|
||||
if (navigator.onLine) {
|
||||
const res = await readJsonFile('documents.json');
|
||||
if (res.success && res.data) {
|
||||
const docsWithSizes = await Promise.all(
|
||||
const docsWithMeta = 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;
|
||||
const meta = await getLocalFileMeta(doc.file);
|
||||
const fileNameOnly = doc.file.split('/').pop();
|
||||
const versionedUrl = `/files/${fileNameOnly}?v=${meta.version}`;
|
||||
|
||||
return {
|
||||
...doc,
|
||||
size: (!doc.size || doc.size === "Okänd") ? meta.size : doc.size,
|
||||
originalFile: doc.file,
|
||||
file: versionedUrl
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
setDocs(docsWithSizes);
|
||||
setDocs(docsWithMeta);
|
||||
setLinks(res.data.links || []);
|
||||
const dataToCache = { ...res.data, docs: docsWithSizes };
|
||||
const dataToCache = { ...res.data, docs: docsWithMeta };
|
||||
localStorage.setItem('kullaberg_documents_cache', JSON.stringify(dataToCache));
|
||||
|
||||
checkCacheStatus(docsWithSizes);
|
||||
checkCacheStatus(docsWithMeta);
|
||||
}
|
||||
} else {
|
||||
const cachedData = localStorage.getItem('kullaberg_documents_cache');
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
// app/files/[...slug]/route.ts
|
||||
|
||||
import fs from 'fs';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import path from 'path';
|
||||
|
||||
const isProd = process.env.NODE_ENV === "production";
|
||||
const DATA_DIR = isProd ? "/app/data" : path.join(process.cwd(), "app/data");
|
||||
const FILES_DIR = path.join(DATA_DIR, "files");
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ slug: string[] }> }
|
||||
) {
|
||||
try {
|
||||
const resolvedParams = await params;
|
||||
const filename = decodeURIComponent(resolvedParams.slug[resolvedParams.slug.length - 1]);
|
||||
const filePath = path.join(FILES_DIR, filename);
|
||||
|
||||
if (!fs.existsSync(filePath)) {
|
||||
console.error(`404 - Filen finns inte på disk: ${filePath}`);
|
||||
return new NextResponse('Filen hittades inte', { status: 404 });
|
||||
}
|
||||
|
||||
const fileBuffer = fs.readFileSync(filePath);
|
||||
|
||||
let contentType = 'application/pdf';
|
||||
if (filename.toLowerCase().endsWith('.png')) contentType = 'image/png';
|
||||
else if (filename.toLowerCase().endsWith('.jpg') || filename.toLowerCase().endsWith('.jpeg')) contentType = 'image/jpeg';
|
||||
else if (filename.toLowerCase().endsWith('.doc') || filename.toLowerCase().endsWith('.docx')) contentType = 'application/msword';
|
||||
|
||||
return new NextResponse(fileBuffer, {
|
||||
headers: {
|
||||
'Content-Type': contentType,
|
||||
'Content-Disposition': `inline; filename="${filename}"`
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Fel vid fildelning:", error);
|
||||
return new NextResponse('Ett internt serverfel uppstod', { status: 500 });
|
||||
}
|
||||
}
|
||||
+36
-28
@@ -26,20 +26,43 @@ export default function Home() {
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
|
||||
useEffect(() => {
|
||||
let isMounted = true;
|
||||
|
||||
const loadNotice = async () => {
|
||||
const cached = localStorage.getItem('kullaberg_notice_cache');
|
||||
if (cached) {
|
||||
setNotice(JSON.parse(cached));
|
||||
setIsLoading(false);
|
||||
}
|
||||
|
||||
const fetchWithTimeout = new Promise<any>((resolve, reject) => {
|
||||
const timer = setTimeout(() => reject(new Error('Timeout')), 5000);
|
||||
readJsonFile('notice.json').then(res => {
|
||||
clearTimeout(timer);
|
||||
resolve(res);
|
||||
}).catch(err => {
|
||||
clearTimeout(timer);
|
||||
reject(err);
|
||||
});
|
||||
});
|
||||
|
||||
try {
|
||||
if (navigator.onLine) {
|
||||
const res = await readJsonFile('notice.json');
|
||||
if (res.success && res.data) {
|
||||
const res = await fetchWithTimeout;
|
||||
if (res.success && res.data && isMounted) {
|
||||
setNotice(res.data);
|
||||
localStorage.setItem('kullaberg_notice_cache', JSON.stringify(res.data));
|
||||
}
|
||||
} else {
|
||||
const cached = localStorage.getItem('kullaberg_notice_cache');
|
||||
if (cached) setNotice(JSON.parse(cached));
|
||||
}
|
||||
setIsLoading(false);
|
||||
} catch (error) {
|
||||
console.warn("Kunde inte hämta nytt meddelande (Liar-Fi), behåller cache.");
|
||||
} finally {
|
||||
if (isMounted) setIsLoading(false);
|
||||
}
|
||||
};
|
||||
|
||||
loadNotice();
|
||||
return () => { isMounted = false; };
|
||||
}, []);
|
||||
|
||||
const nConfig = notice ? getNoticeConfig(notice.type) : null;
|
||||
@@ -66,32 +89,17 @@ export default function Home() {
|
||||
)
|
||||
)}
|
||||
|
||||
{/* NÖDKNAPPEN - Ligger utanför griddet så den alltid är fullbredd och i fokus */}
|
||||
<EmergencyButton
|
||||
href="/emergency"
|
||||
title="Nödsituation"
|
||||
/>
|
||||
{/* NÖDKNAPPEN */}
|
||||
<EmergencyButton href="/emergency" title="Nödsituation" />
|
||||
|
||||
{/* Quick Action Cards now use the glass style */}
|
||||
{/* Quick Action Cards */}
|
||||
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
|
||||
<ActionLinkCard
|
||||
href="/schedule"
|
||||
title="Ditt Schema"
|
||||
description="Kolla dina arbetspass och dagliga rundor snabbt."
|
||||
/>
|
||||
|
||||
<ActionLinkCard
|
||||
href="/faq"
|
||||
title="Vanliga Frågor"
|
||||
description="Snabba svar på turisternas vanligaste funderingar."
|
||||
/>
|
||||
<ActionLinkCard href="/schedule" title="Ditt Schema" description="Kolla dina arbetspass och dagliga rundor snabbt." />
|
||||
<ActionLinkCard href="/faq" title="Vanliga Frågor" description="Snabba svar på turisternas vanligaste funderingar." />
|
||||
</div>
|
||||
|
||||
{/* Contact Card updated to 'glass' variant */}
|
||||
<SectionCard
|
||||
title="Snabbkontakt"
|
||||
icon={PhoneCall}
|
||||
>
|
||||
{/* Contact Card */}
|
||||
<SectionCard title="Snabbkontakt" icon={PhoneCall}>
|
||||
<div className="space-y-4 pt-2">
|
||||
<div className="flex flex-col md:flex-row md:justify-between md:items-center pb-4 border-b border-white gap-2">
|
||||
<span className="text-sm font-bold text-ebony">William Söderberg</span>
|
||||
|
||||
@@ -23,6 +23,7 @@ const nextConfig: NextConfig = {
|
||||
output: "standalone",
|
||||
allowedDevOrigins: [
|
||||
'10.10.0.121',
|
||||
'10.11.0.122',
|
||||
'localhost',
|
||||
],
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user