From 486c1a2556c5899f11314b64738f862d8b93ecb6 Mon Sep 17 00:00:00 2001 From: Claudio Cambra Date: Tue, 4 Apr 2023 16:37:30 +0800 Subject: [PATCH] Split local file related metadata management into own extension in NextcloudFilesDatabaseManager Signed-off-by: Claudio Cambra --- ...cloudFilesDatabaseManager+LocalFiles.swift | 91 +++++++++++++++++++ .../NextcloudFilesDatabaseManager.swift | 73 --------------- .../project.pbxproj | 4 + 3 files changed, 95 insertions(+), 73 deletions(-) create mode 100644 shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudFilesDatabaseManager+LocalFiles.swift diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudFilesDatabaseManager+LocalFiles.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudFilesDatabaseManager+LocalFiles.swift new file mode 100644 index 000000000..2241d200c --- /dev/null +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudFilesDatabaseManager+LocalFiles.swift @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2023 by Claudio Cambra + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + */ + +import Foundation +import RealmSwift +import OSLog + +extension NextcloudFilesDatabaseManager { + func localFileMetadataFromOcId(_ ocId: String) -> NextcloudLocalFileMetadataTable? { + if let metadata = ncDatabase().objects(NextcloudLocalFileMetadataTable.self).filter("ocId == %@", ocId).first { + return NextcloudLocalFileMetadataTable(value: metadata) + } + + return nil + } + + func addLocalFileMetadataFromItemMetadata(_ itemMetadata: NextcloudItemMetadataTable) { + let database = ncDatabase() + + do { + try database.write { + let newLocalFileMetadata = NextcloudLocalFileMetadataTable() + + newLocalFileMetadata.ocId = itemMetadata.ocId + newLocalFileMetadata.fileName = itemMetadata.fileName + newLocalFileMetadata.account = itemMetadata.account + newLocalFileMetadata.etag = itemMetadata.etag + newLocalFileMetadata.exifDate = Date() + newLocalFileMetadata.exifLatitude = "-1" + newLocalFileMetadata.exifLongitude = "-1" + + database.add(newLocalFileMetadata, update: .all) + Logger.ncFilesDatabase.debug("Added local file metadata from item metadata. ocID: \(itemMetadata.ocId, privacy: .public), etag: \(itemMetadata.etag, privacy: .public), fileName: \(itemMetadata.fileName, privacy: .public)") + } + } catch let error { + Logger.ncFilesDatabase.error("Could not add local file metadata from item metadata. ocID: \(itemMetadata.ocId, privacy: .public), etag: \(itemMetadata.etag, privacy: .public), fileName: \(itemMetadata.fileName, privacy: .public), received error: \(error.localizedDescription, privacy: .public)") + } + } + + func deleteLocalFileMetadata(ocId: String) { + let database = ncDatabase() + + do { + try database.write { + let results = database.objects(NextcloudLocalFileMetadataTable.self).filter("ocId == %@", ocId) + database.delete(results) + } + } catch let error { + Logger.ncFilesDatabase.error("Could not delete local file metadata with ocId: \(ocId, privacy: .public), received error: \(error.localizedDescription, privacy: .public)") + } + } + + private func sortedLocalFileMetadatas(_ metadatas: Results) -> [NextcloudLocalFileMetadataTable] { + let sortedMetadatas = metadatas.sorted(byKeyPath: "fileName", ascending: true) + return Array(sortedMetadatas.map { NextcloudLocalFileMetadataTable(value: $0) }) + } + + func localFileMetadatas(account: String) -> [NextcloudLocalFileMetadataTable] { + let results = ncDatabase().objects(NextcloudLocalFileMetadataTable.self).filter("account == %@", account) + return sortedLocalFileMetadatas(results) + } + + func localFileItemMetadatas(account: String) -> [NextcloudItemMetadataTable] { + let localFileMetadatas = localFileMetadatas(account: account) + let localFileMetadatasOcIds = Array(localFileMetadatas.map { $0.ocId }) + + var itemMetadatas: [NextcloudItemMetadataTable] = [] + + for ocId in localFileMetadatasOcIds { + guard let itemMetadata = itemMetadataFromOcId(ocId) else { + Logger.ncFilesDatabase.error("Could not find matching item metadata for local file metadata with ocId: \(ocId, privacy: .public) with request from account: \(account)") + continue; + } + + itemMetadatas.append(NextcloudItemMetadataTable(value: itemMetadata)) + } + + return itemMetadatas + } +} diff --git a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudFilesDatabaseManager.swift b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudFilesDatabaseManager.swift index 0541ff819..158f28111 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudFilesDatabaseManager.swift +++ b/shell_integration/MacOSX/NextcloudIntegration/FileProviderExt/Database/NextcloudFilesDatabaseManager.swift @@ -323,77 +323,4 @@ class NextcloudFilesDatabaseManager : NSObject { Logger.ncFilesDatabase.error("Could not get item parent directory item metadata for metadata. ocID: \(metadata.ocId, privacy: .public), etag: \(metadata.etag, privacy: .public), fileName: \(metadata.fileName, privacy: .public)") return nil } - - - func localFileMetadataFromOcId(_ ocId: String) -> NextcloudLocalFileMetadataTable? { - if let metadata = ncDatabase().objects(NextcloudLocalFileMetadataTable.self).filter("ocId == %@", ocId).first { - return NextcloudLocalFileMetadataTable(value: metadata) - } - - return nil - } - - func addLocalFileMetadataFromItemMetadata(_ itemMetadata: NextcloudItemMetadataTable) { - let database = ncDatabase() - - do { - try database.write { - let newLocalFileMetadata = NextcloudLocalFileMetadataTable() - - newLocalFileMetadata.ocId = itemMetadata.ocId - newLocalFileMetadata.fileName = itemMetadata.fileName - newLocalFileMetadata.account = itemMetadata.account - newLocalFileMetadata.etag = itemMetadata.etag - newLocalFileMetadata.exifDate = Date() - newLocalFileMetadata.exifLatitude = "-1" - newLocalFileMetadata.exifLongitude = "-1" - - database.add(newLocalFileMetadata, update: .all) - Logger.ncFilesDatabase.debug("Added local file metadata from item metadata. ocID: \(itemMetadata.ocId, privacy: .public), etag: \(itemMetadata.etag, privacy: .public), fileName: \(itemMetadata.fileName, privacy: .public)") - } - } catch let error { - Logger.ncFilesDatabase.error("Could not add local file metadata from item metadata. ocID: \(itemMetadata.ocId, privacy: .public), etag: \(itemMetadata.etag, privacy: .public), fileName: \(itemMetadata.fileName, privacy: .public), received error: \(error.localizedDescription, privacy: .public)") - } - } - - func deleteLocalFileMetadata(ocId: String) { - let database = ncDatabase() - - do { - try database.write { - let results = database.objects(NextcloudLocalFileMetadataTable.self).filter("ocId == %@", ocId) - database.delete(results) - } - } catch let error { - Logger.ncFilesDatabase.error("Could not delete local file metadata with ocId: \(ocId, privacy: .public), received error: \(error.localizedDescription, privacy: .public)") - } - } - - private func sortedLocalFileMetadatas(_ metadatas: Results) -> [NextcloudLocalFileMetadataTable] { - let sortedMetadatas = metadatas.sorted(byKeyPath: "fileName", ascending: true) - return Array(sortedMetadatas.map { NextcloudLocalFileMetadataTable(value: $0) }) - } - - func localFileMetadatas(account: String) -> [NextcloudLocalFileMetadataTable] { - let results = ncDatabase().objects(NextcloudLocalFileMetadataTable.self).filter("account == %@", account) - return sortedLocalFileMetadatas(results) - } - - func localFileItemMetadatas(account: String) -> [NextcloudItemMetadataTable] { - let localFileMetadatas = localFileMetadatas(account: account) - let localFileMetadatasOcIds = Array(localFileMetadatas.map { $0.ocId }) - - var itemMetadatas: [NextcloudItemMetadataTable] = [] - - for ocId in localFileMetadatasOcIds { - guard let itemMetadata = itemMetadataFromOcId(ocId) else { - Logger.ncFilesDatabase.error("Could not find matching item metadata for local file metadata with ocId: \(ocId, privacy: .public) with request from account: \(account)") - continue; - } - - itemMetadatas.append(NextcloudItemMetadataTable(value: itemMetadata)) - } - - return itemMetadatas - } } diff --git a/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj b/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj index 4c4d2f101..16c541a4a 100644 --- a/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj +++ b/shell_integration/MacOSX/NextcloudIntegration/NextcloudIntegration.xcodeproj/project.pbxproj @@ -16,6 +16,7 @@ 5318AD9729BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9629BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift */; }; 5318AD9929BF58D000CBB71C /* NKError+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5318AD9829BF58D000CBB71C /* NKError+Extensions.swift */; }; 5352B36629DC14970011CE03 /* NextcloudFilesDatabaseManager+Directories.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5352B36529DC14970011CE03 /* NextcloudFilesDatabaseManager+Directories.swift */; }; + 5352B36829DC17D60011CE03 /* NextcloudFilesDatabaseManager+LocalFiles.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5352B36729DC17D60011CE03 /* NextcloudFilesDatabaseManager+LocalFiles.swift */; }; 5352E85B29B7BFE6002CE85C /* Progress+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5352E85A29B7BFE6002CE85C /* Progress+Extensions.swift */; }; 535AE30E29C0A2CC0042A9BA /* Logger+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 535AE30D29C0A2CC0042A9BA /* Logger+Extensions.swift */; }; 536EFBF7295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */; }; @@ -144,6 +145,7 @@ 5318AD9629BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderMaterialisedEnumerationObserver.swift; sourceTree = ""; }; 5318AD9829BF58D000CBB71C /* NKError+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NKError+Extensions.swift"; sourceTree = ""; }; 5352B36529DC14970011CE03 /* NextcloudFilesDatabaseManager+Directories.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NextcloudFilesDatabaseManager+Directories.swift"; sourceTree = ""; }; + 5352B36729DC17D60011CE03 /* NextcloudFilesDatabaseManager+LocalFiles.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NextcloudFilesDatabaseManager+LocalFiles.swift"; sourceTree = ""; }; 5352E85A29B7BFE6002CE85C /* Progress+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Progress+Extensions.swift"; sourceTree = ""; }; 535AE30D29C0A2CC0042A9BA /* Logger+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Logger+Extensions.swift"; sourceTree = ""; }; 536EFBF6295CF58100F4CB13 /* FileProviderSocketLineProcessor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileProviderSocketLineProcessor.swift; sourceTree = ""; }; @@ -226,6 +228,7 @@ children = ( 5307A6F129675346001E0C6A /* NextcloudFilesDatabaseManager.swift */, 5352B36529DC14970011CE03 /* NextcloudFilesDatabaseManager+Directories.swift */, + 5352B36729DC17D60011CE03 /* NextcloudFilesDatabaseManager+LocalFiles.swift */, 5318AD9029BF42FB00CBB71C /* NextcloudItemMetadataTable.swift */, 53ED472729C88E7000795DB1 /* NextcloudItemMetadataTable+NKFile.swift */, 5318AD9429BF438F00CBB71C /* NextcloudLocalFileMetadataTable.swift */, @@ -582,6 +585,7 @@ 5307A6F229675346001E0C6A /* NextcloudFilesDatabaseManager.swift in Sources */, 53D056312970594F00988392 /* LocalFilesUtils.swift in Sources */, 538E396F27F4765000FA63D5 /* FileProviderItem.swift in Sources */, + 5352B36829DC17D60011CE03 /* NextcloudFilesDatabaseManager+LocalFiles.swift in Sources */, 5318AD9129BF42FB00CBB71C /* NextcloudItemMetadataTable.swift in Sources */, 5352B36629DC14970011CE03 /* NextcloudFilesDatabaseManager+Directories.swift in Sources */, 5318AD9729BF493600CBB71C /* FileProviderMaterialisedEnumerationObserver.swift in Sources */, -- 2.30.2