|
3 | 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
4 | 4 |
|
5 | 5 | import Foundation |
6 | | -import MobileCoreServices |
| 6 | +import UniformTypeIdentifiers |
7 | 7 |
|
8 | 8 | public class NKFileProperty: NSObject { |
9 | 9 | public var classFile: NKTypeClassFile = .unknow |
@@ -39,19 +39,36 @@ public enum NKTypeIconFile: String { |
39 | 39 | case xls = "xls" |
40 | 40 | } |
41 | 41 |
|
| 42 | +public enum NKTypeIdentifierResolver { |
| 43 | + |
| 44 | + public static func preferredFileExtension(for uti: String) -> String? { |
| 45 | + UTType(uti)?.preferredFilenameExtension |
| 46 | + } |
| 47 | + |
| 48 | + public static func typeIdentifier(forExtension ext: String) -> String? { |
| 49 | + UTType(filenameExtension: ext)?.identifier |
| 50 | + } |
| 51 | + |
| 52 | + public static func typeIdentifier(forMIME mime: String) -> String? { |
| 53 | + UTType(mimeType: mime)?.identifier |
| 54 | + } |
| 55 | +} |
| 56 | + |
42 | 57 | /// Class responsible for resolving NKFileProperty information from a given UTI. |
43 | 58 | public final class NKFilePropertyResolver { |
44 | 59 |
|
45 | 60 | public init() {} |
46 | 61 |
|
47 | | - public func resolve(inUTI: CFString, account: String) -> NKFileProperty { |
| 62 | + public func resolve(inUTI: String, account: String) -> NKFileProperty { |
48 | 63 | let fileProperty = NKFileProperty() |
49 | 64 | let typeIdentifier = inUTI as String |
50 | 65 | let capabilities = NKCapabilities.shared.getCapabilitiesBlocking(for: account) |
| 66 | + let utiString = inUTI as String |
51 | 67 |
|
52 | 68 | // Preferred extension |
53 | | - if let fileExtension = UTTypeCopyPreferredTagWithClass(inUTI, kUTTagClassFilenameExtension) { |
54 | | - fileProperty.ext = fileExtension.takeRetainedValue() as String |
| 69 | + if let type = UTType(utiString), |
| 70 | + let ext = type.preferredFilenameExtension { |
| 71 | + fileProperty.ext = ext |
55 | 72 | } |
56 | 73 |
|
57 | 74 | // Collabora Nextcloud Text Office |
@@ -99,49 +116,66 @@ public final class NKFilePropertyResolver { |
99 | 116 | } |
100 | 117 |
|
101 | 118 | // Well-known UTI type classifications |
102 | | - if UTTypeConformsTo(inUTI, kUTTypeImage) { |
103 | | - fileProperty.classFile = .image |
104 | | - fileProperty.iconName = .image |
105 | | - fileProperty.name = "image" |
106 | | - } else if UTTypeConformsTo(inUTI, kUTTypeMovie) { |
107 | | - fileProperty.classFile = .video |
108 | | - fileProperty.iconName = .video |
109 | | - fileProperty.name = "movie" |
110 | | - } else if UTTypeConformsTo(inUTI, kUTTypeAudio) { |
111 | | - fileProperty.classFile = .audio |
112 | | - fileProperty.iconName = .audio |
113 | | - fileProperty.name = "audio" |
114 | | - } else if UTTypeConformsTo(inUTI, kUTTypeZipArchive) { |
115 | | - fileProperty.classFile = .compress |
116 | | - fileProperty.iconName = .compress |
117 | | - fileProperty.name = "archive" |
118 | | - } else if UTTypeConformsTo(inUTI, kUTTypeHTML) { |
119 | | - fileProperty.classFile = .document |
120 | | - fileProperty.iconName = .code |
121 | | - fileProperty.name = "code" |
122 | | - } else if UTTypeConformsTo(inUTI, kUTTypePDF) { |
123 | | - fileProperty.classFile = .document |
124 | | - fileProperty.iconName = .pdf |
125 | | - fileProperty.name = "document" |
126 | | - } else if UTTypeConformsTo(inUTI, kUTTypeRTF) { |
127 | | - fileProperty.classFile = .document |
128 | | - fileProperty.iconName = .txt |
129 | | - fileProperty.name = "document" |
130 | | - } else if UTTypeConformsTo(inUTI, kUTTypeText) { |
131 | | - if fileProperty.ext.isEmpty { fileProperty.ext = "txt" } |
132 | | - fileProperty.classFile = .document |
133 | | - fileProperty.iconName = .txt |
134 | | - fileProperty.name = "text" |
135 | | - } else { |
136 | | - if UTTypeConformsTo(inUTI, kUTTypeContent) { |
| 119 | + if let type = UTType(utiString) { |
| 120 | + if type.conforms(to: .image) { |
| 121 | + fileProperty.classFile = .image |
| 122 | + fileProperty.iconName = .image |
| 123 | + fileProperty.name = "image" |
| 124 | + |
| 125 | + } else if type.conforms(to: .movie) { |
| 126 | + fileProperty.classFile = .video |
| 127 | + fileProperty.iconName = .video |
| 128 | + fileProperty.name = "movie" |
| 129 | + |
| 130 | + } else if type.conforms(to: .audio) { |
| 131 | + fileProperty.classFile = .audio |
| 132 | + fileProperty.iconName = .audio |
| 133 | + fileProperty.name = "audio" |
| 134 | + |
| 135 | + } else if type.conforms(to: .zip) { |
| 136 | + fileProperty.classFile = .compress |
| 137 | + fileProperty.iconName = .compress |
| 138 | + fileProperty.name = "archive" |
| 139 | + |
| 140 | + } else if type.conforms(to: .html) { |
| 141 | + fileProperty.classFile = .document |
| 142 | + fileProperty.iconName = .code |
| 143 | + fileProperty.name = "code" |
| 144 | + |
| 145 | + } else if type.conforms(to: .pdf) { |
| 146 | + fileProperty.classFile = .document |
| 147 | + fileProperty.iconName = .pdf |
| 148 | + fileProperty.name = "document" |
| 149 | + |
| 150 | + } else if type.conforms(to: .rtf) { |
| 151 | + fileProperty.classFile = .document |
| 152 | + fileProperty.iconName = .txt |
| 153 | + fileProperty.name = "document" |
| 154 | + |
| 155 | + } else if type.conforms(to: .text) { |
| 156 | + // Default to .txt if extension is empty |
| 157 | + if fileProperty.ext.isEmpty { |
| 158 | + fileProperty.ext = "txt" |
| 159 | + } |
| 160 | + fileProperty.classFile = .document |
| 161 | + fileProperty.iconName = .txt |
| 162 | + fileProperty.name = "text" |
| 163 | + |
| 164 | + } else if type.conforms(to: .content) { |
137 | 165 | fileProperty.classFile = .document |
138 | 166 | fileProperty.iconName = .document |
139 | 167 | fileProperty.name = "document" |
| 168 | + |
140 | 169 | } else { |
141 | 170 | fileProperty.classFile = .unknow |
142 | 171 | fileProperty.iconName = .unknow |
143 | 172 | fileProperty.name = "file" |
144 | 173 | } |
| 174 | + } else { |
| 175 | + // tipo UTI non valido |
| 176 | + fileProperty.classFile = .unknow |
| 177 | + fileProperty.iconName = .unknow |
| 178 | + fileProperty.name = "file" |
145 | 179 | } |
146 | 180 |
|
147 | 181 | return fileProperty |
|
0 commit comments