NCCameraRoll.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // NCCameraRoll.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 21/12/22.
  6. // Copyright © 2022 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 Foundation
  24. import Photos
  25. import NextcloudKit
  26. class NCCameraRoll: NSObject {
  27. let utilityFileSystem = NCUtilityFileSystem()
  28. func extractCameraRoll(from metadata: tableMetadata, completition: @escaping (_ metadatas: [tableMetadata]) -> Void) {
  29. var chunkSize = NCGlobal.shared.chunkSizeMBCellular
  30. if NCNetworking.shared.networkReachability == NKCommon.TypeReachability.reachableEthernetOrWiFi {
  31. chunkSize = NCGlobal.shared.chunkSizeMBEthernetOrWiFi
  32. }
  33. var metadatas: [tableMetadata] = []
  34. let metadataSource = tableMetadata.init(value: metadata)
  35. guard !metadata.isExtractFile else { return completition([metadataSource]) }
  36. guard !metadataSource.assetLocalIdentifier.isEmpty else {
  37. let filePath = utilityFileSystem.getDirectoryProviderStorageOcId(metadataSource.ocId, fileNameView: metadataSource.fileName)
  38. metadataSource.size = utilityFileSystem.getFileSize(filePath: filePath)
  39. let results = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: metadataSource.fileNameView, mimeType: metadataSource.contentType, directory: false)
  40. metadataSource.contentType = results.mimeType
  41. metadataSource.iconName = results.iconName
  42. metadataSource.classFile = results.classFile
  43. if let date = utilityFileSystem.getFileCreationDate(filePath: filePath) {
  44. metadataSource.creationDate = date
  45. }
  46. if let date = utilityFileSystem.getFileModificationDate(filePath: filePath) {
  47. metadataSource.date = date
  48. }
  49. if metadataSource.size > chunkSize {
  50. metadataSource.chunk = chunkSize
  51. } else {
  52. metadataSource.chunk = 0
  53. }
  54. metadataSource.e2eEncrypted = metadata.isDirectoryE2EE
  55. if metadataSource.chunk > 0 || metadataSource.e2eEncrypted {
  56. metadataSource.session = NextcloudKit.shared.nkCommonInstance.sessionIdentifierUpload
  57. }
  58. metadataSource.isExtractFile = true
  59. if let metadata = NCManageDatabase.shared.addMetadata(metadataSource) {
  60. metadatas.append(metadata)
  61. }
  62. return completition(metadatas)
  63. }
  64. extractImageVideoFromAssetLocalIdentifier(metadata: metadataSource, modifyMetadataForUpload: true) { metadata, fileNamePath, error in
  65. if let metadata = metadata, let fileNamePath = fileNamePath, !error {
  66. metadatas.append(metadata)
  67. let toPath = self.utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)
  68. self.utilityFileSystem.moveFile(atPath: fileNamePath, toPath: toPath)
  69. let fetchAssets = PHAsset.fetchAssets(withLocalIdentifiers: [metadataSource.assetLocalIdentifier], options: nil)
  70. if metadata.isLivePhoto, fetchAssets.count > 0 {
  71. self.createMetadataLivePhoto(metadata: metadata, asset: fetchAssets.firstObject) { metadata in
  72. if let metadata = metadata, let metadata = NCManageDatabase.shared.addMetadata(metadata) {
  73. metadatas.append(metadata)
  74. }
  75. completition(metadatas)
  76. }
  77. } else {
  78. completition(metadatas)
  79. }
  80. } else {
  81. completition(metadatas)
  82. }
  83. }
  84. }
  85. func extractCameraRoll(from metadata: tableMetadata) async -> [tableMetadata] {
  86. await withUnsafeContinuation({ continuation in
  87. extractCameraRoll(from: metadata) { metadatas in
  88. continuation.resume(returning: metadatas)
  89. }
  90. })
  91. }
  92. func extractImageVideoFromAssetLocalIdentifier(metadata: tableMetadata,
  93. modifyMetadataForUpload: Bool,
  94. completion: @escaping (_ metadata: tableMetadata?, _ fileNamePath: String?, _ error: Bool) -> Void) {
  95. var fileNamePath: String?
  96. let metadata = tableMetadata.init(value: metadata)
  97. var compatibilityFormat: Bool = false
  98. var chunkSize = NCGlobal.shared.chunkSizeMBCellular
  99. if NCNetworking.shared.networkReachability == NKCommon.TypeReachability.reachableEthernetOrWiFi {
  100. chunkSize = NCGlobal.shared.chunkSizeMBEthernetOrWiFi
  101. }
  102. func callCompletionWithError(_ error: Bool = true) {
  103. if error {
  104. completion(nil, nil, true)
  105. } else {
  106. var metadataReturn = metadata
  107. if modifyMetadataForUpload {
  108. if metadata.size > chunkSize {
  109. metadata.chunk = chunkSize
  110. } else {
  111. metadata.chunk = 0
  112. }
  113. metadata.e2eEncrypted = metadata.isDirectoryE2EE
  114. if metadata.chunk > 0 || metadata.e2eEncrypted {
  115. metadata.session = NextcloudKit.shared.nkCommonInstance.sessionIdentifierUpload
  116. }
  117. metadata.isExtractFile = true
  118. if let metadata = NCManageDatabase.shared.addMetadata(metadata) {
  119. metadataReturn = metadata
  120. }
  121. }
  122. completion(metadataReturn, fileNamePath, error)
  123. }
  124. }
  125. let fetchAssets = PHAsset.fetchAssets(withLocalIdentifiers: [metadata.assetLocalIdentifier], options: nil)
  126. guard fetchAssets.count > 0, let asset = fetchAssets.firstObject else {
  127. return callCompletionWithError()
  128. }
  129. let extensionAsset = asset.originalFilename.pathExtension.uppercased()
  130. let creationDate = asset.creationDate ?? Date()
  131. let modificationDate = asset.modificationDate ?? Date()
  132. if asset.mediaType == PHAssetMediaType.image && (extensionAsset == "HEIC" || extensionAsset == "DNG") && NCKeychain().formatCompatibility {
  133. let fileName = (metadata.fileNameView as NSString).deletingPathExtension + ".jpg"
  134. metadata.contentType = "image/jpeg"
  135. fileNamePath = NSTemporaryDirectory() + fileName
  136. metadata.fileNameView = fileName
  137. if !metadata.isDirectoryE2EE {
  138. metadata.fileName = fileName
  139. }
  140. compatibilityFormat = true
  141. } else {
  142. fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  143. }
  144. guard let fileNamePath = fileNamePath else { return callCompletionWithError() }
  145. if asset.mediaType == PHAssetMediaType.image {
  146. let options = PHImageRequestOptions()
  147. options.isNetworkAccessAllowed = true
  148. if compatibilityFormat {
  149. options.deliveryMode = .opportunistic
  150. } else {
  151. options.deliveryMode = .highQualityFormat
  152. }
  153. options.isSynchronous = true
  154. if extensionAsset == "DNG" {
  155. options.version = PHImageRequestOptionsVersion.original
  156. }
  157. options.progressHandler = { progress, error, _, _ in
  158. print(progress)
  159. if error != nil { return callCompletionWithError() }
  160. }
  161. PHImageManager.default().requestImageDataAndOrientation(for: asset, options: options) { data, _, _, _ in
  162. guard var data = data else { return callCompletionWithError() }
  163. if compatibilityFormat {
  164. guard let ciImage = CIImage(data: data), let colorSpace = ciImage.colorSpace, let dataJPEG = CIContext().jpegRepresentation(of: ciImage, colorSpace: colorSpace) else { return callCompletionWithError() }
  165. data = dataJPEG
  166. }
  167. self.utilityFileSystem.removeFile(atPath: fileNamePath)
  168. do {
  169. try data.write(to: URL(fileURLWithPath: fileNamePath), options: .atomic)
  170. } catch { return callCompletionWithError() }
  171. metadata.creationDate = creationDate as NSDate
  172. metadata.date = modificationDate as NSDate
  173. metadata.size = self.utilityFileSystem.getFileSize(filePath: fileNamePath)
  174. return callCompletionWithError(false)
  175. }
  176. } else if asset.mediaType == PHAssetMediaType.video {
  177. let options = PHVideoRequestOptions()
  178. options.isNetworkAccessAllowed = true
  179. options.version = PHVideoRequestOptionsVersion.current
  180. options.progressHandler = { progress, error, _, _ in
  181. print(progress)
  182. if error != nil { return callCompletionWithError() }
  183. }
  184. PHImageManager.default().requestAVAsset(forVideo: asset, options: options) { asset, _, _ in
  185. if let asset = asset as? AVURLAsset {
  186. self.utilityFileSystem.removeFile(atPath: fileNamePath)
  187. do {
  188. try FileManager.default.copyItem(at: asset.url, to: URL(fileURLWithPath: fileNamePath))
  189. metadata.creationDate = creationDate as NSDate
  190. metadata.date = modificationDate as NSDate
  191. metadata.size = self.utilityFileSystem.getFileSize(filePath: fileNamePath)
  192. return callCompletionWithError(false)
  193. } catch { return callCompletionWithError() }
  194. } else if let asset = asset as? AVComposition, asset.tracks.count > 1, let exporter = AVAssetExportSession(asset: asset, presetName: AVAssetExportPresetHighestQuality) {
  195. exporter.outputURL = URL(fileURLWithPath: fileNamePath)
  196. exporter.outputFileType = AVFileType.mp4
  197. exporter.shouldOptimizeForNetworkUse = true
  198. exporter.exportAsynchronously {
  199. if exporter.status == .completed {
  200. metadata.creationDate = creationDate as NSDate
  201. metadata.date = modificationDate as NSDate
  202. metadata.size = self.utilityFileSystem.getFileSize(filePath: fileNamePath)
  203. return callCompletionWithError(false)
  204. } else { return callCompletionWithError() }
  205. }
  206. } else {
  207. return callCompletionWithError()
  208. }
  209. }
  210. } else {
  211. return callCompletionWithError()
  212. }
  213. }
  214. private func createMetadataLivePhoto(metadata: tableMetadata,
  215. asset: PHAsset?,
  216. completion: @escaping (_ metadata: tableMetadata?) -> Void) {
  217. guard let asset = asset else { return completion(nil) }
  218. let options = PHLivePhotoRequestOptions()
  219. options.deliveryMode = PHImageRequestOptionsDeliveryMode.fastFormat
  220. options.isNetworkAccessAllowed = true
  221. let ocId = NSUUID().uuidString
  222. let fileName = (metadata.fileName as NSString).deletingPathExtension + ".mov"
  223. let fileNamePath = utilityFileSystem.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)
  224. var chunkSize = NCGlobal.shared.chunkSizeMBCellular
  225. if NCNetworking.shared.networkReachability == NKCommon.TypeReachability.reachableEthernetOrWiFi {
  226. chunkSize = NCGlobal.shared.chunkSizeMBEthernetOrWiFi
  227. }
  228. PHImageManager.default().requestLivePhoto(for: asset, targetSize: UIScreen.main.bounds.size, contentMode: PHImageContentMode.default, options: options) { livePhoto, _ in
  229. guard let livePhoto = livePhoto else { return completion(nil) }
  230. var videoResource: PHAssetResource?
  231. for resource in PHAssetResource.assetResources(for: livePhoto) where resource.type == PHAssetResourceType.pairedVideo {
  232. videoResource = resource
  233. break
  234. }
  235. guard let videoResource = videoResource else { return completion(nil) }
  236. self.utilityFileSystem.removeFile(atPath: fileNamePath)
  237. PHAssetResourceManager.default().writeData(for: videoResource, toFile: URL(fileURLWithPath: fileNamePath), options: nil) { error in
  238. if error != nil { return completion(nil) }
  239. let metadataLivePhoto = NCManageDatabase.shared.createMetadata(account: metadata.account,
  240. user: metadata.user,
  241. userId: metadata.userId,
  242. fileName: fileName,
  243. fileNameView: fileName,
  244. ocId: ocId,
  245. serverUrl: metadata.serverUrl,
  246. urlBase: metadata.urlBase,
  247. url: "",
  248. contentType: "")
  249. metadataLivePhoto.livePhotoFile = metadata.fileName
  250. metadataLivePhoto.classFile = NKCommon.TypeClassFile.video.rawValue
  251. metadataLivePhoto.isExtractFile = true
  252. metadataLivePhoto.session = metadata.session
  253. metadataLivePhoto.sessionSelector = metadata.sessionSelector
  254. metadataLivePhoto.size = self.utilityFileSystem.getFileSize(filePath: fileNamePath)
  255. metadataLivePhoto.status = metadata.status
  256. if metadataLivePhoto.size > chunkSize {
  257. metadataLivePhoto.chunk = chunkSize
  258. } else {
  259. metadataLivePhoto.chunk = 0
  260. }
  261. metadataLivePhoto.e2eEncrypted = metadata.isDirectoryE2EE
  262. if metadataLivePhoto.chunk > 0 || metadataLivePhoto.e2eEncrypted {
  263. metadataLivePhoto.session = NextcloudKit.shared.nkCommonInstance.sessionIdentifierUpload
  264. }
  265. metadataLivePhoto.creationDate = metadata.creationDate
  266. metadataLivePhoto.date = metadata.date
  267. metadataLivePhoto.uploadDate = metadata.uploadDate
  268. return completion(NCManageDatabase.shared.addMetadata(metadataLivePhoto))
  269. }
  270. }
  271. }
  272. }