From ff60c0906db3becfc75ad303045f15e880b27638 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Wed, 28 Jan 2026 10:27:34 +0100 Subject: [PATCH] added searchMedia Signed-off-by: Marino Faggiana --- Nextcloud.xcodeproj/project.pbxproj | 8 +- iOSClient/Media/NCMedia+Netwoking.swift | 117 ++++++++++++++++++++++++ iOSClient/Media/NCMediaDataSource.swift | 14 +-- 3 files changed, 130 insertions(+), 9 deletions(-) create mode 100644 iOSClient/Media/NCMedia+Netwoking.swift diff --git a/Nextcloud.xcodeproj/project.pbxproj b/Nextcloud.xcodeproj/project.pbxproj index 0a34015889..4c951b95c0 100644 --- a/Nextcloud.xcodeproj/project.pbxproj +++ b/Nextcloud.xcodeproj/project.pbxproj @@ -259,6 +259,7 @@ F719D9E0288D37A300762E33 /* NCColorPicker.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F719D9DF288D37A300762E33 /* NCColorPicker.storyboard */; }; F719D9E2288D396100762E33 /* NCColorPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = F719D9E1288D396100762E33 /* NCColorPicker.swift */; }; F71CD6CA2930D7B1006C95C1 /* NCApplicationHandle.swift in Sources */ = {isa = PBXBuildFile; fileRef = F71CD6C92930D7B1006C95C1 /* NCApplicationHandle.swift */; }; + F71CFA672F2A07DB007A3AE9 /* NCMedia+Netwoking.swift in Sources */ = {isa = PBXBuildFile; fileRef = F71CFA662F2A07C6007A3AE9 /* NCMedia+Netwoking.swift */; }; F71D2FB72E09BBD700B751CC /* NCAutoUploadModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = F71D2FB62E09BBD700B751CC /* NCAutoUploadModel.swift */; }; F71F6D072B6A6A5E00F1EB15 /* ThreadSafeArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = F71F6D062B6A6A5E00F1EB15 /* ThreadSafeArray.swift */; }; F71F6D082B6A6A5E00F1EB15 /* ThreadSafeArray.swift in Sources */ = {isa = PBXBuildFile; fileRef = F71F6D062B6A6A5E00F1EB15 /* ThreadSafeArray.swift */; }; @@ -1337,6 +1338,7 @@ F719D9DF288D37A300762E33 /* NCColorPicker.storyboard */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; path = NCColorPicker.storyboard; sourceTree = ""; }; F719D9E1288D396100762E33 /* NCColorPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCColorPicker.swift; sourceTree = ""; }; F71CD6C92930D7B1006C95C1 /* NCApplicationHandle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCApplicationHandle.swift; sourceTree = ""; }; + F71CFA662F2A07C6007A3AE9 /* NCMedia+Netwoking.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NCMedia+Netwoking.swift"; sourceTree = ""; }; F71D2FB62E09BBD700B751CC /* NCAutoUploadModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCAutoUploadModel.swift; sourceTree = ""; }; F71F6D062B6A6A5E00F1EB15 /* ThreadSafeArray.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThreadSafeArray.swift; sourceTree = ""; }; F722133A2D40EF8C002F7438 /* NCFilesNavigationController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCFilesNavigationController.swift; sourceTree = ""; }; @@ -3161,12 +3163,13 @@ F72408322B8A27C900F128E2 /* NCMedia+Command.swift */, F7802B312BD5584F00D74270 /* NCMedia+DragDrop.swift */, F7BD0A032C4689E9003A4A6D /* NCMedia+MediaLayout.swift */, + F71CFA662F2A07C6007A3AE9 /* NCMedia+Netwoking.swift */, F7E2B64E2DDCC5C30075B4D0 /* NCMedia+TransferDelegate.swift */, F78B87E62B62527100C65ADC /* NCMediaDataSource.swift */, - F79699E62E689F68000EC82A /* NCMediaNavigationController.swift */, - F7D60CAE2C941ACB008FBFDD /* NCMediaPinchGesture.swift */, F78B87E82B62550800C65ADC /* NCMediaDownloadThumbnail.swift */, F755CB3F2B8CB13C00CE27E9 /* NCMediaLayout.swift */, + F79699E62E689F68000EC82A /* NCMediaNavigationController.swift */, + F7D60CAE2C941ACB008FBFDD /* NCMediaPinchGesture.swift */, F741C2232B6B9FD600E849BB /* NCMediaSelectTabBar.swift */, ); path = Media; @@ -4559,6 +4562,7 @@ F7D4BF4D2CA2E8D800A5E746 /* TOPasscodeViewController.m in Sources */, F75C0C4823D1FAE300163CC8 /* NCRichWorkspaceCommon.swift in Sources */, AAE330042D2ED20200B04903 /* NCShareNavigationTitleSetting.swift in Sources */, + F71CFA672F2A07DB007A3AE9 /* NCMedia+Netwoking.swift in Sources */, F7DF7B422F1A36C100514020 /* HelperBanner.swift in Sources */, F78ACD4A21903F850088454D /* NCTrashListCell.swift in Sources */, F7386E482DA90E0F009A00F6 /* NCAppVersionManager.swift in Sources */, diff --git a/iOSClient/Media/NCMedia+Netwoking.swift b/iOSClient/Media/NCMedia+Netwoking.swift new file mode 100644 index 0000000000..dbe8e8b9d7 --- /dev/null +++ b/iOSClient/Media/NCMedia+Netwoking.swift @@ -0,0 +1,117 @@ +// SPDX-FileCopyrightText: Nextcloud GmbH +// SPDX-FileCopyrightText: 2026 Marino Faggiana +// SPDX-License-Identifier: GPL-3.0-or-later + +import Foundation +import NextcloudKit +import Alamofire + +extension NCMedia { + func searchMediaAsync(path: String = "", + lessDate: Any, + greaterDate: Any, + elementDate: String, + limit: Int, + account: String, + options: NKRequestOptions = NKRequestOptions(), + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in } + ) async -> (account: String, files: [NKFile]?, error: NKError) { + guard let nkSession = NextcloudKit.shared.nkCommonInstance.nksessions.session(forAccount: account) else { + return (account, nil, .urlError) + } + let files: [NKFile] = [] + let elementDate = elementDate + "/" + var greaterDateString: String?, lessDateString: String? + let href = "/files/" + nkSession.userId + path + if let lessDate = lessDate as? Date { + lessDateString = lessDate.formatted(using: "yyyy-MM-dd'T'HH:mm:ssZZZZZ") + } else if let lessDate = lessDate as? Int { + lessDateString = String(lessDate) + } + if let greaterDate = greaterDate as? Date { + greaterDateString = greaterDate.formatted(using: "yyyy-MM-dd'T'HH:mm:ssZZZZZ") + } else if let greaterDate = greaterDate as? Int { + greaterDateString = String(greaterDate) + } + guard let lessDateString, let greaterDateString else { + return (account, files, .invalidDate) + } + + let httpBodyString = String(format: getRequestBodySearchMedia(createProperties: options.createProperties, removeProperties: options.removeProperties), href, elementDate, elementDate, lessDateString, elementDate, greaterDateString, String(limit)) + + guard let httpBody = httpBodyString.data(using: .utf8) else { + return (account, files, .invalidData) + } + + let results = await NextcloudKit.shared.searchAsync(serverUrl: nkSession.urlBase, httpBody: httpBody, showHiddenFiles: false, includeHiddenFiles: [], account: account, options: options, taskHandler: taskHandler) + + return(results.account, results.files, results.error) + } + + func getRequestBodySearchMedia(createProperties: [NKProperties]?, removeProperties: [NKProperties] = []) -> String { + let request = """ + + + + + + """ + NKProperties.properties(createProperties: createProperties, removeProperties: removeProperties) + """ + + + + + %@ + infinity + + + + + <%@> + + + + + + + + + + + + + image/%% + + + + video/%% + + + + + + <%@> + %@ + + + <%@> + %@ + + + + + + + %@ + + + + """ + return request + } +} + +extension Date { + func formatted(using format: String) -> String { + NKLogFileManager.shared.convertDate(self, format: format) + } +} diff --git a/iOSClient/Media/NCMediaDataSource.swift b/iOSClient/Media/NCMediaDataSource.swift index 25dbafe4ee..b51100090e 100644 --- a/iOSClient/Media/NCMediaDataSource.swift +++ b/iOSClient/Media/NCMediaDataSource.swift @@ -141,13 +141,13 @@ extension NCMedia { let options = NKRequestOptions(timeout: 180, taskDescription: self.global.taskDescriptionRetrievesProperties, queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue) - let result = await NextcloudKit.shared.searchMediaAsync(path: tblAccount.mediaPath, - lessDate: lessDateAny, - greaterDate: greaterDateAny, - elementDate: elementDate, - limit: limit, - account: self.session.account, - options: options) { task in + let result = await searchMediaAsync(path: tblAccount.mediaPath, + lessDate: lessDateAny, + greaterDate: greaterDateAny, + elementDate: elementDate, + limit: limit, + account: self.session.account, + options: options) { task in Task { let identifier = await NCNetworking.shared.networkingTasks.createIdentifier(account: self.session.account, name: "searchMedia")