From 022f83a387f62e78a4a5b18de98e126adb4d26de Mon Sep 17 00:00:00 2001 From: Joe Ma Date: Sun, 11 Sep 2022 23:38:46 +0800 Subject: [PATCH] Use HTML page instead for browser and plain text for console --- paste.html | 2 +- src/index.ts | 45 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/paste.html b/paste.html index 7c7da6e..d7fabc4 100644 --- a/paste.html +++ b/paste.html @@ -60,7 +60,7 @@
- +

diff --git a/src/index.ts b/src/index.ts index 4ab9262..cee9e99 100644 --- a/src/index.ts +++ b/src/index.ts @@ -95,6 +95,10 @@ export default { const path = pathname.replace(/\/+$/, '') || '/'; let cache = caches.default; + const agent = headers.get('user-agent') ?? ''; + // Detect if request from browsers + const is_browser = ['Mozilla', 'AppleWebKit', 'Chrome', 'Safari', 'Gecko'].some(v => agent.includes(v)); + const s3 = new AwsClient({ accessKeyId: env.AWS_ACCESS_KEY_ID, secretAccessKey: env.AWS_SECRET_ACCESS_KEY, @@ -241,7 +245,7 @@ export default { // Key will be expired after 28 day if unmodified ctx.waitUntil(env.PASTE_INDEX.put(uuid, JSON.stringify(descriptor), {expirationTtl: 2419200})); - return new Response(await get_paste_info(uuid, descriptor, env, need_qrcode)); + return await get_paste_info(uuid, descriptor, env, is_browser, need_qrcode); } else { return new Response('Unable to upload the paste.\n', { status: 500, @@ -279,7 +283,7 @@ export default { switch (method) { case 'GET': { const need_qrcode = searchParams.get('qr') === '1'; - return new Response(await get_paste_info(uuid, descriptor, env, need_qrcode)); + return await get_paste_info(uuid, descriptor, env, is_browser, need_qrcode); } case 'POST': { @@ -451,11 +455,12 @@ export default { }, }; -async function get_paste_info(uuid: string, descriptor: PasteIndexEntry, env: Env, need_qr: boolean = false): Promise { +async function get_paste_info(uuid: string, descriptor: PasteIndexEntry, env: Env, use_html: boolean = true, need_qr: boolean = false): Promise { const date = new Date(descriptor.last_modified); + const link = `https://${SERVICE_URL}/${uuid}`; let content = dedent` id: ${uuid} - link: https://${SERVICE_URL}/${uuid} + link: ${link} title: ${descriptor.title || ''} mime-type: ${descriptor.mime_type ?? '-'} size: ${descriptor.size} bytes (${to_human_readable_size(descriptor.size)}) @@ -466,10 +471,37 @@ async function get_paste_info(uuid: string, descriptor: PasteIndexEntry, env: En created at ${date.toISOString()} `; + // Browser response + if (use_html) { + const html = dedent` + + + + + Paste + + +
${content}
+ ${(need_qr) ? `${link}` : ''} + + + `; + + return new Response(html, { + headers: { + 'content-type': 'text/html; charset=UTF-8;', + }, + }); + } + + // Console response if (need_qr) { // Cloudflare currently does not support doing a subrequest to the same zone, use service binding instead const res = await env.QRCODE.fetch('https://qrcode.nekoul.com?' + new URLSearchParams({ - q: `https://${SERVICE_URL}/${uuid}`, + q: link, + type: 'utf8', })); if (res.ok) { @@ -479,7 +511,8 @@ async function get_paste_info(uuid: string, descriptor: PasteIndexEntry, env: En content += '\n'; } } - return content; + + return new Response(content); } function check_password_rules(password: string): boolean {