NCOperationSaveLivePhoto.swift 5.7 KB

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