NCCameraRoll.swift 15 KB

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