Skip to content

Commit c3eb3c9

Browse files
committed
Fixing them one by one
File name preservation
1 parent a1a09fc commit c3eb3c9

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

Build/src/helpers/filePickerHelper.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,9 +856,10 @@ export function useShareTarget(
856856

857857
if (response) {
858858
const blob = await response.blob();
859-
// Try to recover the filename from headers or default to "shared-audio"
860-
const filename =
861-
response.headers.get("x-file-name") || "shared-audio.mp3";
859+
const filenameRaw = response.headers.get("x-file-name");
860+
const filename = filenameRaw
861+
? decodeURIComponent(filenameRaw)
862+
: "shared-audio.mp3";
862863
const file = new File([blob], filename, { type: blob.type });
863864

864865
hasProcessedRef.current = true;

Build/src/workers/sw.ts

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,46 @@ registerRoute(
1818
try {
1919
const formData = await event.request.formData();
2020
const file = formData.get("audio");
21-
if (file) {
21+
if (
22+
file &&
23+
typeof file === "object" &&
24+
"name" in file &&
25+
typeof file.name === "string"
26+
) {
2227
const cache = await caches.open("incoming-shares");
23-
await cache.put("/shared-file", new Response(file));
28+
await cache.put(
29+
"/shared-file",
30+
new Response(file, {
31+
headers: {
32+
"x-file-name": encodeURIComponent(
33+
file.name || "shared-audio.mp3"
34+
),
35+
"content-type": file.type || "application/octet-stream",
36+
},
37+
})
38+
);
39+
} else if (file) {
40+
// Fallback if not a File object
41+
const cache = await caches.open("incoming-shares");
42+
await cache.put(
43+
"/shared-file",
44+
new Response(file, {
45+
headers: {
46+
"x-file-name": "shared-audio.mp3",
47+
"content-type": file.type || "application/octet-stream",
48+
},
49+
})
50+
);
2451
}
2552
const redirectUrl = new URL(
2653
"/beta/HTMLPlayer/?share-received=true",
2754
self.location.origin
2855
);
2956
return Response.redirect(redirectUrl.href, 303);
30-
} catch (e) {
31-
// always return a response even on error
32-
return new Response("Failed to process share", { status: 400 });
57+
} catch (e: any) {
58+
return new Response("Failed to process share: " + (e?.message || e), {
59+
status: 400,
60+
});
3361
}
3462
},
3563
"POST"

0 commit comments

Comments
 (0)