From cfeb5c6ebddc4201335b0fa9e45e04f84fdeb98a Mon Sep 17 00:00:00 2001 From: wfz Date: Wed, 13 May 2026 16:16:51 +0800 Subject: [PATCH] chore: update gitignore and add pages functions --- .gitignore | 3 ++ functions/api/fs/list.ts | 72 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 functions/api/fs/list.ts diff --git a/.gitignore b/.gitignore index 09d2456..5ad8f73 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,6 @@ /temp/ /docs/.vitepress/dist/ /.idea/ +/.wrangler/ +.wranglerrc +.dev.vars diff --git a/functions/api/fs/list.ts b/functions/api/fs/list.ts new file mode 100644 index 0000000..783a87d --- /dev/null +++ b/functions/api/fs/list.ts @@ -0,0 +1,72 @@ +interface Env { + R2_BUCKET: R2Bucket; +} + +export async function onRequest(context: { env: Env; request: Request }) { + const url = new URL(context.request.url); + + if (url.pathname === '/api/fs/list' && context.request.method === 'POST') { + try { + const files: any[] = []; + let cursor: string | undefined; + + do { + const listing = await context.env.R2_BUCKET.list({ + cursor: cursor, + limit: 1000 + }); + + for (const obj of listing.objects) { + if (obj.key.endsWith('.mp4') && !obj.key.includes('/')) { + const nameWithoutExt = obj.key.replace('.mp4', ''); + files.push({ + name: obj.key, + size: obj.size, + modified: obj.lastModified, + thumb: `https://pub-7d4a6640bc77480c99842eea19bb2b69.r2.dev/thumbs/${nameWithoutExt}.jpg` + }); + } + } + + cursor = listing.cursor; + } while (cursor); + + const response = { + code: 200, + message: "success", + data: { + content: files.map((f, i) => ({ + id: `r2_${i}`, + path: "", + name: f.name, + size: f.size, + is_dir: false, + modified: f.modified, + created: f.modified, + sign: "", + thumb: f.thumb, + type: 2, + hashinfo: "null", + hash_info: null + })), + total: files.length, + readme: "", + header: "", + write: false, + provider: "r2" + } + }; + + return new Response(JSON.stringify(response), { + headers: { 'Content-Type': 'application/json' } + }); + } catch (e) { + return new Response(JSON.stringify({ error: e.message }), { + status: 500, + headers: { 'Content-Type': 'application/json' } + }); + } + } + + return new Response('Not Found', { status: 404 }); +} \ No newline at end of file