From 0f890884861ce7c632ce5e77601922af31f53f5a Mon Sep 17 00:00:00 2001 From: Joe Ma Date: Sun, 26 Mar 2023 20:52:40 +0800 Subject: [PATCH] Fix js/css content type (paste) Update README.md Signed-off-by: Joe Ma --- README.md | 2 ++ src/index.ts | 65 ++++++++++++++++++---------------------------- web/v2/js/paste.js | 2 +- 3 files changed, 28 insertions(+), 41 deletions(-) diff --git a/README.md b/README.md index 77abc35..0b35a4d 100644 --- a/README.md +++ b/README.md @@ -120,6 +120,7 @@ Add `?qr=1` to enable QR code generation for paste link. |`qrcode`|Toggle QR code generation| |`paste-type`|Set paste type| |`title`|File's title| +|`json`|Use JSON response| #### For raw request, @@ -131,6 +132,7 @@ Add `?qr=1` to enable QR code generation for paste link. |`x-read-limit`|The maximum access count| |`x-paste-type`|Set paste type| |`x-qr`|Toggle QR code generation| +|`x-json`|Use JSON response| The request body contains the upload content. diff --git a/src/index.ts b/src/index.ts index 4c2a014..8c3c765 100644 --- a/src/index.ts +++ b/src/index.ts @@ -69,54 +69,18 @@ export default { } if (path === '/v1' && method == 'GET') { - return await fetch(PASTE_WEB_URL_v1 + '/paste.html', { - cf: { - cacheEverything: true, - }, - }).then(value => { - return new Response(value.body, { - // Add the correct content-type to response header - headers: { - 'content-type': 'text/html; charset=UTF-8;', - 'cache-control': 'public, max-age=172800', - }, - }); - }); + return await proxy_uri(PASTE_WEB_URL_v1 + '/paste.html'); } if (/\/(js|css)\/.*$/.test(path) && method == 'GET') { - return await fetch(PASTE_WEB_URL + path, { - cf: { - cacheEverything: true, - }, - }).then(value => { - return new Response(value.body, { - // Add the correct content-type to response header - headers: { - 'content-type': 'text/html; charset=UTF-8;', - 'cache-control': 'public, max-age=172800', - }, - }); - }); + return await proxy_uri(PASTE_WEB_URL + path); } if (path === '/') { switch (method) { - // Fetch the HTML for uploading text/file case 'GET': { - return await fetch(PASTE_WEB_URL + '/paste.html', { - cf: { - cacheEverything: true, - }, - }).then(value => { - return new Response(value.body, { - // Add the correct content-type to response header - headers: { - 'content-type': 'text/html; charset=UTF-8;', - 'cache-control': 'public, max-age=172800', - }, - }); - }); + // Fetch the HTML for uploading text/file + return await proxy_uri(PASTE_WEB_URL + '/paste.html'); } // Create new paste @@ -636,6 +600,27 @@ function to_human_readable_size(bytes: number): string { return size; } +// Proxy URI (limit to html/js/css) +async function proxy_uri(path: string, cf: RequestInitCfProperties = {cacheEverything: true}) { + // Fix content type + let file_type = 'text/plain'; + if (path.endsWith('.js')) file_type = 'application/javascript'; + if (path.endsWith('.css')) file_type = 'text/css'; + if (path.endsWith('.html')) file_type = 'text/html'; + + return await fetch(path, { + cf, + }).then(value => { + return new Response(value.body, { + // Add the correct content-type to response header + headers: { + 'content-type': `${file_type}; charset=UTF-8;`, + 'cache-control': 'public, max-age=172800', + }, + }); + }); +} + interface PasteIndexEntry { title?: string, mime_type?: string, diff --git a/web/v2/js/paste.js b/web/v2/js/paste.js index 106a994..13b8944 100644 --- a/web/v2/js/paste.js +++ b/web/v2/js/paste.js @@ -52,7 +52,7 @@ function validate_url(path) { function show_pop_alert(message, alert_type = 'alert-primary') { remove_pop_alert(); - $('body').prepend(jQuery.parseHTML( + $('.navbar').after(jQuery.parseHTML( `