NCImageSessionManager.swift 1.5 KB

123456789101112131415161718192021222324252627282930313233343536
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import Foundation
  6. @objcMembers public class NCImageSessionManager: NCBaseSessionManager {
  7. public static let shared = NCImageSessionManager()
  8. public var cache: URLCache
  9. init() {
  10. let configuration = AFImageDownloader.defaultURLSessionConfiguration()
  11. // Try to remove legacy ImageCache directory in app group
  12. if let legacyCacheURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupIdentifier)?.appendingPathComponent("ImageCache") {
  13. try? FileManager.default .removeItem(at: legacyCacheURL)
  14. }
  15. // In case of images we want to use the cache and store it on disk
  16. // As we use the memory cache from AFImageDownloader, we only want disk cache here
  17. let imageCacheURL = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.appendingPathComponent("ImageCache")
  18. self.cache = URLCache(memoryCapacity: 0, diskCapacity: 100 * 1024 * 1024, directory: imageCacheURL)
  19. configuration.urlCache = self.cache
  20. super.init(configuration: configuration, responseSerializer: AFImageResponseSerializer(), requestSerializer: AFHTTPRequestSerializer())
  21. var acceptableTypes = self.responseSerializer.acceptableContentTypes
  22. acceptableTypes?.insert("image/jpg")
  23. self.responseSerializer.acceptableContentTypes = acceptableTypes
  24. }
  25. }