Add auto content type detection

Fix bugs
This commit is contained in:
Joe Ma 2022-09-12 12:20:00 +08:00
parent 22440ab66f
commit b3b7fafe8c
No known key found for this signature in database
GPG key ID: 7A0ECF5F5EDC587F

View file

@ -158,9 +158,8 @@ export default {
} }
// File // File
if (data instanceof File) { if (data instanceof File) {
if (data.name) { title = data.name || undefined;
title = data.name; mime_type = data.type || undefined;
}
buffer = await data.arrayBuffer(); buffer = await data.arrayBuffer();
// Text // Text
} else { } else {
@ -175,8 +174,14 @@ export default {
} }
const count = formdata.get('read-limit'); const count = formdata.get('read-limit');
if (typeof count === 'string' && !isNaN(+count)) { if (typeof count === 'string') {
read_limit = Number(count) || undefined; const n = parseInt(count);
if (isNaN(n) || n <= 0) {
return new Response('Invalid read-limit field, must be a positive integer.\n', {
status: 422,
});
}
read_limit = n;
} }
// Check if qrcode generation needed // Check if qrcode generation needed
@ -187,14 +192,18 @@ export default {
// Raw body // Raw body
} else { } else {
if (headers.has('x-title')) { title = headers.get('x-title') || undefined;
title = headers.get('x-title') || ''; mime_type = headers.get('x-content-type') || undefined;
}
mime_type = headers.get('x-content-type') || mime_type;
password = headers.get('x-pass') || undefined; password = headers.get('x-pass') || undefined;
const count = headers.get('x-read-limit') || undefined; const count = headers.get('x-read-limit') || undefined;
if (count !== undefined && !isNaN(+count)) { if (typeof count === 'string') {
read_limit = Number(count) || undefined; const n = parseInt(count);
if (isNaN(n) || n <= 0) {
return new Response('x-read-limit must be a positive integer.\n', {
status: 422,
});
}
read_limit = n;
} }
buffer = await request.arrayBuffer(); buffer = await request.arrayBuffer();
} }
@ -235,12 +244,12 @@ export default {
if (res.ok) { if (res.ok) {
// Upload success // Upload success
const descriptor: PasteIndexEntry = { const descriptor: PasteIndexEntry = {
title: title ?? undefined, title: title || undefined,
last_modified: Date.now(), last_modified: Date.now(),
password: password ? sha256(password).slice(0, 16) : undefined, password: password ? sha256(password).slice(0, 16) : undefined,
read_count_remain: read_limit ?? undefined,
mime_type: mime_type || undefined,
size, size,
read_count_remain: read_limit,
mime_type,
}; };
// Key will be expired after 28 day if unmodified // Key will be expired after 28 day if unmodified
@ -341,7 +350,9 @@ export default {
}); });
} }
descriptor.read_count_remain--; descriptor.read_count_remain--;
ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor), {expirationTtl: 2419200})); ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor), {
expiration: descriptor.last_modified / 1000 + 2419200,
}));
} }
// Enable CF cache for authorized request // Enable CF cache for authorized request
@ -492,6 +503,7 @@ async function get_paste_info(uuid: string, descriptor: PasteIndexEntry, env: En
return new Response(html, { return new Response(html, {
headers: { headers: {
'content-type': 'text/html; charset=UTF-8;', 'content-type': 'text/html; charset=UTF-8;',
'cache-control': 'no-store',
}, },
}); });
} }
@ -512,7 +524,11 @@ async function get_paste_info(uuid: string, descriptor: PasteIndexEntry, env: En
} }
content += '\n'; content += '\n';
return new Response(content); return new Response(content, {
headers: {
'cache-control': 'no-store',
},
});
} }
function check_password_rules(password: string): boolean { function check_password_rules(password: string): boolean {