Skip to content

Commit

Permalink
fix(sync): correct format of decrypted paths (#1319)
Browse files Browse the repository at this point in the history
* test: ensure files are decrypted correctly

* fix: relativeLocalPath shouldn't start with documents dir

A huge thank you to @QubaB for their work finding the problem in #1314

This commit fixes the problem slightly earlier in the syncing process, and adds a test for it

Closes #1306, Closes #1304, Closes #1314
  • Loading branch information
adil192 authored Jul 20, 2024
1 parent 544f6ed commit 58efd47
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 24 deletions.
33 changes: 9 additions & 24 deletions lib/data/nextcloud/saber_syncer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -109,18 +109,9 @@ class SaberSyncInterface

@override
Future<SaberSyncFile> getSyncFileFromRemoteFile(WebDavFile remoteFile) async {
final decryptedPath = await decryptPath(client!, remoteFile.path.path);
if (decryptedPath == null)
final relativeLocalPath = await decryptPath(client!, remoteFile.path.path);
if (relativeLocalPath == null)
throw Exception('Decryption failed for ${remoteFile.path.path}');

if (decryptedPath == NextcloudClientExtension.configFileName)
return SaberSyncFile(
remoteFile: remoteFile,
localFile:
FileManager.getFile('/${NextcloudClientExtension.configFileName}'),
);

final relativeLocalPath = '${FileManager.documentsDirectory}$decryptedPath';
final localFile = FileManager.getFile(relativeLocalPath);

return SaberSyncFile(remoteFile: remoteFile, localFile: localFile);
Expand Down Expand Up @@ -399,19 +390,9 @@ class SaberSyncInterface
}

// get remote file
try {
file.remoteFile ??= await _client!.webdav
.propfind(
PathUri.parse(file.remotePath),
depth: WebDavDepth.zero,
prop: const WebDavPropWithoutValues.fromBools(
davGetlastmodified: true,
davGetcontentlength: true,
),
)
.then((multistatus) => multistatus.toWebDavFiles().single);
} catch (e) {
// remote file doesn't exist; keep local
file.remoteFile ??= await _getWebDavFileUncached(file.remotePath);
if (file.remoteFile == null) {
// Remote file doesn't exist, keep local
return BestFile.local;
}

Expand Down Expand Up @@ -443,6 +424,10 @@ class SaberSyncInterface
log.fine('Remote file not cached for $remotePath');
}

return await _getWebDavFileUncached(remotePath);
}

static Future<WebDavFile?> _getWebDavFileUncached(String remotePath) async {
final client = SaberSyncInterface.client;
if (client == null) return null;

Expand Down
8 changes: 8 additions & 0 deletions test/nc_upload_download_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ void main() async {
expect(upBytes.length, greaterThan(0));
await syncer.interface.uploadRemoteFile(syncFile, upBytes);

// Get the sync file again, this time starting with the remote file
final remoteFile =
await syncer.interface.getWebDavFile(syncFile.remotePath);
if (remoteFile == null) fail('Remote file not found after upload');
final syncFile2 =
await syncer.interface.getSyncFileFromRemoteFile(remoteFile);
expect(syncFile2, equals(syncFile));

// Download
final downBytes = await syncer.interface.downloadRemoteFile(syncFile);
expect(downBytes.length, greaterThan(0));
Expand Down

0 comments on commit 58efd47

Please sign in to comment.