diff --git a/public/scripts/ui.js b/public/scripts/ui.js index fb44352..cc2a91b 100644 --- a/public/scripts/ui.js +++ b/public/scripts/ui.js @@ -1617,17 +1617,24 @@ class WebShareTargetUI { console.log('Shared Target Text:', '"' + shareTargetText + '"'); Events.fire('activate-paste-mode', {files: [], text: shareTargetText}) } else if (share_target_type === "files") { - const openRequest = window.indexedDB.open('pairdrop_store') - openRequest.onsuccess( db => { + let openRequest = window.indexedDB.open('pairdrop_store') + openRequest.onsuccess = e => { + const db = e.target.result; const tx = db.transaction('share_target_files', 'readwrite'); const store = tx.objectStore('share_target_files'); const request = store.getAll(); request.onsuccess = _ => { - Events.fire('activate-paste-mode', {files: request.result, text: ""}) + const fileObjects = request.result; + let filesReceived = []; + for (let i=0; i db.close(); } - }) + } } window.history.replaceState({}, "Rewrite URL", '/'); } @@ -1684,7 +1691,7 @@ class PersistentStorage { PersistentStorage.logBrowserNotCapable(); return; } - const DBOpenRequest = window.indexedDB.open('pairdrop_store', 2); + const DBOpenRequest = window.indexedDB.open('pairdrop_store', 3); DBOpenRequest.onerror = (e) => { PersistentStorage.logBrowserNotCapable(); console.log('Error initializing database: '); @@ -1710,7 +1717,10 @@ class PersistentStorage { } try { - db.createObjectStore('share_target_files'); + if (db.objectStoreNames.contains('share_target_files')) { + db.deleteObjectStore('share_target_files'); + } + db.createObjectStore('share_target_files', {autoIncrement: true}); } catch (error) { console.log("Object store named 'share_target_files' already exists") } diff --git a/public/service-worker.js b/public/service-worker.js index d286eb9..e6d1548 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -71,30 +71,12 @@ const update = request => self.addEventListener('fetch', function(event) { if (event.request.method === "POST") { // Requests related to Web Share Target. - event.respondWith( - (async () => { - const formData = await event.request.formData(); - const title = formData.get("title"); - const text = formData.get("text"); - const url = formData.get("url"); - const files = formData.get("files"); - let share_url = "/"; - if (files.length > 0) { - share_url = "/?share-target=files"; - const db = await window.indexedDB.open('pairdrop_store'); - const tx = db.transaction('share_target_files', 'readwrite'); - const store = tx.objectStore('share_target_files'); - for (let i=0; i 0 || text.length > 0 || url.length) { - share_url = `/?share-target=text&title=${title}&text=${text}&url=${url}`; - } - return Response.redirect(encodeURI(share_url), 303); - })() - ); + evaluateRequestData(event.request).then(share_url => { + console.debug(share_url); + event.respondWith( + Response.redirect(encodeURI(share_url), 302) + ); + }) } else { // Regular requests not related to Web Share Target. event.respondWith( @@ -119,3 +101,45 @@ self.addEventListener('activate', evt => }) ) ); + +const evaluateRequestData = async function (request) { + const formData = await request.formData(); + const title = formData.get("title"); + const text = formData.get("text"); + const url = formData.get("url"); + const files = formData.getAll("files"); + console.debug(files) + let fileObjects = []; + for (let i=0; i { + if (fileObjects?.length > 0) { + const DBOpenRequest = indexedDB.open('pairdrop_store'); + DBOpenRequest.onsuccess = (e) => { + const db = e.target.result; + for (let i = 0; i < fileObjects.length; i++) { + const transaction = db.transaction('share_target_files', 'readwrite'); + const objectStore = transaction.objectStore('share_target_files'); + + const objectStoreRequest = objectStore.add(fileObjects[i]); + objectStoreRequest.onsuccess = _ => { + if (i === fileObjects.length - 1) resolve('/?share-target=files'); + } + } + } + DBOpenRequest.onerror = _ => { + resolve('/'); + } + } else if (title?.length > 0 || text?.length > 0 || url?.length > 0) { + console.debug(title || text || url); + resolve(`/?share-target=text&title=${title}&text=${text}&url=${url}`); + } else { + resolve('/'); + } + }); +} diff --git a/public_included_ws_fallback/scripts/ui.js b/public_included_ws_fallback/scripts/ui.js index 8a28a19..53b418b 100644 --- a/public_included_ws_fallback/scripts/ui.js +++ b/public_included_ws_fallback/scripts/ui.js @@ -1618,17 +1618,24 @@ class WebShareTargetUI { console.log('Shared Target Text:', '"' + shareTargetText + '"'); Events.fire('activate-paste-mode', {files: [], text: shareTargetText}) } else if (share_target_type === "files") { - const openRequest = window.indexedDB.open('pairdrop_store') - openRequest.onsuccess( db => { + let openRequest = window.indexedDB.open('pairdrop_store') + openRequest.onsuccess = e => { + const db = e.target.result; const tx = db.transaction('share_target_files', 'readwrite'); const store = tx.objectStore('share_target_files'); const request = store.getAll(); request.onsuccess = _ => { - Events.fire('activate-paste-mode', {files: request.result, text: ""}) + const fileObjects = request.result; + let filesReceived = []; + for (let i=0; i db.close(); } - }) + } } window.history.replaceState({}, "Rewrite URL", '/'); } @@ -1685,7 +1692,7 @@ class PersistentStorage { PersistentStorage.logBrowserNotCapable(); return; } - const DBOpenRequest = window.indexedDB.open('pairdrop_store', 2); + const DBOpenRequest = window.indexedDB.open('pairdrop_store', 3); DBOpenRequest.onerror = (e) => { PersistentStorage.logBrowserNotCapable(); console.log('Error initializing database: '); @@ -1711,7 +1718,10 @@ class PersistentStorage { } try { - db.createObjectStore('share_target_files'); + if (db.objectStoreNames.contains('share_target_files')) { + db.deleteObjectStore('share_target_files'); + } + db.createObjectStore('share_target_files', {autoIncrement: true}); } catch (error) { console.log("Object store named 'share_target_files' already exists") } diff --git a/public_included_ws_fallback/service-worker.js b/public_included_ws_fallback/service-worker.js index 25bc379..d8f7ece 100644 --- a/public_included_ws_fallback/service-worker.js +++ b/public_included_ws_fallback/service-worker.js @@ -71,30 +71,12 @@ const update = request => self.addEventListener('fetch', function(event) { if (event.request.method === "POST") { // Requests related to Web Share Target. - event.respondWith( - (async () => { - const formData = await event.request.formData(); - const title = formData.get("title"); - const text = formData.get("text"); - const url = formData.get("url"); - const files = formData.get("files"); - let share_url = "/"; - if (files.length > 0) { - share_url = "/?share-target=files"; - const db = await window.indexedDB.open('pairdrop_store'); - const tx = db.transaction('share_target_files', 'readwrite'); - const store = tx.objectStore('share_target_files'); - for (let i=0; i 0 || text.length > 0 || url.length) { - share_url = `/?share-target=text&title=${title}&text=${text}&url=${url}`; - } - return Response.redirect(encodeURI(share_url), 303); - })() - ); + evaluateRequestData(event.request).then(share_url => { + console.debug(share_url); + event.respondWith( + Response.redirect(encodeURI(share_url), 302) + ); + }) } else { // Regular requests not related to Web Share Target. event.respondWith( @@ -119,3 +101,45 @@ self.addEventListener('activate', evt => }) ) ); + +const evaluateRequestData = async function (request) { + const formData = await request.formData(); + const title = formData.get("title"); + const text = formData.get("text"); + const url = formData.get("url"); + const files = formData.getAll("files"); + console.debug(files) + let fileObjects = []; + for (let i=0; i { + if (fileObjects?.length > 0) { + const DBOpenRequest = indexedDB.open('pairdrop_store'); + DBOpenRequest.onsuccess = (e) => { + const db = e.target.result; + for (let i = 0; i < fileObjects.length; i++) { + const transaction = db.transaction('share_target_files', 'readwrite'); + const objectStore = transaction.objectStore('share_target_files'); + + const objectStoreRequest = objectStore.add(fileObjects[i]); + objectStoreRequest.onsuccess = _ => { + if (i === fileObjects.length - 1) resolve('/?share-target=files'); + } + } + } + DBOpenRequest.onerror = _ => { + resolve('/'); + } + } else if (title?.length > 0 || text?.length > 0 || url?.length > 0) { + console.debug(title || text || url); + resolve(`/?share-target=text&title=${title}&text=${text}&url=${url}`); + } else { + resolve('/'); + } + }); +}