From 670845ed13e6ec279dcb5b22fc76014795c12058 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Fri, 12 Apr 2024 06:59:51 +0100 Subject: [PATCH] Add new API endpoint to get a list of files for a given directory. --- README.md | 3 +-- fresh.gen.ts | 2 ++ routes/api/files/get.tsx | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 routes/api/files/get.tsx diff --git a/README.md b/README.md index c224008..4c572f9 100644 --- a/README.md +++ b/README.md @@ -67,13 +67,12 @@ Just push to the `main` branch. - [x] Files UI - [x] WebDav Server - [x] [Desktop app for selective file sync](https://github.com/bewcloud/bewcloud-desktop/releases) (`rclone` via WebDav) -- [ ] Mobile app for offline file view (WebDav client) +- [ ] Mobile app for offline file view (API + WebDav client) - [ ] Add photo auto-uplod support for mobile client - [ ] Add notes view support for mobile app - [ ] Add notes edit support for mobile app - [ ] Notes UI - [ ] Photos UI -- [ ] Address `TODO:`s in code ## Where's Contacts/Calendar (CardDav/CalDav)?! Wasn't this supposed to be a core Nextcloud replacement? diff --git a/fresh.gen.ts b/fresh.gen.ts index b40514b..3af51c0 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -11,6 +11,7 @@ import * as $api_files_create_directory from './routes/api/files/create-director import * as $api_files_delete_directory from './routes/api/files/delete-directory.tsx'; import * as $api_files_delete from './routes/api/files/delete.tsx'; import * as $api_files_get_directories from './routes/api/files/get-directories.tsx'; +import * as $api_files_get from './routes/api/files/get.tsx'; import * as $api_files_move_directory from './routes/api/files/move-directory.tsx'; import * as $api_files_move from './routes/api/files/move.tsx'; import * as $api_files_rename_directory from './routes/api/files/rename-directory.tsx'; @@ -52,6 +53,7 @@ const manifest = { './routes/api/files/delete-directory.tsx': $api_files_delete_directory, './routes/api/files/delete.tsx': $api_files_delete, './routes/api/files/get-directories.tsx': $api_files_get_directories, + './routes/api/files/get.tsx': $api_files_get, './routes/api/files/move-directory.tsx': $api_files_move_directory, './routes/api/files/move.tsx': $api_files_move, './routes/api/files/rename-directory.tsx': $api_files_rename_directory, diff --git a/routes/api/files/get.tsx b/routes/api/files/get.tsx new file mode 100644 index 0000000..9a986cc --- /dev/null +++ b/routes/api/files/get.tsx @@ -0,0 +1,40 @@ +import { Handlers } from 'fresh/server.ts'; + +import { DirectoryFile, FreshContextState } from '/lib/types.ts'; +import { getFiles } from '/lib/data/files.ts'; + +interface Data {} + +export interface RequestBody { + parentPath: string; +} + +export interface ResponseBody { + success: boolean; + files: DirectoryFile[]; +} + +export const handler: Handlers = { + async POST(request, context) { + if (!context.state.user) { + return new Response('Unauthorized', { status: 401 }); + } + + const requestBody = await request.clone().json() as RequestBody; + + if ( + !requestBody.parentPath || !requestBody.parentPath.startsWith('/') || requestBody.parentPath.includes('../') + ) { + return new Response('Bad Request', { status: 400 }); + } + + const files = await getFiles( + context.state.user.id, + requestBody.parentPath, + ); + + const responseBody: ResponseBody = { success: true, files }; + + return new Response(JSON.stringify(responseBody)); + }, +};