NCOperationSaveLivePhoto.swift 4.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 NextcloudKit
  26. class NCOperationSaveLivePhoto: ConcurrentOperation, @unchecked Sendable {
  27. var metadata: tableMetadata
  28. var metadataMOV: tableMetadata
  29. let hud: NCHud?
  30. let utilityFileSystem = NCUtilityFileSystem()
  31. init(metadata: tableMetadata, metadataMOV: tableMetadata, hudView: UIView) {
  32. self.metadata = tableMetadata.init(value: metadata)
  33. self.metadataMOV = tableMetadata.init(value: metadataMOV)
  34. self.hud = NCHud(hudView)
  35. hud?.initHudRing(text: NSLocalizedString("_download_image_", comment: ""), detailText: self.metadata.fileName)
  36. }
  37. override func start() {
  38. guard !isCancelled,
  39. let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  40. session: NCNetworking.shared.sessionDownload,
  41. selector: ""),
  42. let metadataLive = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadataMOV],
  43. session: NCNetworking.shared.sessionDownload,
  44. selector: "") else { return self.finish() }
  45. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: false) {
  46. } requestHandler: { _ in
  47. } progressHandler: { progress in
  48. self.hud?.progress(progress.fractionCompleted)
  49. } completion: { _, error in
  50. guard error == .success else {
  51. self.hud?.error(text: NSLocalizedString("_livephoto_save_error_", comment: ""))
  52. return self.finish()
  53. }
  54. NCNetworking.shared.download(metadata: metadataLive, withNotificationProgressTask: false) {
  55. self.hud?.setText(text: NSLocalizedString("_download_video_", comment: ""), detailText: self.metadataMOV.fileName)
  56. } progressHandler: { progress in
  57. self.hud?.progress(progress.fractionCompleted)
  58. } completion: { _, error in
  59. guard error == .success else {
  60. self.hud?.error(text: NSLocalizedString("_livephoto_save_error_", comment: ""))
  61. return self.finish()
  62. }
  63. self.saveLivePhotoToDisk(metadata: self.metadata, metadataMov: self.metadataMOV)
  64. }
  65. }
  66. }
  67. func saveLivePhotoToDisk(metadata: tableMetadata, metadataMov: tableMetadata) {
  68. let fileNameImage = URL(fileURLWithPath: utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  69. let fileNameMov = URL(fileURLWithPath: utilityFileSystem.getDirectoryProviderStorageOcId(metadataMov.ocId, fileNameView: metadataMov.fileNameView))
  70. self.hud?.progress(0)
  71. self.hud?.setText(text: NSLocalizedString("_livephoto_save_", comment: ""))
  72. NCLivePhoto.generate(from: fileNameImage, videoURL: fileNameMov, progress: { progress in
  73. self.hud?.progress(progress)
  74. }, completion: { _, resources in
  75. if let resources {
  76. NCLivePhoto.saveToLibrary(resources) { result in
  77. if !result {
  78. self.hud?.error(text: NSLocalizedString("_livephoto_save_error_", comment: ""))
  79. } else {
  80. self.hud?.success()
  81. }
  82. return self.finish()
  83. }
  84. } else {
  85. self.hud?.error(text: NSLocalizedString("_livephoto_save_error_", comment: ""))
  86. return self.finish()
  87. }
  88. })
  89. }
  90. }