@@ -340,84 +340,65 @@ function runWorker(type, content, key) {
340340 worker . postMessage ( { type, content, key } , transferables ) ;
341341 } ) ;
342342}
343-
344- async function getFileContents ( id ) {
343+ async function setFileContents ( id , dataUri ) {
345344 if ( ! dbCache ) dbCache = await openDB ( CurrentUsername , 1 ) ;
346345 if ( ! cryptoKeyCache ) cryptoKeyCache = await getKey ( password ) ;
347346
348- const transaction = dbCache . transaction ( 'contentpool' , 'readonly' ) ;
349- const store = transaction . objectStore ( 'contentpool' ) ;
350- const request = store . get ( id ) ;
347+ // Parse data URI
348+ const match = dataUri . match ( / ^ d a t a : ( .* ) ; b a s e 6 4 , ( .* ) $ / ) ;
349+ if ( ! match ) throw new Error ( 'Invalid data URI' ) ;
350+ const mime = match [ 1 ] ;
351+ const base64 = match [ 2 ] ;
352+
353+ const uint8 = Uint8Array . from ( atob ( base64 ) , c => c . charCodeAt ( 0 ) ) ;
354+ const encrypted = await runWorker ( 'encrypt' , uint8 , cryptoKeyCache ) ;
355+
356+ const tx = dbCache . transaction ( 'contentpool' , 'readwrite' ) ;
357+ const store = tx . objectStore ( 'contentpool' ) ;
358+ const request = store . put ( { key : id , value : encrypted , type : mime } ) ;
351359
352360 return new Promise ( ( resolve , reject ) => {
353- request . onsuccess = async ( ) => {
354- if ( request . result ) {
355- const { value } = request . result ;
356- try {
357- const result = await runWorker ( 'decrypt' , value , cryptoKeyCache ) ;
358- resolve ( result ) ;
359- } catch ( error ) {
360- reject ( error ) ;
361- }
362- } else {
363- reject ( new Error ( 'File not found' ) ) ;
364- }
365- } ;
361+ request . onsuccess = resolve ;
366362 request . onerror = ( ) => reject ( request . error ) ;
367363 } ) ;
368364}
369365
370- async function setFileContents ( id , content ) {
366+ async function getFileContents ( id ) {
371367 if ( ! dbCache ) dbCache = await openDB ( CurrentUsername , 1 ) ;
372368 if ( ! cryptoKeyCache ) cryptoKeyCache = await getKey ( password ) ;
373369
374- const dataToStore = await runWorker ( 'encrypt' , content , cryptoKeyCache ) ;
375-
376- const transaction = dbCache . transaction ( 'contentpool' , 'readwrite' ) ;
377- const store = transaction . objectStore ( 'contentpool' ) ;
378- const request = store . put ( { key : id , value : dataToStore } ) ;
370+ const tx = dbCache . transaction ( 'contentpool' , 'readonly' ) ;
371+ const store = tx . objectStore ( 'contentpool' ) ;
372+ const request = store . get ( id ) ;
379373
380374 return new Promise ( ( resolve , reject ) => {
381- request . onsuccess = resolve ;
375+ request . onsuccess = async ( ) => {
376+ if ( ! request . result ) return reject ( new Error ( 'File not found' ) ) ;
377+ try {
378+ const decrypted = await runWorker ( 'decrypt' , request . result . value , cryptoKeyCache ) ;
379+ const base64 = btoa ( String . fromCharCode ( ...new Uint8Array ( decrypted ) ) ) ;
380+ resolve ( `data:${ request . result . type } ;base64,${ base64 } ` ) ;
381+ } catch ( err ) {
382+ reject ( err ) ;
383+ }
384+ } ;
382385 request . onerror = ( ) => reject ( request . error ) ;
383386 } ) ;
384387}
385388
386389async function removeFileContents ( id ) {
387390 if ( ! dbCache ) dbCache = await openDB ( CurrentUsername , 1 ) ;
388-
389- const transaction = dbCache . transaction ( 'contentpool' , 'readwrite' ) ;
390- const store = transaction . objectStore ( 'contentpool' ) ;
391+ const tx = dbCache . transaction ( 'contentpool' , 'readwrite' ) ;
392+ const store = tx . objectStore ( 'contentpool' ) ;
391393 const request = store . delete ( id ) ;
392-
393394 return new Promise ( ( resolve , reject ) => {
394395 request . onsuccess = resolve ;
395396 request . onerror = ( ) => reject ( request . error ) ;
396397 } ) ;
397398}
398399
399400const ctntMgr = {
400- async get ( id ) {
401- try {
402- return await enqueueRequest ( getFileContents , [ id ] ) ;
403- } catch ( error ) {
404- throw error ;
405- }
406- } ,
407-
408- async set ( id , content ) {
409- try {
410- return await enqueueRequest ( setFileContents , [ id , content ] ) ;
411- } catch ( error ) {
412- throw error ;
413- }
414- } ,
415-
416- async remove ( id ) {
417- try {
418- return await enqueueRequest ( removeFileContents , [ id ] ) ;
419- } catch ( error ) {
420- throw error ;
421- }
422- }
423- } ;
401+ get ( id ) { return enqueueRequest ( getFileContents , [ id ] ) ; } ,
402+ set ( id , dataUri ) { return enqueueRequest ( setFileContents , [ id , dataUri ] ) ; } ,
403+ remove ( id ) { return enqueueRequest ( removeFileContents , [ id ] ) ; }
404+ } ;
0 commit comments