NCOperationSaveLivePhoto.swift 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // NCOperationSaveLivePhoto.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/10/23.
  6. // Copyright © 2023 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 Queuer
  25. import JGProgressHUD
  26. import NextcloudKit
  27. class NCOperationSaveLivePhoto: ConcurrentOperation {
  28. var metadata: tableMetadata
  29. var metadataMOV: tableMetadata
  30. let hud = JGProgressHUD()
  31. let appDelegate = UIApplication.shared.delegate as? AppDelegate
  32. let utilityFileSystem = NCUtilityFileSystem()
  33. let hudView: UIView
  34. init(metadata: tableMetadata, metadataMOV: tableMetadata, hudView: UIView) {
  35. self.metadata = tableMetadata.init(value: metadata)
  36. self.metadataMOV = tableMetadata.init(value: metadataMOV)
  37. self.hudView = hudView
  38. }
  39. override func start() {
  40. guard !isCancelled,
  41. let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  42. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  43. selector: ""),
  44. let metadataLive = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadataMOV],
  45. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  46. selector: "") else { return self.finish() }
  47. DispatchQueue.main.async {
  48. self.hud.indicatorView = JGProgressHUDRingIndicatorView()
  49. if let indicatorView = self.hud.indicatorView as? JGProgressHUDRingIndicatorView {
  50. indicatorView.ringWidth = 1.5
  51. indicatorView.ringColor = NCBrandColor.shared.brandElement
  52. }
  53. self.hud.textLabel.text = NSLocalizedString("_download_image_", comment: "")
  54. self.hud.detailTextLabel.text = self.metadata.fileName
  55. self.hud.detailTextLabel.textColor = NCBrandColor.shared.iconImageColor2
  56. self.hud.show(in: self.hudView)
  57. }
  58. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: false) {
  59. } requestHandler: { _ in
  60. } progressHandler: { progress in
  61. self.hud.progress = Float(progress.fractionCompleted)
  62. } completion: { _, error in
  63. guard error == .success else {
  64. DispatchQueue.main.async {
  65. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  66. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  67. self.hud.dismiss()
  68. }
  69. return self.finish()
  70. }
  71. NCNetworking.shared.download(metadata: metadataLive, withNotificationProgressTask: false) {
  72. DispatchQueue.main.async {
  73. self.hud.textLabel.text = NSLocalizedString("_download_video_", comment: "")
  74. self.hud.detailTextLabel.text = self.metadataMOV.fileName
  75. }
  76. } progressHandler: { progress in
  77. self.hud.progress = Float(progress.fractionCompleted)
  78. } completion: { _, error in
  79. guard error == .success else {
  80. DispatchQueue.main.async {
  81. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  82. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  83. self.hud.dismiss()
  84. }
  85. return self.finish()
  86. }
  87. self.saveLivePhotoToDisk(metadata: self.metadata, metadataMov: self.metadataMOV)
  88. }
  89. }
  90. }
  91. func saveLivePhotoToDisk(metadata: tableMetadata, metadataMov: tableMetadata) {
  92. let fileNameImage = URL(fileURLWithPath: utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  93. let fileNameMov = URL(fileURLWithPath: utilityFileSystem.getDirectoryProviderStorageOcId(metadataMov.ocId, fileNameView: metadataMov.fileNameView))
  94. DispatchQueue.main.async {
  95. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_", comment: "")
  96. self.hud.detailTextLabel.text = ""
  97. }
  98. NCLivePhoto.generate(from: fileNameImage, videoURL: fileNameMov, progress: { progress in
  99. self.hud.progress = Float(progress)
  100. }, completion: { _, resources in
  101. if let resources {
  102. NCLivePhoto.saveToLibrary(resources) { result in
  103. DispatchQueue.main.async {
  104. if !result {
  105. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  106. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  107. } else {
  108. self.hud.indicatorView = JGProgressHUDSuccessIndicatorView()
  109. self.hud.indicatorView?.tintColor = NCBrandColor.shared.brandElement
  110. self.hud.textLabel.text = NSLocalizedString("_success_", comment: "")
  111. }
  112. self.hud.dismiss()
  113. }
  114. return self.finish()
  115. }
  116. } else {
  117. DispatchQueue.main.async {
  118. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  119. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  120. self.hud.dismiss()
  121. }
  122. return self.finish()
  123. }
  124. })
  125. }
  126. }