-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathMediaImageService.swift
452 lines (399 loc) · 18.9 KB
/
MediaImageService.swift
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import UIKit
import CoreData
/// A service for retrieval and caching of thumbnails for ``Media`` objects.
final class MediaImageService {
static let shared = MediaImageService()
private let cache: MemoryCache
private let coreDataStack: CoreDataStackSwift
private let mediaFileManager: MediaFileManager
private let downloader: ImageDownloader
init(cache: MemoryCache = .shared,
coreDataStack: CoreDataStackSwift = ContextManager.shared,
mediaFileManager: MediaFileManager = MediaFileManager(directory: .cache),
downloader: ImageDownloader = .shared) {
self.cache = cache
self.coreDataStack = coreDataStack
self.mediaFileManager = mediaFileManager
self.downloader = downloader
}
static func migrateCacheIfNeeded() {
let didMigrateKey = "MediaImageService-didMigrateCacheKey"
guard !UserDefaults.standard.bool(forKey: didMigrateKey) else {
return
}
UserDefaults.standard.set(true, forKey: didMigrateKey)
DispatchQueue.global(qos: .utility).async {
MediaFileManager.clearAllMediaCacheFiles(onCompletion: nil, onError: nil)
}
}
enum Error: Swift.Error {
case unsupportedMediaType(_ type: MediaType)
case unsupportedThumbnailSize(_ size: ImageSize)
case missingImageURL
}
/// Returns an image for the given media asset.
///
/// **Performance Characteristics**
///
/// The returned images are decompressed (or bitmapped) and are ready to be
/// displayed even during scrolling.
///
/// The thumbnails (``ImageSize/small`` or ``ImageSize/medium``) don't take
/// a lot of space or memory and are used often. The app often displays
/// multiple thumbnails on the screen at the same time. This is why the
/// thumbnails are stored in both disk and memory cache. The disk cache
/// has no size or time limit.
///
/// The original images (``ImageSize/original``) are rarely displayed by the
/// app and you usually preview only one image at a time. The original images
/// are _not_ stored in the memory cache as they may take up too much space.
/// They are stored in a custom `URLCache` instance that automatically evicts
/// images if it reaches the size limit.
@MainActor
func image(for media: Media, size: ImageSize) async throws -> UIImage {
let media = try await getSafeMedia(for: media)
switch size {
case .small, .large:
return try await thumbnail(for: media, size: size)
case .original:
return try await originalImage(for: media)
}
}
/// Returns a thread-safe media object and materializes a stub if needed.
@MainActor
private func getSafeMedia(for media: Media) async throws -> SafeMedia {
guard media.remoteStatus != .stub else {
guard let mediaID = media.mediaID else {
throw URLError(.unknown) // This should never happen
}
let blogID = TaggedManagedObjectID(media.blog)
return try await fetchStubMedia(for: mediaID, blogID: blogID)
}
return SafeMedia(media)
}
// MARK: - Media (Original)
/// Returns a full-size image for the given media asset.
///
/// The app rarely loads full-size images, and they make take a significant
/// amount of space and memory, so they are cached only in `URLCache`.
private func originalImage(for media: SafeMedia) async throws -> UIImage {
guard media.mediaType == .image else {
assertionFailure("Unsupported media type: \(media.mediaType)")
throw Error.unsupportedMediaType(media.mediaType)
}
if let localURL = media.absoluteLocalURL,
let image = try? await ImageDecoder.makeImage(from: localURL) {
return image
}
if let info = await getFullsizeImageInfo(for: media) {
let data = try await data(for: info, isCached: true)
return try await ImageDecoder.makeImage(from: data)
}
// The media has no local or remote URL – should never happen
throw Error.missingImageURL
}
private func getFullsizeImageInfo(for media: SafeMedia) async -> RemoteImageInfo? {
guard let remoteURL = media.remoteURL.flatMap(URL.init) else {
return nil
}
return try? await coreDataStack.performQuery { context in
let blog = try context.existingObject(with: media.blogID)
return RemoteImageInfo(imageURL: remoteURL, host: MediaHost(with: blog))
}
}
// MARK: - Media (Thumbnails)
private func thumbnail(for media: SafeMedia, size: ImageSize) async throws -> UIImage {
guard media.mediaType == .image || media.mediaType == .video else {
assertionFailure("Unsupported thubmnail media type: \(media.mediaType)")
throw Error.unsupportedMediaType(media.mediaType)
}
guard size != .original else {
assertionFailure("Unsupported thumbnail size: \(size)")
throw Error.unsupportedThumbnailSize(size)
}
if let image = cache.getImage(forKey: makeCacheKey(for: media.mediaID, size: size)) {
return image
}
let image = try await actuallyLoadThumbnail(for: media, size: size)
cache.setImage(image, forKey: makeCacheKey(for: media.mediaID, size: size))
return image
}
private func actuallyLoadThumbnail(for media: SafeMedia, size: ImageSize) async throws -> UIImage {
if let image = await cachedThumbnail(for: media.mediaID, size: size) {
return image
}
if let image = await localThumbnail(for: media, size: size) {
return image
}
return try await remoteThumbnail(for: media, size: size)
}
// MARK: - Thumbnails (Memory Cache)
/// Returns cached image for the given thumbnail.
nonisolated func getCachedThumbnail(for mediaID: TaggedManagedObjectID<Media>, size: ImageSize = .small) -> UIImage? {
cache.getImage(forKey: makeCacheKey(for: mediaID, size: size))
}
// MARK: - Thumbnails (Disk Cache)
/// Returns a local thumbnail for the given media object (if available).
private func cachedThumbnail(for mediaID: TaggedManagedObjectID<Media>, size: ImageSize) async -> UIImage? {
guard let fileURL = getCachedThumbnailURL(for: mediaID, size: size) else { return nil }
return try? await ImageDecoder.makeImage(from: fileURL)
}
private func getCachedThumbnailURL(for mediaID: TaggedManagedObjectID<Media>, size: ImageSize) -> URL? {
let mediaID = mediaID.objectID.uriRepresentation().lastPathComponent
return try? mediaFileManager.makeLocalMediaURL(
withFilename: "\(mediaID)-\(size.rawValue)-thumbnail",
fileExtension: nil, // We don't know ahead of time
incremented: false
)
}
// MARK: - Local Thumbnail
/// Generates a thumbnail from a local asset and saves it in cache.
private func localThumbnail(for media: SafeMedia, size: ImageSize) async -> UIImage? {
guard let url = await generateLocalThumbnail(for: media, size: size) else {
return nil
}
return try? await ImageDecoder.makeImage(from: url)
}
private func generateLocalThumbnail(for media: SafeMedia, size: ImageSize) async -> URL? {
guard let sourceURL = media.absoluteLocalURL else {
return nil
}
let exporter = await makeThumbnailExporter(for: media, size: size)
if sourceURL.isGif {
exporter.options.thumbnailImageType = UTType.gif.identifier
}
guard exporter.supportsThumbnailExport(forFile: sourceURL),
let (_, export) = try? await exporter.exportThumbnail(forFileURL: sourceURL),
let thumbnailURL = getCachedThumbnailURL(for: media.mediaID, size: size)
else {
return nil
}
try? FileManager.default.moveItem(at: export.url, to: thumbnailURL)
return thumbnailURL
}
@MainActor
private func makeThumbnailExporter(for media: SafeMedia, size: ImageSize) -> MediaThumbnailExporter {
let exporter = MediaThumbnailExporter()
exporter.mediaDirectoryType = .cache
exporter.options.preferredSize = MediaImageService.getThumbnailSize(for: media, size: size)
exporter.options.scale = 1 // In pixels
return exporter
}
/// - warning: This method was added only for backward-compatability with
/// the editor that relies on using URLs for displaying the preview thumbnail
/// while the image is loaded.
public func getThumbnailURL(for media: Media, _ completion: @escaping (URL?) -> Void) {
let media = SafeMedia(media)
Task {
let url = await generateLocalThumbnail(for: media, size: .large)
completion(url)
}
}
// MARK: - Remote Thumbnail
/// Downloads a remote thumbnail and saves it in cache.
private func remoteThumbnail(for media: SafeMedia, size: ImageSize) async throws -> UIImage {
guard let info = await getRemoteThumbnailInfo(for: media, size: size) else {
// Self-hosted WordPress sites don't have `remoteThumbnailURL`, so
// the app generates the thumbnail by itself.
if media.mediaType == .video {
return try await generateThumbnailForVideo(for: media, size: size)
}
throw URLError(.badURL)
}
// The service has a custom disk cache for thumbnails, so it's important to
// disable the native url cache which is by default set to `URLCache.shared`
let data = try await data(for: info, isCached: false)
let image = try await ImageDecoder.makeImage(from: data)
if let fileURL = getCachedThumbnailURL(for: media.mediaID, size: size) {
try? data.write(to: fileURL)
}
return image
}
// There are two reasons why these operations are performed in the background:
// performance and making sure the subsystem is thread-safe and can be used
// from the background.
private func getRemoteThumbnailInfo(for media: SafeMedia, size: ImageSize) async -> RemoteImageInfo? {
let targetSize = await MediaImageService.getThumbnailSize(for: media, size: size)
return try? await coreDataStack.performQuery { context in
let blog = try context.existingObject(with: media.blogID)
guard let imageURL = media.getRemoteThumbnailURL(targetSize: targetSize, blog: blog) else { return nil }
return RemoteImageInfo(imageURL: imageURL, host: MediaHost(with: blog))
}
}
// MARK: - Networking
private func data(for info: RemoteImageInfo, isCached: Bool) async throws -> Data {
let options = ImageRequestOptions(isDiskCacheEnabled: isCached)
return try await downloader.data(from: info.imageURL, host: info.host, options: options)
}
private struct RemoteImageInfo {
let imageURL: URL
let host: MediaHost
}
// MARK: - Thubmnail for Video
private func generateThumbnailForVideo(for media: SafeMedia, size: ImageSize) async throws -> UIImage {
guard let videoURL = media.remoteURL.flatMap(URL.init) else {
throw URLError(.badURL)
}
let exporter = await makeThumbnailExporter(for: media, size: size)
let (_, export) = try await exporter.exportThumbnail(forVideoURL: videoURL)
let image = try await ImageDecoder.makeImage(from: export.url)
// The order is important to ensure `export.url` exists when making an image
if let fileURL = getCachedThumbnailURL(for: media.mediaID, size: size) {
try? FileManager.default.moveItem(at: export.url, to: fileURL)
}
return image
}
// MARK: - Stubs
private func fetchStubMedia(for mediaID: NSNumber, blogID: TaggedManagedObjectID<Blog>) async throws -> SafeMedia {
let mediaRepository = MediaRepository(coreDataStack: coreDataStack)
let objectID = try await mediaRepository.getMedia(withID: mediaID, in: blogID)
return try await coreDataStack.performQuery { context in
let media = try context.existingObject(with: objectID)
return SafeMedia(media)
}
}
}
// MARK: - MediaImageService (ThumbnailSize)
extension MediaImageService {
enum ImageSize: String {
/// The small thumbnail that can be used in collection view cells and
/// similar situations.
case small
/// A large thumbnail thumbnail that can typically be used to fit
/// the entire screen on iPhone or a large portion of the sreen on iPad.
case large
/// Loads an original image.
case original
}
@MainActor
static func getThumbnailSize(for media: Media, size: ImageSize) -> CGSize {
getThumbnailSize(for: SafeMedia(media), size: size)
}
@MainActor
fileprivate static func getThumbnailSize(for media: SafeMedia, size: ImageSize) -> CGSize {
let mediaSize = media.size ?? CGSize(width: 1024, height: 1024) // rhs should never happen
return MediaImageService.getThumbnailSize(for: mediaSize, size: size)
}
/// Returns an optimal target size in pixels for a thumbnail of the given
/// size for the given media asset.
@MainActor
static func getThumbnailSize(for mediaSize: CGSize, size: ImageSize) -> CGSize {
let targetSize = MediaImageService.getPreferredThumbnailSize(for: size)
return MediaImageService.targetSize(forMediaSize: mediaSize, targetSize: targetSize)
}
/// Returns a preferred thumbnail size (in pixels) optimized for the device.
///
/// - important: It makes sure the app uses the same thumbnails across
/// different screens and presentation modes to avoid fetching and caching
/// more than one version of the same image.
@MainActor
private static func getPreferredThumbnailSize(for thumbnail: ImageSize) -> CGSize {
let minScreenSide = min(UIScreen.main.bounds.width, UIScreen.main.bounds.height)
switch thumbnail {
case .small:
/// The size is calculated to fill a collection view cell, assuming the app
/// displays a 4 or 5 cells in one row. The cell size can vary depending
/// on whether the device is in landscape or portrait mode, but the thumbnail size is
/// guaranteed to always be the same across app launches and optimized for
/// a portraint (dominant) mode.
let itemPerRow = UIDevice.current.userInterfaceIdiom == .pad ? 5 : 4
let availableWidth = minScreenSide - SiteMediaCollectionViewController.spacing * CGFloat(itemPerRow - 1)
let targetSide = (availableWidth / CGFloat(itemPerRow)).rounded(.down)
let targetSize = CGSize(width: targetSide, height: targetSide)
return targetSize.scaled(by: UIScreen.main.scale)
case .large:
let side = min(1024, minScreenSide * UIScreen.main.scale)
return CGSize(width: side, height: side)
case .original:
assertionFailure("Unsupported thumbnail size")
return CGSize(width: 2048, height: 2048)
}
}
/// Image CDN (Photon) and `MediaImageExporter` both don't support "aspect-fill"
/// resizing mode, so the service performs the necessary calculations by itself.
///
/// Example: if media size is 2000x3000 px and targetSize is 200x200 px, the
/// returned value will be 200x300 px. For more examples, see `MediaImageServiceTests`.
static func targetSize(forMediaSize mediaSize: CGSize, targetSize originalTargetSize: CGSize) -> CGSize {
guard mediaSize.width > 0 && mediaSize.height > 0 else {
return originalTargetSize
}
// Scale image to fill the target size but avoid upscaling
let scale = min(1, max(
originalTargetSize.width / mediaSize.width,
originalTargetSize.height / mediaSize.height
))
let targetSize = mediaSize.scaled(by: scale).rounded()
// Sanitize the size to make sure ultra-wide panoramas are still resized
// to fit the target size, but increase it a bit for an acceptable size.
let threshold: CGFloat = 4
if targetSize.width > originalTargetSize.width * threshold || targetSize.height > originalTargetSize.height * threshold {
return CGSize(
width: min(targetSize.width, originalTargetSize.width * threshold),
height: min(targetSize.height, originalTargetSize.height * threshold)
)
}
return targetSize
}
}
// MARK: - SafeMedia
/// A thread-safe media wrapper for use by `MediaImageService`.
private final class SafeMedia {
let mediaID: TaggedManagedObjectID<Media>
let blogID: TaggedManagedObjectID<Blog>
let mediaType: MediaType
let absoluteLocalURL: URL?
let remoteThumbnailURL: String?
let remoteURL: String?
let size: CGSize?
init(_ media: Media) {
self.mediaID = TaggedManagedObjectID(media)
self.blogID = TaggedManagedObjectID(media.blog)
self.mediaType = media.mediaType
self.absoluteLocalURL = media.absoluteLocalURL
self.remoteURL = media.remoteURL
self.remoteThumbnailURL = media.remoteThumbnailURL
if let width = media.width?.floatValue, let height = media.height?.floatValue {
self.size = CGSize(width: CGFloat(width), height: CGFloat(height))
} else {
self.size = nil
}
}
/// Returns the thumbnail remote URL with a given target size. It uses
/// Image CDN (formerly Photon) if available.
///
/// - parameter targetSize: Target size in pixels.
func getRemoteThumbnailURL(targetSize: CGSize, blog: Blog) -> URL? {
switch mediaType {
case .image:
guard let remoteURL = remoteURL.flatMap(URL.init) else {
return nil
}
// Download a non-retina version for GIFs: makes a massive difference
// in terms of size. Example: 2.4 MB -> 350 KB.
let scale = UIScreen.main.scale
var targetSize = targetSize
if remoteURL.isGif {
targetSize = targetSize
.scaled(by: 1.0 / scale)
.scaled(by: min(2, scale))
}
if !blog.isEligibleForPhoton {
return WPImageURLHelper.imageURLWithSize(targetSize, forImageURL: remoteURL)
} else {
let targetSize = targetSize.scaled(by: 1.0 / UIScreen.main.scale)
return PhotonImageURLHelper.photonURL(with: targetSize, forImageURL: remoteURL)
}
default:
return remoteThumbnailURL.flatMap(URL.init)
}
}
}
private extension Blog {
var isEligibleForPhoton: Bool {
!(isPrivateAtWPCom() || (!isHostedAtWPcom && isBasicAuthCredentialStored()))
}
}
private func makeCacheKey(for mediaID: TaggedManagedObjectID<Media>, size: MediaImageService.ImageSize) -> String {
"\(mediaID.objectID)-\(size.rawValue)"
}