-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
57 lines (49 loc) · 1.98 KB
/
worker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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" };
// Set the appropriate Content-Type header based on the file extension.
contentType = getContentTypeFromExtension(url.pathname);
headers["Content-Type"] = contentType;
response = new Response(response.body, { ...response, headers });
event.waitUntil(cache.put(event.request, response.clone()));
}
// 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);
}
return response;
}
function getContentTypeFromExtension(pathname) {
const extension = pathname.split('.').pop().toLowerCase();
switch (extension) {
case 'mp4':
return 'video/mp4';
case 'webm':
return 'video/webm';
case 'jpg':
case 'jpeg':
return 'image/jpeg';
case 'png':
return 'image/png';
case 'txt':
return 'text/plain';
// Add more cases for other formats if needed.
default:
return 'application/octet-stream'; // Fallback content type.
}
}
addEventListener("fetch", (event) => {
try {
event.respondWith(handleFile(event));
} catch (e) {
return new Response("Something went wrong", { status: 500 });
}
});