Skip to content

Commit 5edeeb1

Browse files
UniformTypeIdentifiers
Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
1 parent 2972bc7 commit 5edeeb1

1 file changed

Lines changed: 73 additions & 39 deletions

File tree

Sources/NextcloudKit/TypeIdentifiers/NKFilePropertyResolver.swift

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

55
import Foundation
6-
import MobileCoreServices
6+
import UniformTypeIdentifiers
77

88
public class NKFileProperty: NSObject {
99
public var classFile: NKTypeClassFile = .unknow
@@ -39,19 +39,36 @@ public enum NKTypeIconFile: String {
3939
case xls = "xls"
4040
}
4141

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+
4257
/// Class responsible for resolving NKFileProperty information from a given UTI.
4358
public final class NKFilePropertyResolver {
4459

4560
public init() {}
4661

47-
public func resolve(inUTI: CFString, account: String) -> NKFileProperty {
62+
public func resolve(inUTI: String, account: String) -> NKFileProperty {
4863
let fileProperty = NKFileProperty()
4964
let typeIdentifier = inUTI as String
5065
let capabilities = NKCapabilities.shared.getCapabilitiesBlocking(for: account)
66+
let utiString = inUTI as String
5167

5268
// 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
5572
}
5673

5774
// Collabora Nextcloud Text Office
@@ -99,49 +116,66 @@ public final class NKFilePropertyResolver {
99116
}
100117

101118
// 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) {
137165
fileProperty.classFile = .document
138166
fileProperty.iconName = .document
139167
fileProperty.name = "document"
168+
140169
} else {
141170
fileProperty.classFile = .unknow
142171
fileProperty.iconName = .unknow
143172
fileProperty.name = "file"
144173
}
174+
} else {
175+
// tipo UTI non valido
176+
fileProperty.classFile = .unknow
177+
fileProperty.iconName = .unknow
178+
fileProperty.name = "file"
145179
}
146180

147181
return fileProperty

0 commit comments

Comments
 (0)