NCMediaDownloadThumbnaill.swift 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. //
  2. // NCMediaDownloadThumbnaill.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/01/24.
  6. // Copyright © 2024 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 NextcloudKit
  25. import Queuer
  26. class NCMediaDownloadThumbnaill: ConcurrentOperation {
  27. var metadata: tableMetadata
  28. var media: NCMedia
  29. var fileNamePreviewLocalPath: String
  30. var fileNameIconLocalPath: String
  31. let utilityFileSystem = NCUtilityFileSystem()
  32. init(metadata: tableMetadata, media: NCMedia) {
  33. self.metadata = tableMetadata.init(value: metadata)
  34. self.media = media
  35. self.fileNamePreviewLocalPath = utilityFileSystem.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)
  36. self.fileNameIconLocalPath = utilityFileSystem.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)
  37. }
  38. override func start() {
  39. guard !isCancelled else { return self.finish() }
  40. var etagResource: String?
  41. let sizePreview = NCUtility().getSizePreview(width: metadata.width, height: metadata.height)
  42. if FileManager.default.fileExists(atPath: fileNameIconLocalPath) && FileManager.default.fileExists(atPath: fileNamePreviewLocalPath) {
  43. etagResource = metadata.etagResource
  44. }
  45. NextcloudKit.shared.downloadPreview(fileId: metadata.fileId,
  46. fileNamePreviewLocalPath: fileNamePreviewLocalPath,
  47. fileNameIconLocalPath: fileNameIconLocalPath,
  48. widthPreview: Int(sizePreview.width),
  49. heightPreview: Int(sizePreview.height),
  50. sizeIcon: NCGlobal.shared.sizeIcon,
  51. etag: etagResource,
  52. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { _, imagePreview, _, _, etag, error in
  53. if error == .success, let imagePreview {
  54. NCManageDatabase.shared.setMetadataEtagResource(ocId: self.metadata.ocId, etagResource: etag)
  55. DispatchQueue.main.async {
  56. if let visibleCells = self.media.collectionView?.indexPathsForVisibleItems.sorted(by: { $0.row < $1.row }).compactMap({ self.media.collectionView?.cellForItem(at: $0) }) {
  57. for case let cell as NCGridMediaCell in visibleCells {
  58. if cell.ocId == self.metadata.ocId, let imageItem = cell.imageItem {
  59. UIView.transition(with: imageItem,
  60. duration: 0.75,
  61. options: .transitionCrossDissolve,
  62. animations: { imageItem.image = imagePreview },
  63. completion: nil)
  64. break
  65. }
  66. }
  67. }
  68. }
  69. NCImageCache.shared.setMediaSize(ocId: self.metadata.ocId, etag: self.metadata.etag, size: imagePreview.size)
  70. let fileSizePreview = self.utilityFileSystem.getFileSize(filePath: self.fileNamePreviewLocalPath)
  71. if NCImageCache.shared.hasMediaImageEnoughSize(fileSizePreview), NCImageCache.shared.hasMediaImageEnoughSpace() {
  72. NCImageCache.shared.setMediaImage(ocId: self.metadata.ocId, etag: self.metadata.etag, image: imagePreview, date: self.metadata.date as Date)
  73. }
  74. }
  75. self.finish()
  76. }
  77. }
  78. override func finish(success: Bool = true) {
  79. super.finish(success: success)
  80. if (metadata.width == 0 && metadata.height == 0) || (NCNetworking.shared.downloadThumbnailQueue.operationCount == 0) {
  81. self.media.collectionViewReloadData()
  82. }
  83. }
  84. }