Skip to content

Commit d7882e2

Browse files
committed
WIP
Signed-off-by: Milen Pivchev <milen.pivchev@gmail.com>
1 parent 34989ff commit d7882e2

4 files changed

Lines changed: 200 additions & 182 deletions

File tree

Sources/NextcloudKit/Models/NKDataFileXML.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -464,11 +464,8 @@ public class NKDataFileXML: NSObject {
464464
file.lockTimeOut = file.lockTime?.addingTimeInterval(TimeInterval(lockTimeOut))
465465
}
466466

467-
let tagsElements = propstat["d:prop", "nc:system-tags"]
468-
for element in tagsElements["nc:system-tag"] {
469-
guard let tag = element.text else { continue }
470-
file.tags.append(tag)
471-
}
467+
let tags: [NKTag] = NKTag.parse(systemTagElements: propstat["d:prop", "nc:system-tags", "nc:system-tag"])
468+
file.tags.append(contentsOf: tags)
472469

473470
// NC27 -----
474471
if let latitude = propstat["d:prop", "nc:file-metadata-gps", "latitude"].double {

Sources/NextcloudKit/Models/NKFile.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public struct NKFile: Sendable {
5656
public var shareType: [Int]
5757
public var size: Int64
5858
public var serverUrl: String
59-
public var tags: [String]
59+
public var tags: [NKTag]
6060
public var trashbinFileName: String
6161
public var trashbinOriginalLocation: String
6262
public var trashbinDeletionTime: Date
@@ -128,7 +128,7 @@ public struct NKFile: Sendable {
128128
shareType: [Int] = [],
129129
size: Int64 = 0,
130130
serverUrl: String = "",
131-
tags: [String] = [],
131+
tags: [NKTag] = [],
132132
trashbinFileName: String = "",
133133
trashbinOriginalLocation: String = "",
134134
trashbinDeletionTime: Date = Date(),

Sources/NextcloudKit/Models/NKTag.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// SPDX-License-Identifier: GPL-3.0-or-later
44

55
import Foundation
6+
import SwiftyXMLParser
67

78
public struct NKTag: Sendable, Equatable, Hashable {
89
public let id: String
@@ -14,4 +15,48 @@ public struct NKTag: Sendable, Equatable, Hashable {
1415
self.name = name
1516
self.color = color
1617
}
18+
19+
static func parse(xmlData: Data) -> [NKTag] {
20+
let xml = XML.parse(xmlData)
21+
let responses = xml["d:multistatus", "d:response"]
22+
var tags: [NKTag] = []
23+
24+
for response in responses {
25+
let propstat = response["d:propstat"][0]
26+
guard let id = propstat["d:prop", "oc:id"].text,
27+
let name = propstat["d:prop", "oc:display-name"].text else {
28+
continue
29+
}
30+
31+
let color = normalizedColor(propstat["d:prop", "nc:color"].text)
32+
33+
tags.append(NKTag(id: id, name: name, color: color))
34+
}
35+
36+
return tags
37+
}
38+
39+
static func parse(systemTagElements: XML.Accessor) -> [NKTag] {
40+
var tags: [NKTag] = []
41+
42+
for element in systemTagElements {
43+
guard let name = element.text?.trimmingCharacters(in: .whitespacesAndNewlines),
44+
!name.isEmpty else {
45+
continue
46+
}
47+
48+
let id = element.attributes["oc:id"] ?? ""
49+
let color = normalizedColor(element.attributes["nc:color"])
50+
tags.append(NKTag(id: id, name: name, color: color))
51+
}
52+
53+
return tags
54+
}
55+
56+
private static func normalizedColor(_ rawValue: String?) -> String? {
57+
guard let rawValue, !rawValue.isEmpty else {
58+
return nil
59+
}
60+
return rawValue.hasPrefix("#") ? rawValue : "#\(rawValue)"
61+
}
1762
}

0 commit comments

Comments
 (0)