NCCollectionViewDownloadThumbnail.swift 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 RealmSwift
  28. class NCCollectionViewDownloadThumbnail: ConcurrentOperation, @unchecked Sendable {
  29. var metadata: tableMetadata
  30. var collectionView: UICollectionView?
  31. var ext = ""
  32. let utilityFileSystem = NCUtilityFileSystem()
  33. let utility = NCUtility()
  34. init(metadata: tableMetadata, collectionView: UICollectionView?, ext: String) {
  35. self.metadata = tableMetadata.init(value: metadata)
  36. self.collectionView = collectionView
  37. self.ext = ext
  38. }
  39. override func start() {
  40. guard !isCancelled else { return self.finish() }
  41. var etagResource: String?
  42. if utilityFileSystem.fileProviderStorageImageExists(metadata.ocId, etag: metadata.etag) {
  43. etagResource = metadata.etagResource
  44. }
  45. NextcloudKit.shared.downloadPreview(fileId: metadata.fileId,
  46. etag: etagResource,
  47. account: self.metadata.account,
  48. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { _, _, _, etag, responseData, error in
  49. if error == .success, let data = responseData?.data, let collectionView = self.collectionView {
  50. NCManageDatabase.shared.setMetadataEtagResource(ocId: self.metadata.ocId, etagResource: etag)
  51. NCUtility().createImageFileFrom(data: data, metadata: self.metadata)
  52. let image = self.utility.getImage(ocId: self.metadata.ocId, etag: self.metadata.etag, ext: self.ext)
  53. DispatchQueue.main.async {
  54. for case let cell as NCCellProtocol in collectionView.visibleCells where cell.fileOcId == self.metadata.ocId {
  55. if let filePreviewImageView = cell.filePreviewImageView {
  56. filePreviewImageView.contentMode = .scaleAspectFill
  57. if self.metadata.hasPreviewBorder {
  58. filePreviewImageView.layer.borderWidth = 0.2
  59. filePreviewImageView.layer.borderColor = UIColor.systemGray3.cgColor
  60. }
  61. if let photoCell = (cell as? NCPhotoCell),
  62. photoCell.bounds.size.width > 100 {
  63. cell.hideButtonMore(false)
  64. cell.hideImageStatus(false)
  65. }
  66. UIView.transition(with: filePreviewImageView,
  67. duration: 0.75,
  68. options: .transitionCrossDissolve,
  69. animations: { filePreviewImageView.image = image },
  70. completion: nil)
  71. break
  72. }
  73. }
  74. }
  75. }
  76. self.finish()
  77. }
  78. }
  79. }