www: use a service worker to download the mp3 and add authorization header

This commit is contained in:
2023-11-10 18:24:26 +01:00
committed by Mathieu Virbel
parent c255b41475
commit 1c11b328b3
4 changed files with 70 additions and 54 deletions

View 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));
}
});