Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interceptor & Monitor Improved #3348

Merged
merged 13 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions File Provider Extension/FileProviderExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -254,13 +254,15 @@ class FileProviderExtension: NSFileProviderExtension {
selector: "",
status: NCGlobal.shared.metadataStatusUploading)

if let task = NKBackground(nkCommonInstance: NextcloudKit.shared.nkCommonInstance).upload(serverUrlFileName: serverUrlFileName,
let (task, error) = NKBackground(nkCommonInstance: NextcloudKit.shared.nkCommonInstance).upload(serverUrlFileName: serverUrlFileName,
fileNameLocalPath: url.path,
dateCreationFile: nil,
dateModificationFile: nil,
overwrite: true,
account: metadata.account,
sessionIdentifier: NCNetworking.shared.sessionUploadBackgroundExt) {
sessionIdentifier: NCNetworking.shared.sessionUploadBackgroundExt)

if let task, error == .success {
self.database.setMetadataSession(ocId: metadata.ocId,
sessionTaskIdentifier: task.taskIdentifier,
status: NCGlobal.shared.metadataStatusUploading)
Expand Down Expand Up @@ -292,7 +294,7 @@ class FileProviderExtension: NSFileProviderExtension {
return completionHandler(nil, NSFileProviderError(.noSuchItem))
}
var size = 0 as Int64
var error: NSError?
var errorCoordinator: NSError?
_ = fileURL.startAccessingSecurityScopedResource()
// typefile directory ? (NOT PERMITTED)
do {
Expand All @@ -309,7 +311,7 @@ class FileProviderExtension: NSFileProviderExtension {
let fileName = self.utilityFileSystem.createFileName(fileURL.lastPathComponent, serverUrl: tableDirectory.serverUrl, account: fileProviderData.shared.session.account)
let ocIdTransfer = NSUUID().uuidString.lowercased()

NSFileCoordinator().coordinate(readingItemAt: fileURL, options: .withoutChanges, error: &error) { url in
NSFileCoordinator().coordinate(readingItemAt: fileURL, options: .withoutChanges, error: &errorCoordinator) { url in
self.providerUtility.copyFile(url.path, toPath: self.utilityFileSystem.getDirectoryProviderStorageOcId(ocIdTransfer, fileNameView: fileName))
}

Expand All @@ -333,13 +335,14 @@ class FileProviderExtension: NSFileProviderExtension {
let serverUrlFileName = tableDirectory.serverUrl + "/" + fileName
let fileNameLocalPath = self.utilityFileSystem.getDirectoryProviderStorageOcId(ocIdTransfer, fileNameView: fileName)

if let task = NKBackground(nkCommonInstance: NextcloudKit.shared.nkCommonInstance).upload(serverUrlFileName: serverUrlFileName,
fileNameLocalPath: fileNameLocalPath,
dateCreationFile: nil,
dateModificationFile: nil,
overwrite: true,
account: metadataForUpload.account,
sessionIdentifier: metadataForUpload.session) {
let (task, error) = NKBackground(nkCommonInstance: NextcloudKit.shared.nkCommonInstance).upload(serverUrlFileName: serverUrlFileName,
fileNameLocalPath: fileNameLocalPath,
dateCreationFile: nil,
dateModificationFile: nil,
overwrite: true,
account: metadataForUpload.account,
sessionIdentifier: metadataForUpload.session)
if let task, error == .success {
self.database.setMetadataSession(ocId: metadataForUpload.ocId,
sessionTaskIdentifier: task.taskIdentifier,
status: NCGlobal.shared.metadataStatusUploading)
Expand Down
8 changes: 6 additions & 2 deletions Nextcloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,7 @@
F783030728B4C52800B84583 /* UIColor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70CEF5523E9C7E50007035B /* UIColor+Extension.swift */; };
F783030D28B4C59A00B84583 /* SwiftEntryKit in Frameworks */ = {isa = PBXBuildFile; productRef = F783030C28B4C59A00B84583 /* SwiftEntryKit */; };
F783034428B5142B00B84583 /* NextcloudKit in Frameworks */ = {isa = PBXBuildFile; productRef = F783034328B5142B00B84583 /* NextcloudKit */; };
F785129C2D7989B30087DDD0 /* NCNetworking+TermsOfService.swift in Sources */ = {isa = PBXBuildFile; fileRef = F785129A2D79899E0087DDD0 /* NCNetworking+TermsOfService.swift */; };
F785EE9D246196DF00B3F945 /* NCNetworkingE2EE.swift in Sources */ = {isa = PBXBuildFile; fileRef = F785EE9C246196DF00B3F945 /* NCNetworkingE2EE.swift */; };
F785EE9E2461A09900B3F945 /* NCNetworking.swift in Sources */ = {isa = PBXBuildFile; fileRef = F75A9EE523796C6F0044CFCE /* NCNetworking.swift */; };
F785EEA42461A4A600B3F945 /* NCUtility.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70BFC7320E0FA7C00C67599 /* NCUtility.swift */; };
Expand Down Expand Up @@ -1584,6 +1585,7 @@
F7802B312BD5584F00D74270 /* NCMedia+DragDrop.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NCMedia+DragDrop.swift"; sourceTree = "<group>"; };
F7816EF12C2C3E1F00A52517 /* NCPushNotification.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCPushNotification.swift; sourceTree = "<group>"; };
F7817CF729801A3500FFBC65 /* Data+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Data+Extension.swift"; sourceTree = "<group>"; };
F785129A2D79899E0087DDD0 /* NCNetworking+TermsOfService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NCNetworking+TermsOfService.swift"; sourceTree = "<group>"; };
F785EE9C246196DF00B3F945 /* NCNetworkingE2EE.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCNetworkingE2EE.swift; sourceTree = "<group>"; };
F7864ACB2A78FE73004870E0 /* NCManageDatabase+LocalFile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NCManageDatabase+LocalFile.swift"; sourceTree = "<group>"; };
F787704E22E7019900F287A9 /* NCShareLinkCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = NCShareLinkCell.xib; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2417,6 +2419,7 @@
F7327E1F2B73A42F00A462C7 /* NCNetworking+Download.swift */,
F7327E342B73AEDE00A462C7 /* NCNetworking+LivePhoto.swift */,
F7F3E58A2D3BB65000A32B14 /* NCNetworking+Recommendations.swift */,
F785129A2D79899E0087DDD0 /* NCNetworking+TermsOfService.swift */,
F7327E3C2B73B92800A462C7 /* NCNetworking+Synchronization.swift */,
F74230F22C79B57200CA1ACA /* NCNetworking+Task.swift */,
F7327E272B73A53400A462C7 /* NCNetworking+Upload.swift */,
Expand Down Expand Up @@ -4760,6 +4763,7 @@
F78E2D6529AF02DB0024D4F3 /* Database.swift in Sources */,
F70CEF5623E9C7E50007035B /* UIColor+Extension.swift in Sources */,
F76882242C0DD1E7001CF441 /* NCSettingsAdvancedView.swift in Sources */,
F785129C2D7989B30087DDD0 /* NCNetworking+TermsOfService.swift in Sources */,
F75CA1472962F13700B01130 /* NCHUDView.swift in Sources */,
F77BB748289985270090FC19 /* UITabBarController+Extension.swift in Sources */,
F763D29D2A249C4500A3C901 /* NCManageDatabase+Capabilities.swift in Sources */,
Expand Down Expand Up @@ -5777,7 +5781,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEBUG_INFORMATION_FORMAT = dwarf;
DEVELOPMENT_TEAM = NKUJUXUJ3B;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand Down Expand Up @@ -5843,7 +5847,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
CURRENT_PROJECT_VERSION = 5;
CURRENT_PROJECT_VERSION = 6;
DEVELOPMENT_TEAM = NKUJUXUJ3B;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
Expand Down
85 changes: 57 additions & 28 deletions iOSClient/Main/NCMainTabBarController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ class NCMainTabBarController: UITabBarController {
var documentPickerViewController: NCDocumentPickerViewController?
let navigationCollectionViewCommon = ThreadSafeArray<NavigationCollectionViewCommon>()
private var previousIndex: Int?
private var timerProcess: Timer?
private let groupDefaults = UserDefaults(suiteName: NCBrandOptions.shared.capabilitiesGroup)
private var unauthorizedAccountInProgress: Bool = false
private var unavailableAccountInProgress: Bool = false
private var checkUserDelaultErrorInProgress: Bool = false
private var timerProcess: Timer?

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -51,6 +50,14 @@ class NCMainTabBarController: UITabBarController {
traitOverrides.horizontalSizeClass = .compact
}

self.timerProcess = Timer.scheduledTimer(withTimeInterval: 1, repeats: true, block: { _ in
guard UIApplication.shared.applicationState == .active else {
return
}

self.checkUserDelaultError()
})

NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterChangeTheming), object: nil, queue: .main) { [weak self] notification in
if let userInfo = notification.userInfo as? NSDictionary,
let account = userInfo["account"] as? String,
Expand All @@ -63,13 +70,25 @@ class NCMainTabBarController: UITabBarController {
}
}

NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterCheckUserDelaultErrorDone), object: nil, queue: nil) { notification in
if let userInfo = notification.userInfo,
let account = userInfo["account"] as? String,
let controller = userInfo["controller"] as? NCMainTabBarController,
account == self.account,
controller == self {
self.checkUserDelaultErrorInProgress = false
}
}

/*
NotificationCenter.default.addObserver(forName: UIApplication.willEnterForegroundNotification, object: nil, queue: nil) { [weak self] _ in
self?.userDefaultsDidChange()
}

NotificationCenter.default.addObserver(forName: UserDefaults.didChangeNotification, object: nil, queue: nil) { [weak self] _ in
self?.userDefaultsDidChange()
}
*/
}

override func viewDidAppear(_ animated: Bool) {
Expand Down Expand Up @@ -100,35 +119,45 @@ class NCMainTabBarController: UITabBarController {
return serverUrl
}

private func userDefaultsDidChange() {
let groupDefaults = UserDefaults(suiteName: NCBrandOptions.shared.capabilitiesGroup)
let unauthorizedArray = groupDefaults?.array(forKey: "Unauthorized") as? [String] ?? []
var unavailableArray = groupDefaults?.array(forKey: "Unavailable") as? [String] ?? []
let session = NCSession.shared.getSession(account: self.account)

if unauthorizedArray.contains(account) {
guard !unauthorizedAccountInProgress else { return }
unauthorizedAccountInProgress = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.checkRemoteUser {
self.unauthorizedAccountInProgress = false
private func checkUserDelaultError() {
guard !checkUserDelaultErrorInProgress else { return }

var unavailableArray = groupDefaults?.array(forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnavailable) as? [String] ?? []
let unauthorizedArray = groupDefaults?.array(forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnauthorized) as? [String] ?? []
let tosArray = groupDefaults?.array(forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsToS) as? [String] ?? []

/// Unavailable
if unavailableArray.contains(account) {
checkUserDelaultErrorInProgress = true
let serverUrl = NCSession.shared.getSession(account: account).urlBase
let account = self.account
NextcloudKit.shared.getServerStatus(serverUrl: serverUrl) { _, serverInfoResult in
switch serverInfoResult {
case .success(let serverInfo):

unavailableArray.removeAll { $0 == account }
self.groupDefaults?.set(unavailableArray, forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnavailable)

if serverInfo.maintenance {
NCContentPresenter().showInfo(title: "_warning_", description: "_maintenance_mode_")
}
case .failure:
break
}
self.checkUserDelaultErrorInProgress = false
}
} else if unavailableArray.contains(self.account) {
guard !unavailableAccountInProgress else { return }
unavailableAccountInProgress = true
Task {
let serverUrlFileName = NCUtilityFileSystem().getHomeServer(session: session)
let options = NKRequestOptions(checkUnauthorized: false)
let results = await NCNetworking.shared.readFileOrFolder(serverUrlFileName: serverUrlFileName, depth: "0", showHiddenFiles: NCKeychain().showHiddenFiles, account: self.account, options: options)
if results.error == .success {
unavailableArray.removeAll { $0 == results.account }
groupDefaults?.set(unavailableArray, forKey: "Unavailable")
} else {
NCContentPresenter().showWarning(error: results.error, priority: .max)
/// Unauthorized
} else if unauthorizedArray.contains(account) {
checkUserDelaultErrorInProgress = true
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
self.checkRemoteUser {
self.checkUserDelaultErrorInProgress = false
}
unavailableAccountInProgress = false
}
/// ToS
} else if tosArray.contains(account) {
checkUserDelaultErrorInProgress = true
NCNetworking.shared.termsOfService(account: account)
}
}

Expand Down
12 changes: 8 additions & 4 deletions iOSClient/NCAccount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,18 @@ class NCAccount: NSObject {
/// Remove account in groupDefaults
///
if let groupDefaults = UserDefaults(suiteName: NCBrandOptions.shared.capabilitiesGroup) {
var unauthorizedArray = groupDefaults.array(forKey: "Unauthorized") as? [String] ?? []
var unavailableArray = groupDefaults.array(forKey: "Unavailable") as? [String] ?? []
var unauthorizedArray = groupDefaults.array(forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnauthorized) as? [String] ?? []
var unavailableArray = groupDefaults.array(forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnavailable) as? [String] ?? []
var tosArray = groupDefaults.array(forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsToS) as? [String] ?? []

unauthorizedArray.removeAll { $0 == account }
groupDefaults.set(unauthorizedArray, forKey: "Unauthorized")
groupDefaults.set(unauthorizedArray, forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnauthorized)

unavailableArray.removeAll { $0 == account }
groupDefaults.set(unavailableArray, forKey: "Unavailable")
groupDefaults.set(unavailableArray, forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsUnavailable)

tosArray.removeAll { $0 == account }
groupDefaults.set(tosArray, forKey: NextcloudKit.shared.nkCommonInstance.groupDefaultsToS)

groupDefaults.synchronize()
}
Expand Down
1 change: 1 addition & 0 deletions iOSClient/NCGlobal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ final class NCGlobal: Sendable {
let notificationCenterReloadHeader = "reloadHeader"
let notificationCenterClearCache = "clearCache"
let notificationCenterChangeLayout = "changeLayout" // userInfo: account, serverUrl, layoutForView
let notificationCenterCheckUserDelaultErrorDone = "checkUserDelaultErrorDone" // userInfo: account, controller

let notificationCenterReloadDataSource = "reloadDataSource" // userInfo: serverUrl?, clearDataSource
let notificationCenterGetServerData = "getServerData" // userInfo: serverUrl?
Expand Down
Loading
Loading