metatext/Caches/ImageCacheConfiguration.swift

45 lines
1.7 KiB
Swift
Raw Normal View History

2021-01-30 01:14:22 +00:00
// Copyright © 2021 Metabolist. All rights reserved.
import Foundation
2021-02-22 23:59:33 +00:00
import SDWebImage
2021-01-30 01:14:22 +00:00
import ServiceLayer
struct ImageCacheConfiguration {
private let environment: AppEnvironment
init(environment: AppEnvironment) {
self.environment = environment
}
}
extension ImageCacheConfiguration {
func configure() throws {
2021-02-22 23:59:33 +00:00
SDImageCache.defaultDiskCacheDirectory = Self.imageCacheDirectoryURL?.path
ImageDiskCache.service = try ImageSerializationService(environment: environment)
SDImageCacheConfig.default.diskCacheClass = ImageDiskCache.self
SDWebImageManager.shared.optionsProcessor = SDWebImageOptionsProcessor { _, options, context in
var mutableOptions = options
mutableOptions.insert(.retryFailed)
mutableOptions.insert(.continueInBackground)
return SDWebImageOptionsResult(options: options, context: context)
}
2021-02-22 23:59:33 +00:00
if let legacyImageCacheDirectoryURL = Self.legacyImageCacheDirectoryURL,
FileManager.default.fileExists(atPath: legacyImageCacheDirectoryURL.path) {
try? FileManager.default.removeItem(at: legacyImageCacheDirectoryURL)
}
2021-01-30 01:14:22 +00:00
}
}
private extension ImageCacheConfiguration {
2021-02-22 23:59:33 +00:00
static let cachesDirectoryURL = FileManager.default.containerURL(
2021-01-30 01:14:22 +00:00
forSecurityApplicationGroupIdentifier: AppEnvironment.appGroup)?
.appendingPathComponent("Library")
.appendingPathComponent("Caches")
2021-02-22 23:59:33 +00:00
static let imageCacheDirectoryURL = cachesDirectoryURL?.appendingPathComponent("com.metabolist.metatext.images")
static let legacyImageCacheDirectoryURL =
cachesDirectoryURL?.appendingPathComponent("com.onevcat.Kingfisher.ImageCache.Images")
2021-01-30 01:14:22 +00:00
}