NCAutoUpload.swift 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: nil) { (hasPermission) in
  53. if hasPermission {
  54. self.uploadAssetsNewAndFull(selector: NCBrandGlobal.shared.selectorUploadAutoUpload)
  55. }
  56. }
  57. }
  58. }
  59. }
  60. func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
  61. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
  62. self.stopSignificantChangeUpdates()
  63. }
  64. func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
  65. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
  66. self.stopSignificantChangeUpdates()
  67. }
  68. // MARK: -
  69. @objc func initAutoUpload(viewController: UIViewController?) {
  70. if let account = NCManageDatabase.shared.getAccountActive() {
  71. if account.autoUpload {
  72. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: viewController) { (hasPermission) in
  73. if hasPermission {
  74. self.uploadAssetsNewAndFull(selector: NCBrandGlobal.shared.selectorUploadAutoUpload)
  75. if account.autoUploadBackground {
  76. NCAskAuthorization.shared.askAuthorizationLocationManager(viewController: viewController) { (hasPermissions) in
  77. if hasPermissions {
  78. self.startSignificantChangeUpdates()
  79. } else {
  80. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
  81. self.stopSignificantChangeUpdates()
  82. }
  83. }
  84. }
  85. } else {
  86. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUpload", state: false)
  87. self.stopSignificantChangeUpdates()
  88. }
  89. }
  90. }
  91. } else {
  92. stopSignificantChangeUpdates()
  93. }
  94. }
  95. @objc func autoUploadFullPhotos() {
  96. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
  97. if hasPermission {
  98. self.uploadAssetsNewAndFull(selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll)
  99. }
  100. }
  101. }
  102. private func uploadAssetsNewAndFull(selector: String) {
  103. }
  104. // MARK: -
  105. @objc func alignPhotoLibrary(viewController: UIViewController?) {
  106. if let account = NCManageDatabase.shared.getAccountActive() {
  107. getCameraRollAssets(viewController: viewController, account: account, selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll, alignPhotoLibrary: true) { (assets) in
  108. NCManageDatabase.shared.clearTable(tablePhotoLibrary.self, account: account.account)
  109. if let assets = assets {
  110. NCManageDatabase.shared.addPhotoLibrary(assets, account: account.account)
  111. NCCommunicationCommon.shared.writeLog("Align Photo Library \(assets.count)")
  112. }
  113. }
  114. }
  115. }
  116. private func getCameraRollAssets(viewController: UIViewController?, account: tableAccount, selector: String, alignPhotoLibrary: Bool, completion: @escaping (_ assets: [PHAsset]?)->()) {
  117. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: viewController) { (hasPermission) in
  118. if hasPermission {
  119. let assetCollection = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSubtype.smartAlbumUserLibrary, options: nil)
  120. if assetCollection.count > 0 {
  121. let predicateImage = NSPredicate(format: "mediaType == %i", PHAssetMediaType.image.rawValue)
  122. let predicateVideo = NSPredicate(format: "mediaType == %i", PHAssetMediaType.video.rawValue)
  123. var predicate: NSPredicate?
  124. let fetchOptions = PHFetchOptions()
  125. var newAssets: [PHAsset] = []
  126. if alignPhotoLibrary || (account.autoUploadImage && account.autoUploadVideo) {
  127. predicate = NSCompoundPredicate(orPredicateWithSubpredicates: [predicateImage, predicateVideo])
  128. } else if account.autoUploadImage {
  129. predicate = predicateImage
  130. } else if account.autoUploadVideo {
  131. predicate = predicateVideo
  132. } else {
  133. completion(nil)
  134. return
  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. }