NCOperationSaveLivePhoto.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. class NCOperationSaveLivePhoto: ConcurrentOperation {
  27. var metadata: tableMetadata
  28. var metadataMOV: tableMetadata
  29. let hud = JGProgressHUD()
  30. let appDelegate = UIApplication.shared.delegate as? AppDelegate
  31. init(metadata: tableMetadata, metadataMOV: tableMetadata) {
  32. self.metadata = tableMetadata.init(value: metadata)
  33. self.metadataMOV = tableMetadata.init(value: metadataMOV)
  34. }
  35. override func start() {
  36. guard !isCancelled else { return self.finish() }
  37. DispatchQueue.main.async {
  38. self.hud.indicatorView = JGProgressHUDRingIndicatorView()
  39. if let indicatorView = self.hud.indicatorView as? JGProgressHUDRingIndicatorView {
  40. indicatorView.ringWidth = 1.5
  41. }
  42. self.hud.textLabel.text = NSLocalizedString("_download_image_", comment: "")
  43. self.hud.detailTextLabel.text = self.metadata.fileName
  44. self.hud.show(in: (self.appDelegate?.window?.rootViewController?.view)!)
  45. }
  46. NCNetworking.shared.download(metadata: metadata, selector: "", notificationCenterProgressTask: false, checkfileProviderStorageExists: true) { _ in
  47. } progressHandler: { progress in
  48. self.hud.progress = Float(progress.fractionCompleted)
  49. } completion: { _, error in
  50. guard error == .success else {
  51. DispatchQueue.main.async {
  52. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  53. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  54. self.hud.dismiss()
  55. }
  56. return self.finish()
  57. }
  58. NCNetworking.shared.download(metadata: self.metadataMOV, selector: "", notificationCenterProgressTask: false, checkfileProviderStorageExists: true) { _ in
  59. DispatchQueue.main.async {
  60. self.hud.textLabel.text = NSLocalizedString("_download_video_", comment: "")
  61. self.hud.detailTextLabel.text = self.metadataMOV.fileName
  62. }
  63. } progressHandler: { progress in
  64. self.hud.progress = Float(progress.fractionCompleted)
  65. } completion: { _, error in
  66. guard error == .success else {
  67. DispatchQueue.main.async {
  68. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  69. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  70. self.hud.dismiss()
  71. }
  72. return self.finish()
  73. }
  74. self.saveLivePhotoToDisk(metadata: self.metadata, metadataMov: self.metadataMOV)
  75. }
  76. }
  77. }
  78. func saveLivePhotoToDisk(metadata: tableMetadata, metadataMov: tableMetadata) {
  79. let fileNameImage = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!)
  80. let fileNameMov = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadataMov.ocId, fileNameView: metadataMov.fileNameView)!)
  81. DispatchQueue.main.async {
  82. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_", comment: "")
  83. self.hud.detailTextLabel.text = ""
  84. }
  85. NCLivePhoto.generate(from: fileNameImage, videoURL: fileNameMov, progress: { progress in
  86. self.hud.progress = Float(progress)
  87. }, completion: { _, resources in
  88. if let resources {
  89. NCLivePhoto.saveToLibrary(resources) { result in
  90. DispatchQueue.main.async {
  91. if !result {
  92. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  93. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  94. } else {
  95. self.hud.indicatorView = JGProgressHUDSuccessIndicatorView()
  96. self.hud.textLabel.text = NSLocalizedString("_success_", comment: "")
  97. }
  98. self.hud.dismiss()
  99. }
  100. return self.finish()
  101. }
  102. } else {
  103. DispatchQueue.main.async {
  104. self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
  105. self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
  106. self.hud.dismiss()
  107. }
  108. return self.finish()
  109. }
  110. })
  111. }
  112. }