NCCollectionViewDownloadThumbnail.swift 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // NCCollectionViewDownloadThumbnail.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 14/03/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 Foundation
  24. import UIKit
  25. import Queuer
  26. import NextcloudKit
  27. import Realm
  28. class NCCollectionViewDownloadThumbnail: ConcurrentOperation {
  29. var metadata: tableMetadata
  30. var cell: NCCellProtocol?
  31. var collectionView: UICollectionView?
  32. var fileNamePreviewLocalPath: String
  33. var fileNameIconLocalPath: String
  34. let utilityFileSystem = NCUtilityFileSystem()
  35. init(metadata: tableMetadata, cell: NCCellProtocol?, collectionView: UICollectionView?) {
  36. self.metadata = tableMetadata.init(value: metadata)
  37. self.cell = cell
  38. self.collectionView = collectionView
  39. self.fileNamePreviewLocalPath = utilityFileSystem.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)
  40. self.fileNameIconLocalPath = utilityFileSystem.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)
  41. }
  42. override func start() {
  43. guard !isCancelled else { return self.finish() }
  44. var etagResource: String?
  45. let sizePreview = NCUtility().getSizePreview(width: metadata.width, height: metadata.height)
  46. if FileManager.default.fileExists(atPath: fileNameIconLocalPath) && FileManager.default.fileExists(atPath: fileNamePreviewLocalPath) {
  47. etagResource = metadata.etagResource
  48. }
  49. NextcloudKit.shared.downloadPreview(fileId: metadata.fileId,
  50. fileNamePreviewLocalPath: fileNamePreviewLocalPath,
  51. fileNameIconLocalPath: fileNameIconLocalPath,
  52. widthPreview: Int(sizePreview.width),
  53. heightPreview: Int(sizePreview.height),
  54. sizeIcon: NCGlobal.shared.sizeIcon,
  55. etag: etagResource,
  56. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { _, _, imageIcon, _, etag, error in
  57. if error == .success, let imageIcon {
  58. NCManageDatabase.shared.setMetadataEtagResource(ocId: self.metadata.ocId, etagResource: etag)
  59. DispatchQueue.main.async {
  60. if self.metadata.ocId == self.cell?.fileObjectId, let filePreviewImageView = self.cell?.filePreviewImageView {
  61. self.cell?.filePreviewImageView?.contentMode = .scaleAspectFill
  62. if self.metadata.hasPreviewBorder {
  63. self.cell?.filePreviewImageView?.layer.borderWidth = 0.2
  64. self.cell?.filePreviewImageView?.layer.borderColor = UIColor.systemGray3.cgColor
  65. }
  66. UIView.transition(with: filePreviewImageView,
  67. duration: 0.75,
  68. options: .transitionCrossDissolve,
  69. animations: { filePreviewImageView.image = imageIcon },
  70. completion: nil)
  71. } else {
  72. self.collectionView?.reloadData()
  73. }
  74. }
  75. let fileSizeIcon = self.utilityFileSystem.getFileSize(filePath: self.fileNameIconLocalPath)
  76. if NCImageCache.shared.hasIconImageEnoughSize(fileSizeIcon) {
  77. NCImageCache.shared.setIconImage(ocId: self.metadata.ocId, etag: self.metadata.etag, image: imageIcon)
  78. }
  79. }
  80. self.finish()
  81. }
  82. }
  83. }