NCAutoUpload.swift 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. //
  2. // NCAutoUpload.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/01/21.
  6. // Copyright © 2021 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 CoreLocation
  25. import NCCommunication
  26. class NCAutoUpload: NSObject, CLLocationManagerDelegate {
  27. @objc static let shared: NCAutoUpload = {
  28. let instance = NCAutoUpload()
  29. return instance
  30. }()
  31. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. public var locationManager: CLLocationManager?
  33. // MARK: -
  34. func startSignificantChangeUpdates() {
  35. if locationManager == nil {
  36. locationManager = CLLocationManager.init()
  37. locationManager?.delegate = self
  38. locationManager?.requestAlwaysAuthorization()
  39. }
  40. locationManager?.startMonitoringSignificantLocationChanges()
  41. }
  42. @objc func stopSignificantChangeUpdates() {
  43. locationManager?.stopMonitoringSignificantLocationChanges()
  44. }
  45. func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
  46. let location = locations.last
  47. let latitude = String(describing: location?.coordinate.latitude)
  48. let longitude = String(describing: location?.coordinate.longitude)
  49. NCCommunicationCommon.shared.writeLog("update location manager: latitude " + latitude + ", longitude " + longitude)
  50. if let account = NCManageDatabase.shared.getAccountActive() {
  51. if account.autoUpload && account.autoUploadBackground && UIApplication.shared.applicationState == UIApplication.State.background {
  52. self.uploadNewAssets()
  53. }
  54. }
  55. }
  56. func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
  57. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
  58. self.stopSignificantChangeUpdates()
  59. }
  60. func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
  61. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
  62. self.stopSignificantChangeUpdates()
  63. }
  64. // MARK: -
  65. @objc func initAutoUpload(viewController: UIViewController?) {
  66. if let account = NCManageDatabase.shared.getAccountActive() {
  67. if account.autoUpload {
  68. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: viewController) { (hasPermission) in
  69. if hasPermission {
  70. self.uploadNewAssets()
  71. if account.autoUploadBackground {
  72. NCAskAuthorization.shared.askAuthorizationLocationManager(viewController: viewController) { (hasPermissions) in
  73. if hasPermissions {
  74. self.startSignificantChangeUpdates()
  75. } else {
  76. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
  77. self.stopSignificantChangeUpdates()
  78. }
  79. }
  80. }
  81. } else {
  82. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUpload", state: false)
  83. self.stopSignificantChangeUpdates()
  84. }
  85. }
  86. }
  87. } else {
  88. stopSignificantChangeUpdates()
  89. }
  90. }
  91. @objc func autoUploadFullPhotos() {
  92. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
  93. if hasPermission {
  94. self.uploadAssetsNewAndFull(selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll)
  95. }
  96. }
  97. }
  98. func uploadNewAssets() {
  99. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
  100. if hasPermission {
  101. self.uploadAssetsNewAndFull(selector: NCBrandGlobal.shared.selectorUploadAutoUpload)
  102. }
  103. }
  104. }
  105. private func uploadAssetsNewAndFull(selector: String) {
  106. }
  107. // MARK: -
  108. @objc func alignPhotoLibrary() {
  109. if let account = NCManageDatabase.shared.getAccountActive() {
  110. getCameraRollAssets(account: account, selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll, alignPhotoLibrary: true) { (assets) in
  111. NCManageDatabase.shared.clearTable(tablePhotoLibrary.self, account: account.account)
  112. if let assets = assets {
  113. NCManageDatabase.shared.addPhotoLibrary(assets, account: account.account)
  114. NCCommunicationCommon.shared.writeLog("Align Photo Library \(assets.count)")
  115. }
  116. }
  117. }
  118. }
  119. private func getCameraRollAssets(account: tableAccount, selector: String, alignPhotoLibrary: Bool, completion: @escaping (_ assets: [PHAsset]?)->()) {
  120. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
  121. if hasPermission {
  122. let assetCollection = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSubtype.smartAlbumUserLibrary, options: nil)
  123. if assetCollection.count > 0 {
  124. let predicateImage = NSPredicate(format: "mediaType == %i", PHAssetMediaType.image.rawValue)
  125. let predicateVideo = NSPredicate(format: "mediaType == %i", PHAssetMediaType.video.rawValue)
  126. var predicate: NSPredicate?
  127. let fetchOptions = PHFetchOptions()
  128. var newAssets: [PHAsset] = []
  129. if alignPhotoLibrary || (account.autoUploadImage && account.autoUploadVideo) {
  130. predicate = NSCompoundPredicate(orPredicateWithSubpredicates: [predicateImage, predicateVideo])
  131. } else if account.autoUploadImage {
  132. predicate = predicateImage
  133. } else if account.autoUploadVideo {
  134. predicate = predicateVideo
  135. }
  136. fetchOptions.predicate = predicate
  137. let assets: PHFetchResult<PHAsset> = PHAsset.fetchAssets(in: assetCollection.firstObject!, options: fetchOptions)
  138. if selector == NCBrandGlobal.shared.selectorUploadAutoUpload {
  139. var creationDate = ""
  140. var idAsset = ""
  141. let idsAsset = NCManageDatabase.shared.getPhotoLibraryIdAsset(image: account.autoUploadImage, video: account.autoUploadVideo, account: account.account)
  142. assets.enumerateObjects { (asset, _, _) in
  143. if asset.creationDate != nil { creationDate = String(describing: asset.creationDate) }
  144. idAsset = account.account + asset.localIdentifier + creationDate
  145. if !(idsAsset?.contains(idAsset) ?? false) {
  146. newAssets.append(asset)
  147. }
  148. }
  149. } else {
  150. assets.enumerateObjects { (asset, _, _) in
  151. newAssets.append(asset)
  152. }
  153. }
  154. completion(newAssets)
  155. } else {
  156. completion(nil)
  157. }
  158. } else {
  159. completion(nil)
  160. }
  161. }
  162. }
  163. }