From 7b6870f46f1d4a0a423a2d1fb8b1bc10d49448cf Mon Sep 17 00:00:00 2001 From: Jackabomb <48334975+Jackabomb@users.noreply.github.com> Date: Tue, 18 Apr 2023 12:26:28 -0700 Subject: [PATCH 1/2] Fix backups not working in the root directory. Fixes #327. Whenever GoogleDriveStorageProvider receives the empty string as a path argument, it will now treat it as a synonymn for "root" instead of passing it through to the API. Note, the paths "/", "//", "///", and so on are equally invalid and should probably be treated similarly, but that is not addressed here. --- .../GoogleDrive/GoogleDriveStorageProvider.cs | 21 ++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/KeeAnywhere/StorageProviders/GoogleDrive/GoogleDriveStorageProvider.cs b/KeeAnywhere/StorageProviders/GoogleDrive/GoogleDriveStorageProvider.cs index 97d06fa..75a8ffb 100644 --- a/KeeAnywhere/StorageProviders/GoogleDrive/GoogleDriveStorageProvider.cs +++ b/KeeAnywhere/StorageProviders/GoogleDrive/GoogleDriveStorageProvider.cs @@ -88,18 +88,22 @@ public async Task Copy(string sourcePath, string destPath) if (sourceFile == null) throw new FileNotFoundException("Google Drive: File not found.", sourcePath); - var destFolder = CloudPath.GetDirectoryName(destPath); - var parentFolder = await api.GetFileByPath(destFolder); - if (parentFolder == null) - throw new FileNotFoundException("Google Drive: File not found.", destFolder); - var destFilename = CloudPath.GetFileName(destPath); + var destFolder = CloudPath.GetDirectoryName(destPath); var destFile = new File { - Name = destFilename, - Parents = new[] {parentFolder.Id} + Name = destFilename }; + if (!string.IsNullOrEmpty(destFolder)) + { + var parentFolder = await api.GetFileByPath(destFolder); + if (parentFolder == null) + throw new FileNotFoundException("Google Drive: File not found.", destFolder); + + destFile.Parents = new[] { parentFolder.Id }; + } + var result = await api.Files.Copy(destFile, sourceFile.Id).ExecuteAsync(); } @@ -166,6 +170,9 @@ public async Task> GetChildrenByParentItem(Stor public async Task> GetChildrenByParentPath(string path) { + if (string.IsNullOrEmpty(path)) + return await GetChildrenByParentItem(await GetRootItem()); + var api = await GetApi(); var item = await api.GetFileByPath(path); if (item == null) From 456dc9ae836edbc0126ec38c690c9a58251cfb01 Mon Sep 17 00:00:00 2001 From: Jackabomb <48334975+Jackabomb@users.noreply.github.com> Date: Tue, 18 Apr 2023 13:30:15 -0700 Subject: [PATCH 2/2] Add root as default parent for new backups. This gets overwritten for anything not in the drive root, so newly-copied backups in the drive root have their parent explicitly set to root, instead of inheriting it from the source file. Apparently this is how the Google Drive API treats copies with File unset or set to an empty list: the newly-created copy inherits the Parents list of the source file it was copied from. This will fix a future incompatibility between root backups and shortcuts. If a shortcut exists in the root, and we try to back it up, the backups should appear alongside the shortcut. However, unless we set the parent to root, they would inherit the target's parent directory and appear alongside the target in it's home directory (wherever in the drive that might be). --- .../StorageProviders/GoogleDrive/GoogleDriveStorageProvider.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/KeeAnywhere/StorageProviders/GoogleDrive/GoogleDriveStorageProvider.cs b/KeeAnywhere/StorageProviders/GoogleDrive/GoogleDriveStorageProvider.cs index 75a8ffb..19fd7b1 100644 --- a/KeeAnywhere/StorageProviders/GoogleDrive/GoogleDriveStorageProvider.cs +++ b/KeeAnywhere/StorageProviders/GoogleDrive/GoogleDriveStorageProvider.cs @@ -92,7 +92,8 @@ public async Task Copy(string sourcePath, string destPath) var destFolder = CloudPath.GetDirectoryName(destPath); var destFile = new File { - Name = destFilename + Name = destFilename, + Parents = new[] { "root" } }; if (!string.IsNullOrEmpty(destFolder))