@@ -481,20 +481,20 @@ function processFiles(files: File[]): AudioFile[] {
481481// ---------------------
482482export async function extractAudioMetadata ( file : File ) : Promise < AudioMetadata > {
483483 setProcessingState ( true ) ;
484-
484+
485485 const ext = file . name . split ( "." ) . pop ( ) ?. toLowerCase ( ) ;
486486
487487 // Use flo-specific extraction for .flo files
488488 if ( ext === "flo" ) {
489489 try {
490490 const arrayBuffer = await file . arrayBuffer ( ) ;
491- const {
492- getFloMetadata,
493- getFloCoverArt,
494- getFloSyncedLyrics,
495- getFloInfo
491+ const {
492+ getFloMetadata,
493+ getFloCoverArt,
494+ getFloSyncedLyrics,
495+ getFloInfo
496496 } = await import ( "./floProcessor" ) ;
497-
497+
498498 // Get all flo data in parallel
499499 const [ meta , cover , lyrics , info ] = await Promise . all ( [
500500 getFloMetadata ( arrayBuffer ) ,
@@ -725,7 +725,7 @@ export function setupFileHandler(
725725 typeof window . launchQueue . setConsumer !== "function"
726726 ) {
727727 console . warn ( "File Handling API not supported in this browser" ) ;
728- return ( ) => { } ; // Return empty cleanup function
728+ return ( ) => { } ; // Return empty cleanup function
729729 }
730730
731731 const consumer = async ( launchParams : any ) => {
@@ -790,7 +790,7 @@ export function setupFileHandler(
790790 typeof window . launchQueue . setConsumer === "function"
791791 ) {
792792 try {
793- window . launchQueue . setConsumer ( ( ) => { } ) ;
793+ window . launchQueue . setConsumer ( ( ) => { } ) ;
794794 } catch ( e ) {
795795 console . warn ( "Could not clear file handler consumer:" , e ) ;
796796 }
@@ -810,21 +810,17 @@ export interface ShareTargetResult {
810810}
811811
812812export function handleShareTarget ( ) : ShareTargetResult | null {
813- // Share targets can send data via URL parameters or launch queue
814813 if ( typeof window === "undefined" ) {
815814 return null ;
816815 }
817816
818- // Check URL parameters for share data
819817 const urlParams = new URLSearchParams ( window . location . search ) ;
820818 const title = urlParams . get ( "title" ) || undefined ;
821819 const text = urlParams . get ( "text" ) || undefined ;
822820 const url = urlParams . get ( "url" ) || undefined ;
823821
824- // Determine what type of share this is
825- // Files would typically come through launch queue or POST request
826- if ( title || text ) {
827- // Text-based share - could be song info to search for
822+ if ( title || text || url ) {
823+ // Handle text sharing
828824 return {
829825 files : [ ] ,
830826 title,
@@ -834,12 +830,12 @@ export function handleShareTarget(): ShareTargetResult | null {
834830 } ;
835831 }
836832
837- return null ;
833+ // Handle files shared via Cache API
834+ return null ; // Will be handled in useShareTarget
838835}
839836
840- // Hook to handle share targets and file shares
841837export function useShareTarget (
842- onShareReceived : ( result : ShareTargetResult ) => void ,
838+ onShareReceived : ( result : ShareTargetResult ) => void
843839) {
844840 const hasProcessedRef = useRef ( false ) ;
845841
@@ -848,46 +844,47 @@ export function useShareTarget(
848844
849845 async function processShare ( ) {
850846 const urlParams = new URLSearchParams ( window . location . search ) ;
851-
852- // Check for files in Cache (sent from Service Worker)
847+
853848 if ( urlParams . get ( "share-received" ) === "true" ) {
854849 try {
855- const cache = await caches . open ( 'incoming-shares' ) ;
856- const response = await cache . match ( '/shared-file' ) ;
857-
858- if ( response ) {
859- const blob = await response . blob ( ) ;
860- // Try to recover the filename from headers or default to "shared-audio"
861- const filename = response . headers . get ( 'x-file-name' ) || "shared-audio.mp3" ;
862- const file = new File ( [ blob ] , filename , { type : blob . type } ) ;
850+ const cache = await caches . open ( "incoming-shares" ) ;
851+ const keys = await cache . keys ( ) ;
852+
853+ const files : File [ ] = [ ] ;
854+ for ( const key of keys ) {
855+ if ( key . url . includes ( "/shared-file-" ) ) {
856+ const response = await cache . match ( key ) ;
857+ const blob = await response ?. blob ( ) ;
858+ const fileName =
859+ response ?. headers . get ( "x-file-name" ) || "unknown-file" ;
860+ files . push ( new File ( [ blob ! ] , fileName , { type : blob ?. type } ) ) ;
861+ }
862+ }
863863
864+ if ( files . length > 0 ) {
864865 hasProcessedRef . current = true ;
865- onShareReceived ( {
866- files : [ file ] ,
867- type : "files" ,
868- } ) ;
866+ onShareReceived ( { files, type : "files" } ) ;
867+
868+ // Cleanup the cache after processing
869+ for ( const key of keys ) {
870+ await cache . delete ( key ) ;
871+ }
869872
870- // Cleanup
871- await cache . delete ( '/shared-file' ) ;
872873 const cleanUrl = new URL ( window . location . href ) ;
873874 cleanUrl . searchParams . delete ( "share-received" ) ;
874875 window . history . replaceState ( { } , "" , cleanUrl . toString ( ) ) ;
875- return ; // Exit early if we handled files
876+ return ;
876877 }
877- } catch ( e ) {
878- console . error ( "Failed to retrieve shared file from cache " , e ) ;
878+ } catch ( error ) {
879+ console . error ( "Failed to retrieve shared files: " , error ) ;
879880 }
880881 }
881882
882- // Fallback to standard Text/URL share handling
883+ // Fallback to text sharing (e.g., query parameters)
883884 const shareResult = handleShareTarget ( ) ;
884- if ( shareResult && shareResult . type !== "none" ) {
885+ if ( shareResult ) {
885886 hasProcessedRef . current = true ;
886887 onShareReceived ( shareResult ) ;
887-
888- const url = new URL ( window . location . href ) ;
889- [ "title" , "text" , "url" ] . forEach ( ( p ) => url . searchParams . delete ( p ) ) ;
890- window . history . replaceState ( { } , "" , url . toString ( ) ) ;
891888 }
892889 }
893890
0 commit comments