From 8b3af1c53c4f356ec2d466a7a034fd341cc5f487 Mon Sep 17 00:00:00 2001 From: Arianrhod Sandlot Date: Tue, 23 Jan 2024 21:58:47 +0800 Subject: [PATCH] feat: add server provider --- package.json | 1 + pnpm-lock.yaml | 14 ++++- .../file-system-providers/server-provider.ts | 57 +++++++++++++++++++ src/server/app.ts | 7 +++ 4 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 src/core/classes/file-system-providers/server-provider.ts create mode 100644 src/server/app.ts diff --git a/package.json b/package.json index c349889..b672dac 100644 --- a/package.json +++ b/package.json @@ -30,6 +30,7 @@ "framer-motion": "10.16.16", "github-fork-ribbon-css": "0.2.3", "goodcodes-parser": "2.5.0", + "hono": "^3.12.6", "i18next": "23.7.13", "i18next-browser-languagedetector": "7.2.0", "idb": "8.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 5bb0b1b..98a745e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -62,6 +62,9 @@ dependencies: goodcodes-parser: specifier: 2.5.0 version: 2.5.0 + hono: + specifier: ^3.12.6 + version: 3.12.6 i18next: specifier: 23.7.13 version: 23.7.13 @@ -1860,8 +1863,8 @@ packages: '@types/gapi.client.discovery-v1': 0.0.4 dev: true - /@maxim_mazurok/gapi.client.drive-v3@0.0.20231217: - resolution: {integrity: sha512-1lkVnRLCMym0f3ZvJQJBSye9GiTDS6VFJMzEdu3KEQt4LLAN4H4gTtBww8x/ucajTfB0rJevFRKjO1MZHnkElQ==} + /@maxim_mazurok/gapi.client.drive-v3@0.0.20240118: + resolution: {integrity: sha512-hCBTOZ0Tf1iWEVB3i0iyp4WRt7oTQFB/NpNl/ApI6YIRlv0RiiaQrhU9GG3kbpkGciLaeIJsNVt9Po0+chAq/A==} dependencies: '@types/gapi.client': 1.0.8 '@types/gapi.client.discovery-v1': 0.0.4 @@ -2739,7 +2742,7 @@ packages: /@types/gapi.client.drive-v3@0.0.4: resolution: {integrity: sha512-jE37dJ0EzAdY0aJPFOp20xmec/aO0P4HtUIA9k07RMPyedFDOcuMlSac1r0PklwQdgXF7BHaMoObNHNAnwSQUQ==} dependencies: - '@maxim_mazurok/gapi.client.drive-v3': 0.0.20231217 + '@maxim_mazurok/gapi.client.drive-v3': 0.0.20240118 dev: true /@types/gapi.client@1.0.8: @@ -4735,6 +4738,11 @@ packages: function-bind: 1.1.2 dev: true + /hono@3.12.6: + resolution: {integrity: sha512-nnLMJbBA8k+tW8XD1Xt0BfNmJswppYF2pSOVo5U3DdU72SPYUjFkPg7/Q9KfkNcsrXzxFdJQ00JYjPGancmOOA==} + engines: {node: '>=16.0.0'} + dev: false + /hosted-git-info@2.8.9: resolution: {integrity: sha512-mxIDAb9Lsm6DoOJ7xH+5+X4y1LU/4Hi50L9C5sIswK3JzULS4bwk1FvjdBgvYR4bzT4tuUQiC15FE2f5HbLvYw==} dev: true diff --git a/src/core/classes/file-system-providers/server-provider.ts b/src/core/classes/file-system-providers/server-provider.ts new file mode 100644 index 0000000..a7f5be0 --- /dev/null +++ b/src/core/classes/file-system-providers/server-provider.ts @@ -0,0 +1,57 @@ +import ky from 'ky' +import { noop } from 'lodash-es' +import { FileAccessor } from './file-accessor' +import { type FileSystemProvider } from './file-system-provider' + +export class ServerProvider implements FileSystemProvider { + static getSingleton() { + return new ServerProvider() + } + + async getContent(path: string) { + return await ky('/api/content', { json: { path } }).blob() + } + + async peekContent(path: string) { + return await this.getContent(path) + } + + async getContentAndCache(path: string) { + return await this.getContent(path) + } + + // path should not start with a slash + // todo: maybe needs to make it be the same as the onedrive provider + async create({ file, path }: { file: Blob; path: string }) { + const body = new FormData() + body.append('path', path) + body.append('file', file) + await ky('/api/create', { body }) + } + + // todo: not implemented yet + async delete(path: string) { + noop(path) + await Promise.resolve(undefined) + } + + async list(path = '') { + const files = await ky('/api/create', { json: { path } }).json() + + return files.map( + (item) => + new FileAccessor({ + name: item.name ?? '', + directory: path, + type: item.type, + temporaryUrl: item.temporaryUrl, + fileSystemProvider: this, + }), + ) + } + + async peek(path: string) { + noop(path) + return await Promise.resolve(undefined) + } +} diff --git a/src/server/app.ts b/src/server/app.ts new file mode 100644 index 0000000..12d33e0 --- /dev/null +++ b/src/server/app.ts @@ -0,0 +1,7 @@ +import { Hono } from 'hono' + +const app = new Hono() + +app.get('/api/ls', (c) => c.text('Hono!')) + +export default app