From 106ad2cec00b9d865519c7c50b301354629acc35 Mon Sep 17 00:00:00 2001 From: Marino Faggiana Date: Fri, 20 Dec 2024 14:36:52 +0100 Subject: [PATCH] cod Signed-off-by: Marino Faggiana --- .../Models/NKRecommendedFiles.swift | 74 +++++++++++++++++++ .../NextcloudKit+RecommendedFiles.swift | 53 +++++++++++++ 2 files changed, 127 insertions(+) create mode 100644 Sources/NextcloudKit/Models/NKRecommendedFiles.swift create mode 100644 Sources/NextcloudKit/NextcloudKit+RecommendedFiles.swift diff --git a/Sources/NextcloudKit/Models/NKRecommendedFiles.swift b/Sources/NextcloudKit/Models/NKRecommendedFiles.swift new file mode 100644 index 0000000..10d92a2 --- /dev/null +++ b/Sources/NextcloudKit/Models/NKRecommendedFiles.swift @@ -0,0 +1,74 @@ +// SPDX-FileCopyrightText: Nextcloud GmbH +// SPDX-FileCopyrightText: 2024 Marino Faggiana +// SPDX-License-Identifier: GPL-3.0-or-later + +import Foundation +import SwiftyXMLParser + +public class NKRecommendation { + var id: String + var timestamp: Date? + var name: String + var directory: String + var extensionType: String + var mimeType: String + var hasPreview: Bool + var reason: String + + init(id: String, timestamp: Date?, name: String, directory: String, extensionType: String, mimeType: String, hasPreview: Bool, reason: String) { + self.id = id + self.timestamp = timestamp + self.name = name + self.directory = directory + self.extensionType = extensionType + self.mimeType = mimeType + self.hasPreview = hasPreview + self.reason = reason + } +} + +class XMLToRecommendationParser { + func parse(xml: String) -> [NKRecommendation]? { + guard let data = xml.data(using: .utf8) else { return nil } + let xml = XML.parse(data) + + // Parsing "enabled" + guard let enabledString = xml["ocs", "data", "enabled"].text, + Bool(enabledString == "1") + else { + return nil + } + + // Parsing "recommendations" + var recommendations: [NKRecommendation] = [] + let elements = xml["ocs", "data", "recommendations", "element"] + + for element in elements { + let id = element["id"].text ?? "" + var timestamp: Date? + if let timestampDouble = element["timestamp"].double, timestampDouble > 0 { + timestamp = Date(timeIntervalSince1970: timestampDouble) + } + let name = element["name"].text ?? "" + let directory = element["directory"].text ?? "" + let extensionType = element["extension"].text ?? "" + let mimeType = element["mimeType"].text ?? "" + let hasPreview = element["hasPreview"].text == "1" + let reason = element["reason"].text ?? "" + + let recommendation = NKRecommendation( + id: id, + timestamp: timestamp, + name: name, + directory: directory, + extensionType: extensionType, + mimeType: mimeType, + hasPreview: hasPreview, + reason: reason + ) + recommendations.append(recommendation) + } + + return recommendations + } +} diff --git a/Sources/NextcloudKit/NextcloudKit+RecommendedFiles.swift b/Sources/NextcloudKit/NextcloudKit+RecommendedFiles.swift new file mode 100644 index 0000000..172fb58 --- /dev/null +++ b/Sources/NextcloudKit/NextcloudKit+RecommendedFiles.swift @@ -0,0 +1,53 @@ +// SPDX-FileCopyrightText: Nextcloud GmbH +// SPDX-FileCopyrightText: 2024 Marino Faggiana +// SPDX-License-Identifier: GPL-3.0-or-later + +import Foundation +import Alamofire +import SwiftyJSON + +public extension NextcloudKit { + func getRecommendedFiles(account: String, + options: NKRequestOptions = NKRequestOptions(), + request: @escaping (DataRequest?) -> Void = { _ in }, + taskHandler: @escaping (_ task: URLSessionTask) -> Void = { _ in }, + completion: @escaping (_ account: String, _ recommendations: [NKRecommendation]?, _ responseData: AFDataResponse?, _ error: NKError) -> Void) { + let endpoint = "ocs/v2.php/apps/recommendations/api/v1/recommendations" + /// + options.contentType = "application/xml" + /// + guard let nkSession = nkCommonInstance.getSession(account: account), + let url = nkCommonInstance.createStandardUrl(serverUrl: nkSession.urlBase, endpoint: endpoint, options: options), + let headers = nkCommonInstance.getStandardHeaders(account: account, options: options) else { + return options.queue.async { completion(account, nil, nil, .urlError) } + } + + let tosRequest = nkSession.sessionData.request(url, method: .get, encoding: URLEncoding.default, headers: headers, interceptor: nil).validate(statusCode: 200..<300).onURLSessionTaskCreation { task in + task.taskDescription = options.taskDescription + taskHandler(task) + }.responseData(queue: self.nkCommonInstance.backgroundQueue) { response in + if self.nkCommonInstance.levelLog > 0 { + debugPrint(response) + } + switch response.result { + case .success(let data): + if let xmlString = String(data: data, encoding: .utf8) { + let parser = XMLToRecommendationParser() + if let recommendations = parser.parse(xml: xmlString) { + options.queue.async { completion(account, recommendations, response, .success) } + } else { + options.queue.async { completion(account, nil, response, .xmlError) } + } + } else { + options.queue.async { completion(account, nil, response, .xmlError) } + } + case .failure(let error): + let error = NKError(error: error, afResponse: response, responseData: response.data) + options.queue.async { + completion(account, nil, response, error) + } + } + } + options.queue.async { request(tosRequest) } + } +}