Skip to content

Commit

Permalink
Update worker.js
Browse files Browse the repository at this point in the history
  • Loading branch information
RooRay authored May 24, 2024
1 parent a9ab104 commit c13624d
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions worker.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
// Read github.com/RooRay/B2Worker for setup instructions

export async function handleFile(event) {
const url = new URL(event.request.url);
const cache = caches.default;
let response = await cache.match(event.request);
let contentType = 'application/octet-stream'; // Default content type

if (!response) {
// Replace this URL with your friendly URL as per the instructions
response = await fetch(`https://f003.backblazeb2.com/file/RooImg${url.pathname}`);
const headers = { "cache-control": "public, max-age=14400" };

Expand All @@ -19,8 +16,8 @@ export async function handleFile(event) {
event.waitUntil(cache.put(event.request, response.clone()));
}

// If the file is an image, instruct the browser to display it in the browser.
if (contentType.startsWith('image/')) {
// If the file is an image or a text file, instruct the browser to display it in the browser.
if (contentType.startsWith('image/') || contentType === 'text/plain') {
const disposition = `inline; filename="${url.pathname.split('/').pop()}"`;
response.headers.set('Content-Disposition', disposition);
}
Expand All @@ -40,7 +37,9 @@ function getContentTypeFromExtension(pathname) {
return 'image/jpeg';
case 'png':
return 'image/png';
// Add more cases for other image formats if needed.
case 'txt':
return 'text/plain';
// Add more cases for other formats if needed.
default:
return 'application/octet-stream'; // Fallback content type.
}
Expand Down

0 comments on commit c13624d

Please sign in to comment.