Files
bewcloud/routes/photos.tsx
Bruno Bernardino e337859a22 Implement a more robust Config (#60)
* Implement a more robust Config

This moves the configuration variables from the `.env` file to a new `bewcloud.config.ts` file. Note that DB connection and secrets are still in the `.env` file.

This will allow for more reliable and easier personalized configurations, and was a requirement to start working on adding SSO (#13).

For now, `.env`-based config will still be allowed and respected (overriden by `bewcloud.config.ts`), but in the future I'll probably remove it (some major upgrade).

* Update deploy script to also copy the new config file
2025-05-25 15:48:53 +01:00

64 lines
2.0 KiB
TypeScript

import { Handlers, PageProps } from 'fresh/server.ts';
import { Directory, DirectoryFile, FreshContextState } from '/lib/types.ts';
import { AppConfig } 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 (!(await AppConfig.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>
);
}