Repository navigation
Expand file tree
/
Copy pathSharedCapture.swift
More file actions
342 lines (304 loc) · 12.3 KB
/
Copy pathSharedCapture.swift
File metadata and controls
342 lines (304 loc) · 12.3 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import Foundation
import CryptoKit
public struct SharedCapturePayload: Codable, Sendable, Equatable {
public let text: String?
public let title: String?
public let url: String?
public init(text: String?, title: String?, url: String?) {
self.text = text?.trimmingCharacters(in: .whitespacesAndNewlines).nilIfEmpty
self.title = title?.trimmingCharacters(in: .whitespacesAndNewlines).nilIfEmpty
self.url = url?.trimmingCharacters(in: .whitespacesAndNewlines).nilIfEmpty
}
}
public struct SharedDeepLink: Codable, Sendable, Equatable {
public struct QueryItem: Codable, Sendable, Equatable {
let name: String
let value: String?
}
var uuid: String?
let scheme: String?
let host: String?
let path: String?
let query: [QueryItem]
public init(_ url: URL) {
uuid = nil
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
scheme = components?.scheme
host = components?.host
path = components?.path
query = (components?.queryItems ?? []).map { QueryItem(name: $0.name, value: $0.value) }
}
}
extension SharedDeepLink {
var opensWidgetBlock: Bool {
scheme == "logseq" && host == "graph"
&& query.contains { $0.name == "widget-action" && $0.value == "open-block" }
&& query.contains { $0.name == "block-id" && $0.value?.isEmpty == false }
}
static func blockURL(graphName: String, blockID: String) -> String {
var components = URLComponents()
components.scheme = "logseq"
components.host = "graph"
components.path = "/" + graphName
components.queryItems = [URLQueryItem(name: "block-id", value: blockID)]
return components.url?.absoluteString ?? ""
}
}
public struct SharedCaptureAsset: Codable, Sendable, Equatable {
public let title: String
public let assetType: String
public let size: Int
public let checksum: String
public let stagedFileName: String
public init(
title: String,
assetType: String,
size: Int,
checksum: String,
stagedFileName: String
) {
self.title = title
self.assetType = assetType
self.size = size
self.checksum = checksum
self.stagedFileName = stagedFileName
}
}
public struct SharedCaptureItem: Codable, Sendable, Equatable, Identifiable {
public enum Kind: String, Codable, Sendable {
case text
case asset
case payload
case deepLink
}
public let id: String
public let kind: Kind
public let captureText: String?
public let captureAsset: SharedCaptureAsset?
public let capturePayload: SharedCapturePayload?
public let captureLink: SharedDeepLink?
public init(id: String, kind: Kind, captureText: String? = nil, captureAsset: SharedCaptureAsset? = nil,
capturePayload: SharedCapturePayload? = nil, captureLink: SharedDeepLink? = nil) {
self.id = id
self.kind = kind
self.captureText = captureText
self.captureAsset = captureAsset
self.capturePayload = capturePayload
self.captureLink = captureLink
}
public static func text(id: String, text: String) -> SharedCaptureItem {
SharedCaptureItem(id: id, kind: .text, captureText: text, captureAsset: nil)
}
public static func asset(id: String, asset: SharedCaptureAsset) -> SharedCaptureItem {
SharedCaptureItem(id: id, kind: .asset, captureText: nil, captureAsset: asset)
}
}
@MainActor public final class SharedCaptureInbox {
public static let shared = SharedCaptureInbox(directory: SharedCaptureStorage.queueDirectory)
private let defaults: UserDefaults
private let storageKey = "logseq.pendingSharedCaptureItems"
private let directory: URL?
public init(defaults: UserDefaults = .standard) {
self.defaults = defaults
self.directory = nil
}
public init(directory: URL) {
self.defaults = .standard
self.directory = directory
}
public func enqueue(_ item: SharedCaptureItem) {
guard !pendingItems().contains(where: { $0.id == item.id }) else { return }
if let directory {
try? FileManager.default.createDirectory(
at: directory,
withIntermediateDirectories: true
)
guard let data = try? JSONEncoder().encode(item) else { return }
try? data.write(to: itemURL(id: item.id, in: directory), options: .atomic)
return
}
var pending = pendingItems()
pending.append(item)
save(pending)
}
public func enqueueText(_ text: String, id: String = UUID().uuidString.lowercased()) {
guard let text = text.trimmingCharacters(in: .whitespacesAndNewlines).nilIfEmpty else {
return
}
enqueue(.text(id: id, text: text))
}
public func enqueue(payload: SharedCapturePayload, id: String = UUID().uuidString.lowercased()) {
guard payload.text != nil || payload.title != nil || payload.url != nil else { return }
enqueue(SharedCaptureItem(id: id, kind: .payload, capturePayload: payload))
}
public func pendingItems() -> [SharedCaptureItem] {
if let directory {
let urls = (try? FileManager.default.contentsOfDirectory(
at: directory,
includingPropertiesForKeys: [.contentModificationDateKey],
options: [.skipsHiddenFiles]
)) ?? []
return urls
.filter { $0.pathExtension == "json" }
.sorted { lhs, rhs in
let lhsDate = (try? lhs.resourceValues(forKeys: [.contentModificationDateKey]))?
.contentModificationDate ?? .distantPast
let rhsDate = (try? rhs.resourceValues(forKeys: [.contentModificationDateKey]))?
.contentModificationDate ?? .distantPast
if lhsDate == rhsDate { return lhs.lastPathComponent < rhs.lastPathComponent }
return lhsDate < rhsDate
}
.compactMap { url in
guard let data = try? Data(contentsOf: url) else { return nil }
return try? JSONDecoder().decode(SharedCaptureItem.self, from: data)
}
}
guard let value = defaults.string(forKey: storageKey),
let data = value.data(using: .utf8),
let pending = try? JSONDecoder().decode([SharedCaptureItem].self, from: data)
else { return [] }
return pending
}
public func acknowledge(id: String) {
if let directory {
try? FileManager.default.removeItem(at: itemURL(id: id, in: directory))
return
}
let remaining = pendingItems().filter { $0.id != id }
if remaining.isEmpty {
defaults.removeObject(forKey: storageKey)
} else {
save(remaining)
}
}
private func save(_ pending: [SharedCaptureItem]) {
guard let data = try? JSONEncoder().encode(pending),
let value = String(data: data, encoding: .utf8)
else { return }
defaults.set(value, forKey: storageKey)
}
private func itemURL(id: String, in directory: URL) -> URL {
let name = SHA256.hash(data: Data(id.utf8))
.map { String(format: "%02x", $0) }
.joined()
return directory.appendingPathComponent("\(name).json", isDirectory: false)
}
}
@MainActor public enum SharedCaptureProcessor {
public static func process(
inbox: SharedCaptureInbox,
handle: @MainActor (SharedCaptureItem) async -> Bool
) async {
while let item = inbox.pendingItems().first {
guard await handle(item) else { return }
inbox.acknowledge(id: item.id)
}
}
}
public enum SharedCaptureStorage {
public static let appGroupIdentifier = "group.com.logseq.logseq"
public static var sharedDirectory: URL {
let base = FileManager.default.containerURL(
forSecurityApplicationGroupIdentifier: appGroupIdentifier
) ?? FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
return base.appendingPathComponent("SharedCapture", isDirectory: true)
}
public static var queueDirectory: URL {
let base = FileManager.default.containerURL(
forSecurityApplicationGroupIdentifier: appGroupIdentifier
) ?? FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]
return base.appendingPathComponent("SharedCaptureQueue", isDirectory: true)
}
}
public struct ImportedSharedCaptureAsset: Sendable, Equatable {
public let title: String
public let assetType: String
public let size: Int
public let checksum: String
public let localPath: String
}
public enum SharedCaptureAssetError: Error, Equatable {
case missingStagedFile
case invalidSize
case invalidChecksum
}
public enum SharedCaptureAssetStager {
public static func stage(
sourceURL: URL,
contentType: String,
sharedDirectory: URL
) throws -> SharedCaptureAsset {
try stage(data: Data(contentsOf: sourceURL), title: sourceURL.lastPathComponent,
contentType: contentType, sharedDirectory: sharedDirectory)
}
static func stage(
data: Data,
title: String,
contentType: String,
sharedDirectory: URL
) throws -> SharedCaptureAsset {
let manager = FileManager.default
try manager.createDirectory(at: sharedDirectory, withIntermediateDirectories: true)
let prepared = LGImageCompression.prepare(data, title: title, contentType: contentType)
let title = prepared.title
let safeTitle = title.replacingOccurrences(of: "/", with: "-")
let safeName = "\(UUID().uuidString.lowercased())-\(safeTitle)"
let destination = sharedDirectory.appendingPathComponent(safeName, isDirectory: false)
let data = prepared.data
try data.write(to: destination, options: .atomic)
return SharedCaptureAsset(
title: title,
assetType: prepared.contentType,
size: data.count,
checksum: sha256(data),
stagedFileName: safeName
)
}
fileprivate static func sha256(_ data: Data) -> String {
SHA256.hash(data: data).map { String(format: "%02x", $0) }.joined()
}
}
public enum SharedCaptureAssetImporter {
public static func importAsset(
_ asset: SharedCaptureAsset,
sharedDirectory: URL,
documentsDirectory: URL
) throws -> ImportedSharedCaptureAsset {
let manager = FileManager.default
let assetsDirectory = documentsDirectory.appendingPathComponent("Assets", isDirectory: true)
try manager.createDirectory(at: assetsDirectory, withIntermediateDirectories: true)
let destinationName = asset.stagedFileName
let destination = assetsDirectory.appendingPathComponent(destinationName, isDirectory: false)
let source = sharedDirectory.appendingPathComponent(asset.stagedFileName, isDirectory: false)
if manager.fileExists(atPath: destination.path) {
try validate(destination, against: asset)
if manager.fileExists(atPath: source.path) {
try manager.removeItem(at: source)
}
} else {
guard manager.fileExists(atPath: source.path) else {
throw SharedCaptureAssetError.missingStagedFile
}
try validate(source, against: asset)
try manager.copyItem(at: source, to: destination)
try manager.removeItem(at: source)
}
return ImportedSharedCaptureAsset(
title: asset.title,
assetType: asset.assetType,
size: asset.size,
checksum: asset.checksum,
localPath: "Assets/\(destinationName)"
)
}
private static func validate(_ file: URL, against asset: SharedCaptureAsset) throws {
let data = try Data(contentsOf: file)
guard data.count == asset.size else { throw SharedCaptureAssetError.invalidSize }
guard SharedCaptureAssetStager.sha256(data) == asset.checksum else {
throw SharedCaptureAssetError.invalidChecksum
}
}
}
private extension String {
var nilIfEmpty: String? { isEmpty ? nil : self }
}