({ request, user, match, session, isRunningLocally }: RequestHandlerParams)
| 9 | import { SortColumn, sortDirectories, sortFiles, SortOrder } from '/public/ts/utils/files.ts'; |
| 10 | |
| 11 | async function get({ request, user, match, session, isRunningLocally }: RequestHandlerParams) { |
| 12 | const baseUrl = (await AppConfig.getConfig()).auth.baseUrl; |
| 13 | |
| 14 | if (!(await AppConfig.isAppEnabled('files'))) { |
| 15 | return new Response('Redirect', { status: 303, headers: { 'Location': `/` } }); |
| 16 | } |
| 17 | |
| 18 | const searchParams = new URL(request.url).searchParams; |
| 19 | |
| 20 | let currentPath = searchParams.get('path') || '/'; |
| 21 | |
| 22 | // Send invalid paths back to root |
| 23 | if (!currentPath.startsWith('/') || currentPath.includes('../')) { |
| 24 | currentPath = '/'; |
| 25 | } |
| 26 | |
| 27 | // Always append a trailing slash |
| 28 | if (!currentPath.endsWith('/')) { |
| 29 | currentPath = `${currentPath}/`; |
| 30 | } |
| 31 | |
| 32 | const validSortColumns = ['name', 'updated_at', 'size_in_bytes']; |
| 33 | const validSortOrders = ['asc', 'desc']; |
| 34 | |
| 35 | const savedSorting = user!.extra.file_sorting; |
| 36 | const urlSortBy = searchParams.get('sortBy'); |
| 37 | const urlSortOrder = searchParams.get('sortOrder'); |
| 38 | |
| 39 | const initialSortBy = |
| 40 | ((urlSortBy && validSortColumns.includes(urlSortBy)) |
| 41 | ? urlSortBy |
| 42 | : (savedSorting?.sort_by && validSortColumns.includes(savedSorting.sort_by)) |
| 43 | ? savedSorting.sort_by |
| 44 | : 'name') as SortColumn; |
| 45 | |
| 46 | const initialSortOrder = |
| 47 | ((urlSortOrder && validSortOrders.includes(urlSortOrder)) |
| 48 | ? urlSortOrder |
| 49 | : (savedSorting?.sort_order && validSortOrders.includes(savedSorting.sort_order)) |
| 50 | ? savedSorting.sort_order |
| 51 | : 'asc') as SortOrder; |
| 52 | |
| 53 | let userDirectories = await DirectoryModel.list(user!.id, currentPath); |
| 54 | |
| 55 | let userFiles = await FileModel.list(user!.id, currentPath); |
| 56 | |
| 57 | const sortOptions = { sortBy: initialSortBy, sortOrder: initialSortOrder }; |
| 58 | userDirectories = sortDirectories(userDirectories, sortOptions); |
| 59 | userFiles = sortFiles(userFiles, sortOptions); |
| 60 | |
| 61 | const isPublicFileSharingAllowed = await AppConfig.isPublicFileSharingAllowed(); |
| 62 | const areDirectoryDownloadsAllowed = await AppConfig.areDirectoryDownloadsAllowed(); |
| 63 | |
| 64 | const htmlContent = defaultHtmlContent({ |
| 65 | userDirectories, |
| 66 | userFiles, |
| 67 | currentPath, |
| 68 | baseUrl, |
nothing calls this directly
no test coverage detected