NCMediaCache.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. private typealias ThumbnailLRUCache = LRUCache<String, UIImage>
  32. private let cache: ThumbnailLRUCache = ThumbnailLRUCache(countLimit: 2000)
  33. func createCache(account: String) {
  34. let resultsMedia = NCManageDatabase.shared.getMediaOcIdEtag(account: account)
  35. guard !resultsMedia.isEmpty,
  36. let directory = CCUtility.getDirectoryProviderStorage() else { return }
  37. let ext = ".preview.ico"
  38. let manager = FileManager.default
  39. let resourceKeys = Set<URLResourceKey>([.nameKey, .pathKey, .fileSizeKey, .creationDateKey])
  40. struct FileInfo {
  41. var path: URL
  42. var ocId: String
  43. var date: Date
  44. }
  45. var files: [FileInfo] = []
  46. let startDate = Date()
  47. // Get files only image / video
  48. if let enumerator = manager.enumerator(at: URL(fileURLWithPath: directory), includingPropertiesForKeys: [.isRegularFileKey], options: [.skipsHiddenFiles]) {
  49. for case let fileURL as URL in enumerator where fileURL.lastPathComponent.hasSuffix(ext) {
  50. let fileName = fileURL.lastPathComponent
  51. let ocId = fileURL.deletingLastPathComponent().lastPathComponent
  52. guard let resourceValues = try? fileURL.resourceValues(forKeys: resourceKeys),
  53. let size = resourceValues.fileSize,
  54. size > 0,
  55. let date = resourceValues.creationDate,
  56. let etag = resultsMedia[ocId],
  57. fileName == etag + ext else { continue }
  58. files.append(FileInfo(path: fileURL, ocId: ocId, date: date))
  59. }
  60. }
  61. // Sort for most recent
  62. files.sort(by: { $0.date > $1.date })
  63. if let firstDate = files.first?.date, let lastDate = files.last?.date {
  64. print("First date: \(firstDate)")
  65. print("Last date: \(lastDate)")
  66. }
  67. // Insert in cache
  68. cache.removeAllValues()
  69. for file in files {
  70. autoreleasepool {
  71. if let image = UIImage(contentsOfFile: file.path.path) {
  72. cache.setValue(image, forKey: file.ocId)
  73. }
  74. }
  75. }
  76. let endDate = Date()
  77. let diffDate = endDate.timeIntervalSinceReferenceDate - startDate.timeIntervalSinceReferenceDate
  78. NextcloudKit.shared.nkCommonInstance.writeLog("--------- ThumbnailLRUCache image process ---------")
  79. NextcloudKit.shared.nkCommonInstance.writeLog("Counter process: \(cache.count)")
  80. NextcloudKit.shared.nkCommonInstance.writeLog("Time process: \(diffDate)")
  81. NextcloudKit.shared.nkCommonInstance.writeLog("--------- ThumbnailLRUCache image process ---------")
  82. }
  83. func getImage(ocId: String) -> UIImage? {
  84. return cache.value(forKey: ocId)
  85. }
  86. func setImage(ocId: String, image: UIImage) {
  87. cache.setValue(image, forKey: ocId)
  88. }
  89. @objc func clearCache() {
  90. cache.removeAllValues()
  91. }
  92. }