@@ -76,10 +76,6 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
76
76
return ATTACHMENT_TABLE ;
77
77
}
78
78
79
- get storageDirectory ( ) {
80
- return `${ this . storage . getUserStorageDirectory ( ) } ${ this . options . attachmentDirectoryName } ` ;
81
- }
82
-
83
79
async init ( ) {
84
80
// Ensure the directory where attachments are downloaded, exists
85
81
await this . storage . makeDir ( this . storageDirectory ) ;
@@ -134,7 +130,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
134
130
} ) ;
135
131
console . debug ( `Attachment (${ id } ) not found in database, creating new record` ) ;
136
132
await this . saveToQueue ( newRecord ) ;
137
- } else if ( record . local_uri == null || ! ( await this . storage . fileExists ( record . local_uri ) ) ) {
133
+ } else if ( record . local_uri == null || ! ( await this . storage . fileExists ( this . getLocalUri ( record . local_uri ) ) ) ) {
138
134
// 2. Attachment in database but no local file, mark as queued download
139
135
console . debug ( `Attachment (${ id } ) found in database but no local file, marking as queued download` ) ;
140
136
await this . update ( {
@@ -211,11 +207,11 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
211
207
await this . powersync . writeTransaction ( deleteRecord ) ;
212
208
}
213
209
214
- const uri = record . local_uri || this . getLocalUri ( record . filename ) ;
210
+ const localFilePathUri = this . getLocalUri ( record . local_uri || this . getLocalFilePathSuffix ( record . filename ) ) ;
215
211
216
212
try {
217
213
// Delete file on storage
218
- await this . storage . deleteFile ( uri , {
214
+ await this . storage . deleteFile ( localFilePathUri , {
219
215
filename : record . filename
220
216
} ) ;
221
217
} catch ( e ) {
@@ -241,8 +237,10 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
241
237
if ( ! record . local_uri ) {
242
238
throw new Error ( `No local_uri for record ${ JSON . stringify ( record , null , 2 ) } ` ) ;
243
239
}
240
+
241
+ const localFilePathUri = this . getLocalUri ( record . local_uri ) ;
244
242
try {
245
- if ( ! ( await this . storage . fileExists ( record . local_uri ) ) ) {
243
+ if ( ! ( await this . storage . fileExists ( localFilePathUri ) ) ) {
246
244
console . warn ( `File for ${ record . id } does not exist, skipping upload` ) ;
247
245
await this . update ( {
248
246
...record ,
@@ -251,7 +249,7 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
251
249
return true ;
252
250
}
253
251
254
- const fileBuffer = await this . storage . readFile ( record . local_uri , {
252
+ const fileBuffer = await this . storage . readFile ( localFilePathUri , {
255
253
encoding : EncodingType . Base64 ,
256
254
mediaType : record . media_type
257
255
} ) ;
@@ -276,9 +274,10 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
276
274
277
275
async downloadRecord ( record : AttachmentRecord ) {
278
276
if ( ! record . local_uri ) {
279
- record . local_uri = this . getLocalUri ( record . filename ) ;
277
+ record . local_uri = this . getLocalFilePathSuffix ( record . filename ) ;
280
278
}
281
- if ( await this . storage . fileExists ( record . local_uri ) ) {
279
+ const localFilePathUri = this . getLocalUri ( record . local_uri ) ;
280
+ if ( await this . storage . fileExists ( localFilePathUri ) ) {
282
281
console . debug ( `Local file already downloaded, marking "${ record . id } " as synced` ) ;
283
282
await this . update ( { ...record , state : AttachmentState . SYNCED } ) ;
284
283
return true ;
@@ -299,9 +298,9 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
299
298
} ) ;
300
299
301
300
// Ensure directory exists
302
- await this . storage . makeDir ( record . local_uri . replace ( record . filename , '' ) ) ;
301
+ await this . storage . makeDir ( localFilePathUri . replace ( record . filename , '' ) ) ;
303
302
// Write the file
304
- await this . storage . writeFile ( record . local_uri , base64Data , {
303
+ await this . storage . writeFile ( localFilePathUri , base64Data , {
305
304
encoding : EncodingType . Base64
306
305
} ) ;
307
306
@@ -436,8 +435,28 @@ export abstract class AbstractAttachmentQueue<T extends AttachmentQueueOptions =
436
435
}
437
436
}
438
437
439
- getLocalUri ( filename : string ) : string {
440
- return `${ this . storageDirectory } /${ filename } ` ;
438
+ /**
439
+ * Returns the local file path for the given filename, used to store in the database.
440
+ * Example: filename: "attachment-1.jpg" returns "attachments/attachment-1.jpg"
441
+ */
442
+ getLocalFilePathSuffix ( filename : string ) : string {
443
+ return `${ this . options . attachmentDirectoryName } /${ filename } ` ;
444
+ }
445
+
446
+ /**
447
+ * Return users storage directory with the attachmentPath use to load the file.
448
+ * Example: filePath: "attachments/attachment-1.jpg" returns "/var/mobile/Containers/Data/Application/.../Library/attachments/attachment-1.jpg"
449
+ */
450
+ getLocalUri ( filePath : string ) : string {
451
+ return `${ this . storage . getUserStorageDirectory ( ) } /${ filePath } ` ;
452
+ }
453
+
454
+ /**
455
+ * Returns the directory where attachments are stored on the device, used to make dir
456
+ * Example: "/var/mobile/Containers/Data/Application/.../Library/attachments/"
457
+ */
458
+ get storageDirectory ( ) {
459
+ return `${ this . storage . getUserStorageDirectory ( ) } ${ this . options . attachmentDirectoryName } ` ;
441
460
}
442
461
443
462
async expireCache ( ) {
0 commit comments