Repository navigation
Expand file tree
/
Copy pathLGInlineImage.swift
More file actions
191 lines (173 loc) · 7.88 KB
/
Copy pathLGInlineImage.swift
File metadata and controls
191 lines (173 loc) · 7.88 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
import ImageIO
import LogseqModel
import SwiftUI
typealias LGImageDimensions = LogseqImageDimensions
extension LogseqImageDimensions {
var previewSize: CGSize? {
guard width > 0, height > 0 else { return nil }
let width = CGFloat(width)
let height = CGFloat(height)
let scale = min(326 / width, 280 / height, 1)
return CGSize(width: ceil(width * scale), height: ceil(height * scale))
}
static func read(at url: URL) -> Self? {
guard let source = CGImageSourceCreateWithURL(url as CFURL, [kCGImageSourceShouldCache: false] as CFDictionary),
let properties = CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [CFString: Any],
let width = properties[kCGImagePropertyPixelWidth] as? Int,
let height = properties[kCGImagePropertyPixelHeight] as? Int,
width > 0, height > 0 else { return nil }
let orientation = properties[kCGImagePropertyOrientation] as? Int ?? 1
return (5...8).contains(orientation) ? Self(width: height, height: width) : Self(width: width, height: height)
}
}
#if os(iOS)
@MainActor enum LGGraphAssetLoader {
private struct Response: Decodable {
let ok: Bool
let result: Result?
let error: LogseqCoreError?
struct Result: Decodable {
let graphId: String
let localPath: String?
let request: LogseqPendingSyncRequest?
}
}
static func load(uuid: String, assetType: String, expectedGraphId: String? = nil, feedbackTitle: String? = nil,
callCore: (LogseqRPCRequest) async -> String = { await LogseqCore.callAsync($0) },
downloadFile: (URLRequest) async throws -> (URL, URLResponse) = {
try await URLSession.shared.download(for: $0)
}) async throws -> URL {
var metadata = ["uuid": uuid, "assetType": assetType]
func call(_ action: String, _ fields: [String: String]) async throws -> Response.Result {
let payload = String(decoding: try JSONEncoder().encode(fields), as: UTF8.self)
let encoded = await callCore(LogseqRPCRequest(method: "dispatch",
params: LogseqRPCParams(action: action, payload: payload)))
let response = try JSONDecoder().decode(Response.self, from: Data(encoded.utf8))
guard response.ok, let result = response.result else {
throw NSError(domain: "LogseqAsset", code: 1, userInfo: [
NSLocalizedDescriptionKey: response.error?.message ?? "Asset download failed"
])
}
return result
}
let prepared = try await call("prepareAssetDownload", metadata)
if let expectedGraphId, prepared.graphId != expectedGraphId { throw CancellationError() }
if let path = prepared.localPath { return URL(fileURLWithPath: path) }
if let feedbackTitle {
return try await LGFileDownloadFeedback.shared.download(title: feedbackTitle) {
try await download()
}
}
return try await download()
func download() async throws -> URL {
guard let pending = prepared.request, let url = URL(string: pending.url) else {
throw URLError(.badURL)
}
var request = URLRequest(url: url)
request.httpMethod = pending.method
request.setValue("Bearer " + pending.token, forHTTPHeaderField: "Authorization")
let (download, response) = try await downloadFile(request)
guard let http = response as? HTTPURLResponse, http.statusCode == 200 else {
throw URLError(.badServerResponse)
}
let temporary = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
try FileManager.default.moveItem(at: download, to: temporary)
defer { try? FileManager.default.removeItem(at: temporary) }
try Task.checkCancellation()
metadata["graphId"] = prepared.graphId
metadata["path"] = temporary.path
let cached = try await call("cacheDownloadedAsset", metadata)
guard let path = cached.localPath else { throw URLError(.cannotCreateFile) }
return URL(fileURLWithPath: path)
}
}
}
private final class LGInlineImageThumbnailBox {
let image: CGImage
init(_ image: CGImage) {
self.image = image
}
}
private final class LGInlineImageThumbnailCache: @unchecked Sendable {
static let shared = LGInlineImageThumbnailCache()
private let cache = NSCache<NSURL, LGInlineImageThumbnailBox>()
private let sizes = NSCache<NSURL, NSValue>()
private init() {
cache.countLimit = 80
sizes.countLimit = 256
}
func image(for url: URL) -> CGImage? {
cache.object(forKey: url as NSURL)?.image
}
func store(_ image: CGImage, for url: URL) {
cache.setObject(LGInlineImageThumbnailBox(image), forKey: url as NSURL)
}
func previewSize(for url: URL) -> CGSize? {
if let cached = sizes.object(forKey: url as NSURL) { return cached.cgSizeValue }
guard let size = LGImageDimensions.read(at: url)?.previewSize else { return nil }
sizes.setObject(NSValue(cgSize: size), forKey: url as NSURL)
return size
}
}
struct LGInlineImage: View {
let url: URL
let dimensions: LGImageDimensions?
@State private var thumbnail: CGImage?
init(url: URL, dimensions: LGImageDimensions? = nil) {
self.url = url
self.dimensions = dimensions
_thumbnail = State(
initialValue: LGInlineImageThumbnailCache.shared.image(for: url)
)
}
var body: some View {
let size = dimensions?.previewSize ?? LGInlineImageThumbnailCache.shared.previewSize(for: url)
Group {
if let thumbnail {
let displaySize = size ?? LGImageDimensions(width: thumbnail.width, height: thumbnail.height).previewSize
Image(decorative: thumbnail, scale: 1)
.resizable()
.scaledToFit()
.frame(width: displaySize?.width, height: displaySize?.height)
.clipShape(.rect(cornerRadius: 8))
.frame(maxWidth: .infinity, alignment: .leading)
} else {
Self.placeholder(size: size) { ProgressView() }
}
}
.accessibilityIdentifier("asset.preview.image")
.task(id: url) {
if let cached = LGInlineImageThumbnailCache.shared.image(for: url) {
thumbnail = cached
return
}
let imageURL = url
let image = await Task.detached(priority: .utility) {
guard let source = CGImageSourceCreateWithURL(imageURL as CFURL, [
kCGImageSourceShouldCache: false,
] as CFDictionary) else { return nil as CGImage? }
return CGImageSourceCreateThumbnailAtIndex(source, 0, [
kCGImageSourceCreateThumbnailFromImageAlways: true,
kCGImageSourceCreateThumbnailWithTransform: true,
kCGImageSourceThumbnailMaxPixelSize: 1200,
] as CFDictionary)
}.value
guard !Task.isCancelled else { return }
if let image {
LGInlineImageThumbnailCache.shared.store(image, for: imageURL)
}
thumbnail = image
}
}
static func placeholder<Content: View>(size: CGSize?, @ViewBuilder content: () -> Content) -> some View {
content()
.frame(width: size?.width, height: size?.height ?? 120)
.background {
if size != nil {
RoundedRectangle(cornerRadius: 8).fill(.secondary.opacity(0.08))
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
}
#endif