|
13 | 13 | // limitations under the License.
|
14 | 14 |
|
15 | 15 | import Foundation
|
| 16 | +private import UniformTypeIdentifiers |
| 17 | + |
16 | 18 | #if os(iOS) || os(tvOS) || os(visionOS)
|
17 | 19 | import MobileCoreServices
|
18 | 20 | #elseif os(macOS) || os(watchOS)
|
@@ -71,21 +73,28 @@ class StorageUtils {
|
71 | 73 | return string.addingPercentEncoding(withAllowedCharacters: allowedSet)!
|
72 | 74 | }
|
73 | 75 |
|
74 |
| - class func MIMETypeForExtension(_ fileExtension: String?) -> String { |
75 |
| - guard let fileExtension = fileExtension else { |
| 76 | + static func MIMETypeForExtension(_ fileExtension: String?) -> String { |
| 77 | + guard let fileExtension else { |
76 | 78 | return "application/octet-stream"
|
77 | 79 | }
|
78 |
| - |
79 |
| - if let type = UTTypeCreatePreferredIdentifierForTag( |
80 |
| - kUTTagClassFilenameExtension, |
81 |
| - fileExtension as NSString, |
82 |
| - nil |
83 |
| - )?.takeRetainedValue() { |
84 |
| - if let mimeType = UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType)? |
85 |
| - .takeRetainedValue() { |
86 |
| - return mimeType as String |
| 80 | + // TODO: Remove `else` when min. supported macOS is 11.0+. |
| 81 | + if #available(macOS 11.0, iOS 14.0, tvOS 14.0, watchOS 7.0, *) { |
| 82 | + guard let mimeType = UTType(filenameExtension: fileExtension)?.preferredMIMEType else { |
| 83 | + return "application/octet-stream" |
87 | 84 | }
|
| 85 | + return mimeType |
| 86 | + } else { |
| 87 | + if let type = UTTypeCreatePreferredIdentifierForTag( |
| 88 | + kUTTagClassFilenameExtension, |
| 89 | + fileExtension as NSString, |
| 90 | + nil |
| 91 | + )?.takeRetainedValue() { |
| 92 | + if let mimeType = UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType)? |
| 93 | + .takeRetainedValue() { |
| 94 | + return mimeType as String |
| 95 | + } |
| 96 | + } |
| 97 | + return "application/octet-stream" |
88 | 98 | }
|
89 |
| - return "application/octet-stream" |
90 | 99 | }
|
91 | 100 | }
|
0 commit comments