import { Handlers, PageProps } from 'fresh/server.ts'; import { Directory, DirectoryFile, FreshContextState } from '/lib/types.ts'; import { isAppEnabled } from '/lib/config.ts'; import { getDirectories, getFiles } from '/lib/data/files.ts'; import { PHOTO_EXTENSIONS } from '/lib/utils/photos.ts'; import PhotosWrapper from '/islands/photos/PhotosWrapper.tsx'; interface Data { userDirectories: Directory[]; userPhotos: DirectoryFile[]; currentPath: string; } export const handler: Handlers = { async GET(request, context) { if (!context.state.user) { return new Response('Redirect', { status: 303, headers: { 'Location': `/login` } }); } if (!isAppEnabled('photos')) { return new Response('Redirect', { status: 303, headers: { 'Location': `/files` } }); } const searchParams = new URL(request.url).searchParams; let currentPath = searchParams.get('path') || '/Photos/'; // Send invalid paths back to Photos root if (!currentPath.startsWith('/Photos/') || currentPath.includes('../')) { currentPath = '/Photos/'; } // Always append a trailing slash if (!currentPath.endsWith('/')) { currentPath = `${currentPath}/`; } const userDirectories = await getDirectories(context.state.user.id, currentPath); const userFiles = await getFiles(context.state.user.id, currentPath); const userPhotos = userFiles.filter((file) => { const lowercaseFileName = file.file_name.toLowerCase(); return PHOTO_EXTENSIONS.some((extension) => lowercaseFileName.endsWith(extension)); }); return await context.render({ userDirectories, userPhotos, currentPath }); }, }; export default function PhotosPage({ data }: PageProps) { return (
); }