Skip to content

Commit 421c989

Browse files
committed
modify the naming of files
1 parent 08616a3 commit 421c989

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

wami/app.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,6 @@ browseImagesButton.addEventListener('click', async e => {
273273
if (!('showOpenFilePicker' in window)) {
274274
// Browser doesn't support the File System Access API.
275275
// Use the legacy file input.
276-
277276
const button = document.createElement('input');
278277
button.type = 'file';
279278
button.multiple = true;
@@ -286,7 +285,7 @@ browseImagesButton.addEventListener('click', async e => {
286285

287286
for (const file of files) {
288287
imagesToStore.push({
289-
file,
288+
file, // <-- The original file object keeps its name
290289
fsHandlePromise: Promise.resolve(null)
291290
});
292291
resolve();
@@ -615,11 +614,14 @@ async function loadAndProcessSharedImages(shareCache, shareData) {
615614
const fileResponse = await shareCache.match(`file-${i}`);
616615
if (fileResponse) {
617616
const blob = await fileResponse.blob();
618-
const file = new File(
619-
[blob],
620-
`shared-${i + 1}.${getFileExtension(blob.type)}`,
621-
{ type: blob.type }
622-
);
617+
618+
// Get the original file name from the shareData.fileNames array
619+
// This is more reliable than trying to extract it from the blob
620+
const fileName = (shareData.fileNames && shareData.fileNames[i]) ||
621+
`shared-${i + 1}.${getFileExtension(blob.type)}`;
622+
623+
// Create a File object with the original name
624+
const file = new File([blob], fileName, { type: blob.type });
623625

624626
imagesToStore.push({
625627
file,

wami/sw.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,7 @@ self.addEventListener('activate', event => {
9494
});
9595

9696
// Handle share target requests: This listener specifically processes POST requests
97-
// to the /share-target endpoint, which is triggered when other apps share content with wami.
98-
// It stores the shared data and files in a temporary cache for the main app to access.
97+
// to the /share-target endpoint
9998
self.addEventListener('fetch', event => {
10099
// Only process POST requests to the share-target endpoint
101100
if (event.request.method === 'POST' && event.request.url.includes('/share-target')) {
@@ -122,23 +121,26 @@ self.addEventListener('fetch', event => {
122121
// Store the files in a temporary cache for the client to access
123122
const shareCache = await caches.open('share-target-cache');
124123

125-
// Create an object with the share data
124+
// Extract file names and store them in the shareData
125+
const fileNames = files.map(file => file.name);
126+
127+
// Create share data object with file names array
126128
const shareData = {
127129
title: data.title,
128130
text: data.text,
129131
url: data.url,
130132
timestamp: Date.now(),
131-
fileCount: files.length
133+
fileCount: files.length,
134+
fileNames: fileNames // Store file names in the share data
132135
};
133136

134-
// Store the share data and files
137+
// Store the share data
135138
await shareCache.put('shareData', new Response(JSON.stringify(shareData)));
136139

137140
// Store each file with a unique key
138141
for (let i = 0; i < files.length; i++) {
139142
const file = files[i];
140-
const response = new Response(file);
141-
await shareCache.put(`file-${i}`, response);
143+
await shareCache.put(`file-${i}`, new Response(file));
142144
}
143145
}
144146
} catch (error) {

0 commit comments

Comments
 (0)