Skip to content

Commit

Permalink
cod
Browse files Browse the repository at this point in the history
Signed-off-by: Marino Faggiana <[email protected]>
  • Loading branch information
marinofaggiana committed Dec 20, 2024
1 parent 0fd68b6 commit 106ad2c
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
74 changes: 74 additions & 0 deletions Sources/NextcloudKit/Models/NKRecommendedFiles.swift
Original file line number Diff line number Diff line change
@@ -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
}
}
53 changes: 53 additions & 0 deletions Sources/NextcloudKit/NextcloudKit+RecommendedFiles.swift
Original file line number Diff line number Diff line change
@@ -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<Data>?, _ 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) }
}
}

0 comments on commit 106ad2c

Please sign in to comment.