@@ -43,7 +43,7 @@ class IndexedDBService {
4343 request . onupgradeneeded = ( event ) => {
4444 console . log ( 'Upgrading IndexedDB schema...' ) ;
4545 const db = ( event . target as IDBOpenDBRequest ) . result ;
46-
46+
4747 if ( ! db . objectStoreNames . contains ( PDF_STORE_NAME ) ) {
4848 console . log ( 'Creating PDF documents store...' ) ;
4949 db . createObjectStore ( PDF_STORE_NAME , { keyPath : 'id' } ) ;
@@ -75,7 +75,12 @@ class IndexedDBService {
7575 console . log ( 'Adding document to IndexedDB:' , document . name ) ;
7676 const transaction = this . db ! . transaction ( [ PDF_STORE_NAME ] , 'readwrite' ) ;
7777 const store = transaction . objectStore ( PDF_STORE_NAME ) ;
78- const request = store . put ( document ) ;
78+
79+ // Create a structured clone of the document to ensure proper storage
80+ const request = store . put ( {
81+ ...document ,
82+ data : document . data // Store ArrayBuffer directly
83+ } ) ;
7984
8085 request . onerror = ( event ) => {
8186 const error = ( event . target as IDBRequest ) . error ;
@@ -165,7 +170,7 @@ class IndexedDBService {
165170 // Create two transactions - one for document deletion, one for location cleanup
166171 const docTransaction = this . db ! . transaction ( [ PDF_STORE_NAME ] , 'readwrite' ) ;
167172 const configTransaction = this . db ! . transaction ( [ CONFIG_STORE_NAME ] , 'readwrite' ) ;
168-
173+
169174 const docStore = docTransaction . objectStore ( PDF_STORE_NAME ) ;
170175 const configStore = configTransaction . objectStore ( CONFIG_STORE_NAME ) ;
171176
@@ -221,7 +226,7 @@ class IndexedDBService {
221226
222227 const transaction = this . db ! . transaction ( [ EPUB_STORE_NAME ] , 'readwrite' ) ;
223228 const store = transaction . objectStore ( EPUB_STORE_NAME ) ;
224-
229+
225230 // Create a structured clone of the document to ensure proper storage
226231 const request = store . put ( {
227232 ...document ,
@@ -315,7 +320,7 @@ class IndexedDBService {
315320 // Create two transactions - one for document deletion, one for location cleanup
316321 const docTransaction = this . db ! . transaction ( [ EPUB_STORE_NAME ] , 'readwrite' ) ;
317322 const configTransaction = this . db ! . transaction ( [ CONFIG_STORE_NAME ] , 'readwrite' ) ;
318-
323+
319324 const docStore = docTransaction . objectStore ( EPUB_STORE_NAME ) ;
320325 const configStore = configTransaction . objectStore ( CONFIG_STORE_NAME ) ;
321326
@@ -477,17 +482,15 @@ class IndexedDBService {
477482 async syncToServer ( ) : Promise < { lastSync : number } > {
478483 const pdfDocs = await this . getAllDocuments ( ) ;
479484 const epubDocs = await this . getAllEPUBDocuments ( ) ;
480-
485+
481486 const documents = [ ] ;
482-
483- // Process PDF documents - store the raw PDF data
487+
488+ // Process PDF documents - convert ArrayBuffer to array for JSON serialization
484489 for ( const doc of pdfDocs ) {
485- const arrayBuffer = await doc . data . arrayBuffer ( ) ;
486- const uint8Array = new Uint8Array ( arrayBuffer ) ;
487490 documents . push ( {
488491 ...doc ,
489492 type : 'pdf' ,
490- data : Array . from ( uint8Array ) // Convert to regular array for JSON serialization
493+ data : Array . from ( new Uint8Array ( doc . data ) )
491494 } ) ;
492495 }
493496
@@ -496,7 +499,7 @@ class IndexedDBService {
496499 documents . push ( {
497500 ...doc ,
498501 type : 'epub' ,
499- data : Array . from ( new Uint8Array ( doc . data ) ) // Convert to regular array for JSON serialization
502+ data : Array . from ( new Uint8Array ( doc . data ) )
500503 } ) ;
501504 }
502505
@@ -520,31 +523,26 @@ class IndexedDBService {
520523 }
521524
522525 const { documents } = await response . json ( ) ;
523-
526+
524527 // Process each document
525528 for ( const doc of documents ) {
529+ // Convert the numeric array back to ArrayBuffer
530+ const uint8Array = new Uint8Array ( doc . data ) ;
531+ const documentData = {
532+ id : doc . id ,
533+ type : doc . type ,
534+ name : doc . name ,
535+ size : doc . size ,
536+ lastModified : doc . lastModified ,
537+ data : uint8Array . buffer
538+ } ;
539+
526540 if ( doc . type === 'pdf' ) {
527- // Create a Blob from the raw binary data
528- const blob = new Blob ( [ new Uint8Array ( doc . data ) ] , { type : 'application/pdf' } ) ;
529- await this . addDocument ( {
530- id : doc . id ,
531- type : doc . type ,
532- name : doc . name ,
533- size : doc . size ,
534- lastModified : doc . lastModified ,
535- data : blob
536- } ) ;
541+ await this . addDocument ( documentData ) ;
537542 } else if ( doc . type === 'epub' ) {
538- // Convert the numeric array back to ArrayBuffer for EPUB
539- const uint8Array = new Uint8Array ( doc . data ) ;
540- await this . addEPUBDocument ( {
541- id : doc . id ,
542- type : doc . type ,
543- name : doc . name ,
544- size : doc . size ,
545- lastModified : doc . lastModified ,
546- data : uint8Array . buffer
547- } ) ;
543+ await this . addEPUBDocument ( documentData ) ;
544+ } else {
545+ console . warn ( `Unknown document type: ${ doc . type } ` ) ;
548546 }
549547 }
550548
0 commit comments