Skip to content

Commit 1b66f0a

Browse files
committed
FIx clearing local uri
1 parent 84e8d7b commit 1b66f0a

File tree

4 files changed

+76
-2
lines changed

4 files changed

+76
-2
lines changed

packages/powersync_core/lib/src/attachments/attachment.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ final class Attachment {
143143
);
144144
}
145145

146+
Attachment markAsUnavailableLocally(AttachmentState newState) {
147+
return Attachment(
148+
id: id,
149+
timestamp: timestamp,
150+
filename: filename,
151+
state: newState,
152+
localUri: null,
153+
mediaType: mediaType,
154+
size: size,
155+
hasSynced: false,
156+
metaData: metaData,
157+
);
158+
}
159+
146160
@override
147161
String toString() {
148162
return 'Attachment(id: $id, state: $state, localUri: $localUri, metadata: $metaData)';

packages/powersync_core/lib/src/attachments/attachment_queue_service.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ base class AttachmentQueue {
433433
attachment.state == AttachmentState.queuedUpload) &&
434434
!exists) {
435435
updates.add(
436-
attachment.copyWith(state: AttachmentState.archived, localUri: null),
436+
attachment.markAsUnavailableLocally(AttachmentState.archived),
437437
);
438438
}
439439
}

packages/powersync_core/lib/src/attachments/io_local_storage.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import 'local_storage.dart';
99

1010
/// Implements [LocalStorage] for device filesystem using Dart IO.
1111
///
12-
/// Handles file and directory operations for attachments.
12+
/// Handles file and directory operations for attachments. The database only
13+
/// stores relative paths for attachments that this implementation resolves
14+
/// against the root path provided as a constructor argument. For that reason,
15+
/// it's important that the root directory stays consistent, as data may be lost
16+
/// otherwise.
1317
///
1418
/// {@category attachments}
1519
@experimental

packages/powersync_core/test/attachments/attachment_test.dart

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,62 @@ void main() {
108108
expect(await localStorage.fileExists(localUri), isFalse);
109109
});
110110

111+
test('stores relative paths', () async {
112+
// Regression test we had in the Kotlin/Swift implementation:
113+
// https://github.com/powersync-ja/powersync-swift/pull/74
114+
await queue.startSync();
115+
await db.execute(
116+
'INSERT INTO users (id, name, email, photo_id) VALUES (uuid(), ?, ?, ?)',
117+
['steven', '[email protected]', 'picture_id'],
118+
);
119+
120+
// Wait for attachment to sync.
121+
await expectLater(
122+
attachments,
123+
emitsThrough([
124+
isA<Attachment>()
125+
.having((e) => e.state, 'state', AttachmentState.synced)
126+
]));
127+
128+
expect(await localStorage.fileExists('picture_id.jpg'), isTrue);
129+
});
130+
131+
test('recovers from deleted local files', () async {
132+
// Create an attachments record which has an invalid local_uri.
133+
await db.execute(
134+
'INSERT OR REPLACE INTO attachments_queue '
135+
'(id, timestamp, filename, local_uri, media_type, size, state, has_synced, meta_data) '
136+
'VALUES (uuid(), current_timestamp, ?, ?, ?, ?, ?, ?, ?)',
137+
[
138+
'attachment.jpg',
139+
'invalid/dir/attachment.jpg',
140+
'application/jpeg',
141+
1,
142+
AttachmentState.synced.toInt(),
143+
1,
144+
""
145+
],
146+
);
147+
await attachments.next;
148+
149+
queue = AttachmentQueue(
150+
db: db,
151+
remoteStorage: remoteStorage,
152+
watchAttachments: watchAttachments,
153+
localStorage: localStorage,
154+
archivedCacheLimit: 1,
155+
);
156+
157+
// The attachment should be marked as archived, and the local URI should be
158+
// removed.
159+
await queue.startSync();
160+
161+
final [attachment] = await attachments.next;
162+
expect(attachment.filename, 'attachment.jpg');
163+
expect(attachment.localUri, isNull);
164+
expect(attachment.state, AttachmentState.archived);
165+
});
166+
111167
test('uploads attachments', () async {
112168
await queue.startSync();
113169

0 commit comments

Comments
 (0)