Files
bewcloud/routes/photos.tsx
Bruno Bernardino 6cfb62d1a2 Refactor data handlers + misc fixes
This refactors the data handlers into a more standard/understood model-like architecture, to prepare for a new, more robust config system.

It also fixes a problem with creating new Notes and uploading new Photos via the web interface (related to #58).

Finally, it speeds up docker builds by sending in less files, which aren't necessary or will be built anyway.

This is all in preparation to allow building #13 more robustly.
2025-05-24 08:24:10 +01:00

64 lines
1.9 KiB
TypeScript

import { Handlers, PageProps } from 'fresh/server.ts';
import { Directory, DirectoryFile, FreshContextState } from '/lib/types.ts';
import { isAppEnabled } from '/lib/config.ts';
import { DirectoryModel, FileModel } from '/lib/models/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<Data, FreshContextState> = {
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 DirectoryModel.list(context.state.user.id, currentPath);
const userFiles = await FileModel.list(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<Data, FreshContextState>) {
return (
<main>
<PhotosWrapper
initialDirectories={data.userDirectories}
initialFiles={data.userPhotos}
initialPath={data.currentPath}
/>
</main>
);
}