forked from nextcloud/ios
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNCOperationSaveLivePhoto.swift
131 lines (121 loc) · 6.18 KB
/
NCOperationSaveLivePhoto.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//
// NCOperationSaveLivePhoto.swift
// Nextcloud
//
// Created by Marino Faggiana on 19/10/23.
// Copyright © 2023 Marino Faggiana. All rights reserved.
//
// Author Marino Faggiana <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
import UIKit
import Queuer
import JGProgressHUD
import NextcloudKit
class NCOperationSaveLivePhoto: ConcurrentOperation {
var metadata: tableMetadata
var metadataMOV: tableMetadata
let hud = JGProgressHUD()
let appDelegate = UIApplication.shared.delegate as? AppDelegate
let utilityFileSystem = NCUtilityFileSystem()
init(metadata: tableMetadata, metadataMOV: tableMetadata) {
self.metadata = tableMetadata.init(value: metadata)
self.metadataMOV = tableMetadata.init(value: metadataMOV)
}
override func start() {
guard !isCancelled,
let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
selector: ""),
let metadataLive = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadataMOV],
session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
selector: "") else { return self.finish() }
DispatchQueue.main.async {
self.hud.indicatorView = JGProgressHUDRingIndicatorView()
if let indicatorView = self.hud.indicatorView as? JGProgressHUDRingIndicatorView {
indicatorView.ringWidth = 1.5
}
self.hud.textLabel.text = NSLocalizedString("_download_image_", comment: "")
self.hud.detailTextLabel.text = self.metadata.fileName
self.hud.show(in: (self.appDelegate?.window?.rootViewController?.view)!)
}
NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: false) {
} requestHandler: { _ in
} progressHandler: { progress in
self.hud.progress = Float(progress.fractionCompleted)
} completion: { _, error in
guard error == .success else {
DispatchQueue.main.async {
self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
self.hud.dismiss()
}
return self.finish()
}
NCNetworking.shared.download(metadata: metadataLive, withNotificationProgressTask: false) {
DispatchQueue.main.async {
self.hud.textLabel.text = NSLocalizedString("_download_video_", comment: "")
self.hud.detailTextLabel.text = self.metadataMOV.fileName
}
} progressHandler: { progress in
self.hud.progress = Float(progress.fractionCompleted)
} completion: { _, error in
guard error == .success else {
DispatchQueue.main.async {
self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
self.hud.dismiss()
}
return self.finish()
}
self.saveLivePhotoToDisk(metadata: self.metadata, metadataMov: self.metadataMOV)
}
}
}
func saveLivePhotoToDisk(metadata: tableMetadata, metadataMov: tableMetadata) {
let fileNameImage = URL(fileURLWithPath: utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
let fileNameMov = URL(fileURLWithPath: utilityFileSystem.getDirectoryProviderStorageOcId(metadataMov.ocId, fileNameView: metadataMov.fileNameView))
DispatchQueue.main.async {
self.hud.textLabel.text = NSLocalizedString("_livephoto_save_", comment: "")
self.hud.detailTextLabel.text = ""
}
NCLivePhoto.generate(from: fileNameImage, videoURL: fileNameMov, progress: { progress in
self.hud.progress = Float(progress)
}, completion: { _, resources in
if let resources {
NCLivePhoto.saveToLibrary(resources) { result in
DispatchQueue.main.async {
if !result {
self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
} else {
self.hud.indicatorView = JGProgressHUDSuccessIndicatorView()
self.hud.textLabel.text = NSLocalizedString("_success_", comment: "")
}
self.hud.dismiss()
}
return self.finish()
}
} else {
DispatchQueue.main.async {
self.hud.indicatorView = JGProgressHUDErrorIndicatorView()
self.hud.textLabel.text = NSLocalizedString("_livephoto_save_error_", comment: "")
self.hud.dismiss()
}
return self.finish()
}
})
}
}