NCMediaCache.swift 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // NCMediaCache.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 18/10/23.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import LRUCache
  25. import NextcloudKit
  26. @objc class NCMediaCache: NSObject {
  27. @objc public static let shared: NCMediaCache = {
  28. let instance = NCMediaCache()
  29. return instance
  30. }()
  31. let limit: Int = 2000
  32. private typealias ThumbnailLRUCache = LRUCache<String, UIImage>
  33. private lazy var cache: ThumbnailLRUCache = {
  34. return ThumbnailLRUCache(countLimit: limit)
  35. }()
  36. func createCache(account: String) {
  37. let resultsMedia = NCManageDatabase.shared.getMediaOcIdEtag(account: account)
  38. guard !resultsMedia.isEmpty,
  39. let directory = CCUtility.getDirectoryProviderStorage() else { return }
  40. let ext = ".preview.ico"
  41. let manager = FileManager.default
  42. let resourceKeys = Set<URLResourceKey>([.nameKey, .pathKey, .fileSizeKey, .creationDateKey])
  43. struct FileInfo {
  44. var path: URL
  45. var ocId: String
  46. var date: Date
  47. }
  48. var files: [FileInfo] = []
  49. let startDate = Date()
  50. if let enumerator = manager.enumerator(at: URL(fileURLWithPath: directory), includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles]) {
  51. for case let fileURL as URL in enumerator where fileURL.lastPathComponent.hasSuffix(ext) {
  52. let fileName = fileURL.lastPathComponent
  53. let ocId = fileURL.deletingLastPathComponent().lastPathComponent
  54. guard let resourceValues = try? fileURL.resourceValues(forKeys: resourceKeys),
  55. let size = resourceValues.fileSize,
  56. size > 0,
  57. let date = resourceValues.creationDate,
  58. let etag = resultsMedia[ocId],
  59. fileName == etag + ext else { continue }
  60. files.append(FileInfo(path: fileURL, ocId: ocId, date: date))
  61. }
  62. }
  63. files.sort(by: { $0.date > $1.date })
  64. if let firstDate = files.first?.date, let lastDate = files.last?.date {
  65. print("First date: \(firstDate)")
  66. print("Last date: \(lastDate)")
  67. }
  68. cache.removeAllValues()
  69. var counter: Int = 0
  70. for file in files {
  71. counter += 1
  72. if counter > limit { break }
  73. autoreleasepool {
  74. if let image = UIImage(contentsOfFile: file.path.path) {
  75. cache.setValue(image, forKey: file.ocId)
  76. }
  77. }
  78. }
  79. let endDate = Date()
  80. let diffDate = endDate.timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate
  81. NextcloudKit.shared.nkCommonInstance.writeLog("--------- ThumbnailLRUCache image process ---------")
  82. NextcloudKit.shared.nkCommonInstance.writeLog("Counter process: \(cache.count)")
  83. NextcloudKit.shared.nkCommonInstance.writeLog("Time process: \(diffDate)")
  84. NextcloudKit.shared.nkCommonInstance.writeLog("--------- ThumbnailLRUCache image process ---------")
  85. }
  86. func getImage(ocId: String) -> UIImage? {
  87. return cache.value(forKey: ocId)
  88. }
  89. func setImage(ocId: String, image: UIImage) {
  90. cache.setValue(image, forKey: ocId)
  91. }
  92. @objc func clearCache() {
  93. cache.removeAllValues()
  94. }
  95. }