Fix js/css content type (paste)

Update README.md

Signed-off-by: Joe Ma <rikkaneko23@gmail.com>
This commit is contained in:
Joe Ma 2023-03-26 20:52:40 +08:00
parent 7e65c403f7
commit 0f89088486
No known key found for this signature in database
GPG key ID: 7A0ECF5F5EDC587F
3 changed files with 28 additions and 41 deletions

View file

@ -120,6 +120,7 @@ Add `?qr=1` to enable QR code generation for paste link.
|`qrcode`|Toggle QR code generation| |`qrcode`|Toggle QR code generation|
|`paste-type`|Set paste type| |`paste-type`|Set paste type|
|`title`|File's title| |`title`|File's title|
|`json`|Use JSON response|
#### For raw request, #### 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-read-limit`|The maximum access count|
|`x-paste-type`|Set paste type| |`x-paste-type`|Set paste type|
|`x-qr`|Toggle QR code generation| |`x-qr`|Toggle QR code generation|
|`x-json`|Use JSON response|
The request body contains the upload content. The request body contains the upload content.

View file

@ -69,54 +69,18 @@ export default {
} }
if (path === '/v1' && method == 'GET') { if (path === '/v1' && method == 'GET') {
return await fetch(PASTE_WEB_URL_v1 + '/paste.html', { return await proxy_uri(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',
},
});
});
} }
if (/\/(js|css)\/.*$/.test(path) && method == 'GET') { if (/\/(js|css)\/.*$/.test(path) && method == 'GET') {
return await fetch(PASTE_WEB_URL + path, { return await proxy_uri(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',
},
});
});
} }
if (path === '/') { if (path === '/') {
switch (method) { switch (method) {
// Fetch the HTML for uploading text/file
case 'GET': { case 'GET': {
return await fetch(PASTE_WEB_URL + '/paste.html', { // Fetch the HTML for uploading text/file
cf: { return await proxy_uri(PASTE_WEB_URL + '/paste.html');
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',
},
});
});
} }
// Create new paste // Create new paste
@ -636,6 +600,27 @@ function to_human_readable_size(bytes: number): string {
return size; 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 { interface PasteIndexEntry {
title?: string, title?: string,
mime_type?: string, mime_type?: string,

View file

@ -52,7 +52,7 @@ function validate_url(path) {
function show_pop_alert(message, alert_type = 'alert-primary') { function show_pop_alert(message, alert_type = 'alert-primary') {
remove_pop_alert(); remove_pop_alert();
$('body').prepend(jQuery.parseHTML( $('.navbar').after(jQuery.parseHTML(
`<div class="alert ${alert_type} alert-dismissible fade show position-absolute top-0 start-50 translate-middle-x" `<div class="alert ${alert_type} alert-dismissible fade show position-absolute top-0 start-50 translate-middle-x"
style="margin-top: 80px; max-width: 500px; width: 80%" id="pop_alert" role="alert"> \ style="margin-top: 80px; max-width: 500px; width: 80%" id="pop_alert" role="alert"> \
<div>${message}</div> \ <div>${message}</div> \