* 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
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { Handlers, PageProps } from 'fresh/server.ts';
|
|
|
|
import { Directory, DirectoryFile, FreshContextState } from '/lib/types.ts';
|
|
import { DirectoryModel, FileModel } from '/lib/models/files.ts';
|
|
import { AppConfig } from '/lib/config.ts';
|
|
import FilesWrapper from '/islands/files/FilesWrapper.tsx';
|
|
|
|
interface Data {
|
|
userDirectories: Directory[];
|
|
userFiles: DirectoryFile[];
|
|
currentPath: string;
|
|
baseUrl: string;
|
|
}
|
|
|
|
export const handler: Handlers<Data, FreshContextState> = {
|
|
async GET(request, context) {
|
|
if (!context.state.user) {
|
|
return new Response('Redirect', { status: 303, headers: { 'Location': `/login` } });
|
|
}
|
|
|
|
const baseUrl = (await AppConfig.getConfig()).auth.baseUrl;
|
|
|
|
const searchParams = new URL(request.url).searchParams;
|
|
|
|
let currentPath = searchParams.get('path') || '/';
|
|
|
|
// Send invalid paths back to root
|
|
if (!currentPath.startsWith('/') || currentPath.includes('../')) {
|
|
currentPath = '/';
|
|
}
|
|
|
|
// 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);
|
|
|
|
return await context.render({ userDirectories, userFiles, currentPath, baseUrl });
|
|
},
|
|
};
|
|
|
|
export default function FilesPage({ data }: PageProps<Data, FreshContextState>) {
|
|
return (
|
|
<main>
|
|
<FilesWrapper
|
|
initialDirectories={data.userDirectories}
|
|
initialFiles={data.userFiles}
|
|
initialPath={data.currentPath}
|
|
baseUrl={data.baseUrl}
|
|
/>
|
|
</main>
|
|
);
|
|
}
|