NCCollectionViewDownloadThumbnail.swift 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 Queuer
  25. import NextcloudKit
  26. import Realm
  27. class NCCollectionViewDownloadThumbnail: ConcurrentOperation {
  28. var metadata: tableMetadata
  29. var cell: NCCellProtocol?
  30. var collectionView: UICollectionView?
  31. var fileNamePath: String
  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.fileNamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId)
  40. self.fileNamePreviewLocalPath = utilityFileSystem.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)
  41. self.fileNameIconLocalPath = utilityFileSystem.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)
  42. }
  43. override func start() {
  44. guard !isCancelled else { return self.finish() }
  45. var etagResource: String?
  46. let sizePreview = NCUtility().getSizePreview(width: metadata.width, height: metadata.height)
  47. if FileManager.default.fileExists(atPath: fileNameIconLocalPath) && FileManager.default.fileExists(atPath: fileNamePreviewLocalPath) {
  48. etagResource = metadata.etagResource
  49. }
  50. NextcloudKit.shared.downloadPreview(fileNamePathOrFileId: fileNamePath,
  51. fileNamePreviewLocalPath: fileNamePreviewLocalPath,
  52. widthPreview: Int(sizePreview.width),
  53. heightPreview: Int(sizePreview.height),
  54. fileNameIconLocalPath: fileNameIconLocalPath,
  55. sizeIcon: NCGlobal.shared.sizeIcon,
  56. etag: etagResource,
  57. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { _, _, imageIcon, _, etag, error in
  58. if error == .success, let image = imageIcon {
  59. NCManageDatabase.shared.setMetadataEtagResource(ocId: self.metadata.ocId, etagResource: etag)
  60. DispatchQueue.main.async {
  61. if self.metadata.ocId == self.cell?.fileObjectId, let filePreviewImageView = self.cell?.filePreviewImageView {
  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 = image },
  70. completion: nil)
  71. } else {
  72. self.collectionView?.reloadData()
  73. }
  74. }
  75. }
  76. self.finish()
  77. }
  78. }
  79. }