NCMediaDownloadThumbnaill.swift 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 fileNamePath: String
  30. var fileNamePreviewLocalPath: String
  31. var fileNameIconLocalPath: String
  32. let utilityFileSystem = NCUtilityFileSystem()
  33. init(metadata: tableMetadata, media: NCMedia) {
  34. self.metadata = tableMetadata.init(value: metadata)
  35. self.media = media
  36. self.fileNamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId)
  37. self.fileNamePreviewLocalPath = utilityFileSystem.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)
  38. self.fileNameIconLocalPath = utilityFileSystem.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)
  39. }
  40. override func start() {
  41. guard !isCancelled else { return self.finish() }
  42. var etagResource: String?
  43. let sizePreview = NCUtility().getSizePreview(width: metadata.width, height: metadata.height)
  44. if FileManager.default.fileExists(atPath: fileNameIconLocalPath) && FileManager.default.fileExists(atPath: fileNamePreviewLocalPath) {
  45. etagResource = metadata.etagResource
  46. }
  47. NextcloudKit.shared.downloadPreview(fileNamePathOrFileId: fileNamePath,
  48. fileNamePreviewLocalPath: fileNamePreviewLocalPath,
  49. widthPreview: Int(sizePreview.width),
  50. heightPreview: Int(sizePreview.height),
  51. fileNameIconLocalPath: fileNameIconLocalPath,
  52. sizeIcon: NCGlobal.shared.sizeIcon,
  53. etag: etagResource,
  54. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { _, imagePreview, _, _, etag, error in
  55. if error == .success, let image = imagePreview {
  56. NCManageDatabase.shared.setMetadataEtagResource(ocId: self.metadata.ocId, etagResource: etag)
  57. DispatchQueue.main.async {
  58. if let visibleCells = self.media.collectionView?.indexPathsForVisibleItems.sorted(by: { $0.row < $1.row }).compactMap({ self.media.collectionView?.cellForItem(at: $0) }) {
  59. for case let cell as NCGridMediaCell in visibleCells {
  60. if cell.ocId == self.metadata.ocId, let filePreviewImageView = cell.imageItem {
  61. UIView.transition(with: filePreviewImageView,
  62. duration: 0.75,
  63. options: .transitionCrossDissolve,
  64. animations: { filePreviewImageView.image = image },
  65. completion: nil)
  66. break
  67. }
  68. }
  69. }
  70. }
  71. NCImageCache.shared.setMediaSize(ocId: self.metadata.ocId, etag: self.metadata.etag, size: image.size)
  72. }
  73. self.finish()
  74. }
  75. }
  76. override func finish(success: Bool = true) {
  77. super.finish(success: success)
  78. if (metadata.width == 0 && metadata.height == 0) || (NCNetworking.shared.downloadThumbnailQueue.operationCount == 0) {
  79. self.media.collectionViewReloadData()
  80. }
  81. }
  82. }