-
-
Notifications
You must be signed in to change notification settings - Fork 465
Expand file tree
/
Copy pathDocumentMetadataBuilder.swift
More file actions
69 lines (56 loc) · 1.85 KB
/
DocumentMetadataBuilder.swift
File metadata and controls
69 lines (56 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// LICENSE: see License.md in the package root
import Foundation
import UniformTypeIdentifiers
public class DocumentMetadataBuilder {
private let uri: URL
private let resourceValues: URLResourceValues?
private var bookmarkData: Data?
private var metadataError: Error?
private var bookmarkError: Error?
init(forUri uri: URL) {
self.uri = uri
self.resourceValues = nil
}
init(forUri uri: URL, resourceValues: URLResourceValues) {
self.uri = uri
self.resourceValues = resourceValues
}
convenience init(forUri uri: URL, error: Error) {
self.init(forUri: uri)
self.metadataError = error
}
func setBookmark(_ bookmark: Data) {
self.bookmarkData = bookmark
}
func setBookmarkError(_ bookmarkError: Error) {
self.bookmarkError = bookmarkError
}
func setMetadataReadingError(_ error: Error) {
self.metadataError = error
}
func build() -> [String: Any?] {
var dictionary: [String: Any?] = [:]
if resourceValues?.isDirectory != true {
let utTypeFromFile: UTType? = resourceValues?.contentType
let utType: UTType? = utTypeFromFile ?? UTType(filenameExtension: uri.pathExtension)
dictionary = [
"name": resourceValues?.name,
"size": resourceValues?.fileSize,
"type": utType?.preferredMIMEType,
"nativeType": utType?.identifier,
"error": metadataError?.localizedDescription,
"isVirtual": false,
"convertibleToMimeTypes": nil
]
}
dictionary["uri"] = uri.absoluteString
if let bookmark = bookmarkData {
dictionary["bookmarkStatus"] = "success"
dictionary["bookmark"] = bookmark.base64EncodedString()
} else if let bookmarkError = bookmarkError {
dictionary["bookmarkStatus"] = "error"
dictionary["bookmarkError"] = bookmarkError.localizedDescription
}
return dictionary
}
}