NCMediaDownloadThumbnail.swift 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. //
  2. // NCMediaDownloadThumbnail.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. import Alamofire
  27. class NCMediaDownloadThumbnail: ConcurrentOperation, @unchecked Sendable {
  28. var metadata: NCMediaDataSource.Metadata
  29. let utilityFileSystem = NCUtilityFileSystem()
  30. let global = NCGlobal.shared
  31. let media: NCMedia
  32. var session: NCSession.Session
  33. init(metadata: NCMediaDataSource.Metadata, media: NCMedia) {
  34. self.metadata = metadata
  35. self.media = media
  36. self.session = media.session
  37. }
  38. override func start() {
  39. guard !isCancelled,
  40. let tableMetadata = NCManageDatabase.shared.getResultMetadataFromOcId(self.metadata.ocId)?.freeze() else { return self.finish() }
  41. var etagResource: String?
  42. if utilityFileSystem.fileProviderStorageImageExists(metadata.ocId, etag: metadata.etag) {
  43. etagResource = tableMetadata.etagResource
  44. }
  45. NextcloudKit.shared.downloadPreview(fileId: tableMetadata.fileId,
  46. etag: etagResource,
  47. account: self.session.account,
  48. options: NKRequestOptions(queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)) { _, _, _, etag, responseData, error in
  49. if error == .success, let data = responseData?.data {
  50. self.media.filesExists.append(self.metadata.ocId)
  51. NCManageDatabase.shared.setMetadataEtagResource(ocId: self.metadata.ocId, etagResource: etag)
  52. NCUtility().createImageFileFrom(data: data, metadata: tableMetadata)
  53. let image = NCUtility().getImage(ocId: self.metadata.ocId, etag: self.metadata.etag, ext: NCGlobal.shared.getSizeExtension(column: self.media.numberOfColumns))
  54. DispatchQueue.main.async {
  55. for case let cell as NCMediaCell in self.media.collectionView.visibleCells {
  56. if cell.ocId == self.metadata.ocId {
  57. UIView.transition(with: cell.imageItem, duration: 0.75, options: .transitionCrossDissolve, animations: { cell.imageItem.image = image
  58. }, completion: nil)
  59. break
  60. }
  61. }
  62. }
  63. } else if error.errorCode == self.global.errorResourceNotFound {
  64. NotificationCenter.default.postOnMainThread(name: self.global.notificationCenterDeleteFile, userInfo: ["ocId": tableMetadata.ocId, "error": error])
  65. }
  66. self.finish()
  67. }
  68. }
  69. }