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|
|`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.

View file

@ -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,

View file

@ -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(
`<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"> \
<div>${message}</div> \