From c18f08a4cbf7dde027bc5063138bf0a6dace3865 Mon Sep 17 00:00:00 2001 From: Joe Ma Date: Tue, 29 Jul 2025 20:12:39 +0800 Subject: [PATCH] Rename large_upload endpoint path Signed-off-by: Joe Ma --- README.md | 8 ++++---- frontend/static/paste.js | 4 ++-- pnpm-workspace.yaml | 4 ++++ src/{v2 => api}/large_upload.ts | 2 +- src/index.ts | 10 +++++----- src/utils.ts | 2 +- 6 files changed, 17 insertions(+), 13 deletions(-) create mode 100644 pnpm-workspace.yaml rename src/{v2 => api}/large_upload.ts (99%) diff --git a/README.md b/README.md index 9d6e979..e1501c3 100644 --- a/README.md +++ b/README.md @@ -172,7 +172,7 @@ Delete paste by uuid. *If the password is set, this request requires additional Update paste setting. *If the password is set, this request requires additional `x-pass` header* -### POST /v2/large_upload/create +### POST /api/large_upload/create Generate the presigned URL for upload large paste to the given S3 endpoint `LARGE_ENDPOINT` using HTTP `PUT` request. @@ -189,11 +189,11 @@ Generate the presigned URL for upload large paste to the given S3 endpoint `LARG The `file-size` and `file-sha256sum` field is required. -### POST /v2/large_upload/complete/\ +### POST /api/large_upload/complete/\ -Finialize the paste created from `/v2/large_upload/create`. +Finialize the paste created from `/api/large_upload/create`. -### GET /v2/large_upload/\ +### GET /api/large_upload/\ Generate the presigned URL for upload large paste to the given S3 endpoint `LARGE_DOWNLOAD_ENDPOINT` using HTTP `GET` request. diff --git a/frontend/static/paste.js b/frontend/static/paste.js index f1413af..3090dd4 100644 --- a/frontend/static/paste.js +++ b/frontend/static/paste.js @@ -271,7 +271,7 @@ $(function () { try { // Retrieve presigned URL for upload large paste - const res = await fetch(`${ENDPOINT}/v2/large_upload/create`, { + const res = await fetch(`${ENDPOINT}/api/large_upload/create`, { method: 'POST', body: filtered, }); @@ -294,7 +294,7 @@ $(function () { throw new Error(`Unable to upload paste: ${(await res1.text()) || `${res1.status} ${res1.statusText}`}`); } // Finialize the paste - const res2 = await fetch(`${ENDPOINT}/v2/large_upload/complete/${create_result.uuid}`, { + const res2 = await fetch(`${ENDPOINT}/api/large_upload/complete/${create_result.uuid}`, { method: 'POST', }); if (res2.ok) { diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml new file mode 100644 index 0000000..5ba62b2 --- /dev/null +++ b/pnpm-workspace.yaml @@ -0,0 +1,4 @@ +onlyBuiltDependencies: + - esbuild + - sharp + - workerd diff --git a/src/v2/large_upload.ts b/src/api/large_upload.ts similarity index 99% rename from src/v2/large_upload.ts rename to src/api/large_upload.ts index 0b2d1b0..c6dadef 100644 --- a/src/v2/large_upload.ts +++ b/src/api/large_upload.ts @@ -6,7 +6,7 @@ import { ERequest, Env, PasteIndexEntry } from '../types'; import { gen_id, get_paste_info_obj } from '../utils'; import constants from '../constant'; -export const router = Router({ base: '/v2/large_upload' }); +export const router = Router({ base: '/api/large_upload' }); export async function get_presign_url(uuid: string, descriptor: PasteIndexEntry) { // Use cached presigned url if expiration is more than 10 mins diff --git a/src/index.ts b/src/index.ts index ef16e2f..44315de 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,7 +23,7 @@ import { ERequest, Env, PasteIndexEntry } from './types'; import { serve_static } from './proxy'; import { check_password_rules, get_paste_info, get_basic_auth, gen_id } from './utils'; import constants, { fetch_constant } from './constant'; -import { get_presign_url, router as large_upload } from './v2/large_upload'; +import { get_presign_url, router as large_upload } from './api/large_upload'; // In favour of new cors() in itty-router v5 const { preflight, corsify } = cors({ @@ -38,10 +38,10 @@ const { preflight, corsify } = cors({ const router = Router({ before: [ - preflight, (_, env) => { fetch_constant(env); }, + preflight, ], catch: error, finally: [corsify], @@ -264,7 +264,7 @@ router.post('/', async (request, env, ctx) => { }); // Handle large upload (> 25MB) -router.all('/v2/large_upload/*', large_upload.fetch); +router.all('/api/large_upload/*', large_upload.fetch); // Fetch paste by uuid [4-digit UUID] router.get('/:uuid/:option?', async (request, env, ctx) => { @@ -379,7 +379,7 @@ router.get('/:uuid/:option?', async (request, env, ctx) => { const cache = caches.default; const match_etag = headers.get('If-None-Match') || undefined; // Define the Request object as cache key - const req_key = new Request(`https://${env.SERVICE_URL}/${uuid}`, { + const req_key = new Request(`${env.SERVICE_URL}/${uuid}`, { method: 'GET', headers: match_etag ? { @@ -570,7 +570,7 @@ router.delete('/:uuid', async (request, env, ctx) => { if (res.ok) { ctx.waitUntil(env.PASTE_INDEX.delete(uuid)); // Invalidate CF cache - ctx.waitUntil(cache.delete(new Request(`https://${env.SERVICE_URL}/${uuid}`))); + ctx.waitUntil(cache.delete(new Request(`${env.SERVICE_URL}/${uuid}`))); return new Response('OK\n'); } else { return new Response('Unable to process such request.\n', { diff --git a/src/utils.ts b/src/utils.ts index 20ce884..432b5e8 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -29,7 +29,7 @@ export const gen_id = customAlphabet( export function get_paste_info_obj(uuid: string, descriptor: PasteIndexEntry, env: Env) { const created = new Date(descriptor.last_modified); const expired = new Date(descriptor.expiration ?? descriptor.last_modified + 2419200000); - const link = `https://${env.SERVICE_URL}/${uuid}`; + const link = `${env.SERVICE_URL}/${uuid}`; const paste_info = { uuid, link,