NCOperationSaveLivePhoto.swift 5.5 KB

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