Skip to content

Commit cdf66de

Browse files
DominicGBauerDominicGBauer
andauthored
chore: allow different file extensions (#47)
Co-authored-by: DominicGBauer <dominic@nomanini.com>
1 parent 775e4db commit cdf66de

File tree

4 files changed

+12
-11
lines changed

4 files changed

+12
-11
lines changed

demos/supabase-todolist/lib/attachments/queue.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class PhotoAttachmentQueue extends AbstractAttachmentQueue {
5353
}
5454

5555
@override
56-
StreamSubscription<void> watchIds() {
56+
StreamSubscription<void> watchIds({String fileExtension = 'jpg'}) {
5757
log.info('Watching photos in $todosTable...');
5858
return db.watch('''
5959
SELECT photo_id FROM $todosTable
@@ -63,7 +63,7 @@ class PhotoAttachmentQueue extends AbstractAttachmentQueue {
6363
}).listen((ids) async {
6464
List<String> idsInQueue = await attachmentsService.getAttachmentIds();
6565
for (String id in ids) {
66-
await syncingService.reconcileId(id, idsInQueue);
66+
await syncingService.reconcileId(id, idsInQueue, fileExtension);
6767
}
6868
});
6969
}

packages/powersync_attachments_helper/example/getting_started.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class PhotoAttachmentQueue extends AbstractAttachmentQueue {
4444
}
4545

4646
@override
47-
StreamSubscription<void> watchIds() {
47+
StreamSubscription<void> watchIds({String fileExtension = 'jpg'}) {
4848
return db.watch('''
4949
SELECT photo_id FROM users
5050
WHERE photo_id IS NOT NULL
@@ -53,7 +53,7 @@ class PhotoAttachmentQueue extends AbstractAttachmentQueue {
5353
}).listen((ids) async {
5454
List<String> idsInQueue = await attachmentsService.getAttachmentIds();
5555
for (String id in ids) {
56-
await syncingService.reconcileId(id, idsInQueue);
56+
await syncingService.reconcileId(id, idsInQueue, fileExtension);
5757
}
5858
});
5959
}

packages/powersync_attachments_helper/lib/src/attachments_queue.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ abstract class AbstractAttachmentQueue {
3434
db, remoteStorage, localStorage, attachmentsService, getLocalUri);
3535
}
3636

37-
/// Create watcher to get list of ID's from a table to be used for syncing in the attachment queue
38-
StreamSubscription<void> watchIds();
37+
/// Create watcher to get list of ID's from a table to be used for syncing in the attachment queue.
38+
/// Set the file extension if you are using a different file type
39+
StreamSubscription<void> watchIds({String fileExtension = 'jpg'});
3940

4041
/// Create a function to save photos using the attachment queue
4142
Future<Attachment> savePhoto(String photoId, int size);

packages/powersync_attachments_helper/lib/src/syncing_service.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,22 +196,22 @@ class SyncingService {
196196
/// in local and remote storage.
197197
/// If the ID is in the queue, but the file does not exist locally then it is
198198
/// marked for download.
199-
reconcileId(String id, List<String> idsInQueue) async {
199+
reconcileId(String id, List<String> idsInQueue, String fileExtension) async {
200200
bool idIsInQueue = idsInQueue.contains(id);
201201

202-
String imagePath = await getLocalUri('$id.jpg');
203-
File file = File(imagePath);
202+
String path = await getLocalUri('$id.$fileExtension');
203+
File file = File(path);
204204
bool fileExists = await file.exists();
205205

206206
if (!idIsInQueue) {
207207
if (fileExists) {
208-
log.info('ignore file $id.jpg as it already exists');
208+
log.info('ignore file $id.$fileExtension as it already exists');
209209
return;
210210
}
211211
log.info('Adding $id to queue');
212212
return await attachmentsService.saveAttachment(Attachment(
213213
id: id,
214-
filename: '$id.jpg',
214+
filename: '$id.$fileExtension',
215215
state: AttachmentState.queuedDownload.index,
216216
));
217217
}

0 commit comments

Comments
 (0)