mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
www: use a service worker to download the mp3 and add authorization header
This commit is contained in:
25
www/public/service-worker.js
Normal file
25
www/public/service-worker.js
Normal file
@@ -0,0 +1,25 @@
|
||||
let authToken = ""; // Variable to store the token
|
||||
|
||||
self.addEventListener("message", (event) => {
|
||||
if (event.data && event.data.type === "SET_AUTH_TOKEN") {
|
||||
authToken = event.data.token;
|
||||
}
|
||||
});
|
||||
|
||||
self.addEventListener("fetch", function (event) {
|
||||
// Check if the request is for a media file
|
||||
if (/\/v1\/transcripts\/.*\/audio\/mp3$/.test(event.request.url)) {
|
||||
// Modify the request to add the Authorization header
|
||||
const modifiedHeaders = new Headers(event.request.headers);
|
||||
if (authToken) {
|
||||
modifiedHeaders.append("Authorization", `Bearer ${authToken}`);
|
||||
}
|
||||
|
||||
const modifiedRequest = new Request(event.request, {
|
||||
headers: modifiedHeaders,
|
||||
});
|
||||
|
||||
// Respond with the modified request
|
||||
event.respondWith(fetch(modifiedRequest));
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user