NCAutoUpload.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. statusAuthorizationLocationChanged()
  58. }
  59. func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
  60. statusAuthorizationLocationChanged()
  61. }
  62. func statusAuthorizationLocationChanged() {
  63. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
  64. self.stopSignificantChangeUpdates()
  65. }
  66. // MARK: -
  67. @objc func initStateAutoUpload(viewController: UIViewController?) {
  68. if let account = NCManageDatabase.shared.getAccountActive() {
  69. if account.autoUpload {
  70. setupAutoUpload()
  71. if account.autoUploadBackground {
  72. NCAskAuthorization.shared.askAuthorizationLocationManager(viewController: appDelegate.window.rootViewController) { (hasPermissions) in
  73. if hasPermissions {
  74. self.startSignificantChangeUpdates()
  75. } else {
  76. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUploadBackground", state: false)
  77. self.stopSignificantChangeUpdates()
  78. }
  79. }
  80. }
  81. }
  82. } else {
  83. stopSignificantChangeUpdates()
  84. }
  85. }
  86. @objc func setupAutoUpload() {
  87. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
  88. if hasPermission {
  89. self.uploadNewAssets()
  90. } else {
  91. NCManageDatabase.shared.setAccountAutoUploadProperty("autoUpload", state: false)
  92. self.stopSignificantChangeUpdates()
  93. }
  94. }
  95. }
  96. @objc func setupAutoUploadFull() {
  97. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
  98. if hasPermission {
  99. self.uploadAssetsNewAndFull(selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll)
  100. }
  101. }
  102. }
  103. @objc func uploadNewAssets() {
  104. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
  105. if hasPermission {
  106. self.uploadAssetsNewAndFull(selector: NCBrandGlobal.shared.selectorUploadAutoUpload)
  107. }
  108. }
  109. }
  110. @objc func alignPhotoLibrary() {
  111. if let account = NCManageDatabase.shared.getAccountActive() {
  112. getCameraRollAssets(account: account, selector: NCBrandGlobal.shared.selectorUploadAutoUploadAll, alignPhotoLibrary: true) { (assets) in
  113. NCManageDatabase.shared.clearTable(tablePhotoLibrary.self, account: account.account)
  114. if let assets = assets {
  115. NCManageDatabase.shared.addPhotoLibrary(assets, account: account.account)
  116. NCCommunicationCommon.shared.writeLog("Align Photo Library \(assets.count)")
  117. }
  118. }
  119. }
  120. }
  121. private func uploadAssetsNewAndFull(selector: String) {
  122. }
  123. private func getCameraRollAssets(account: tableAccount, selector: String, alignPhotoLibrary: Bool, completion: @escaping (_ assets: [PHAsset]?)->()) {
  124. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: appDelegate.window.rootViewController) { (hasPermission) in
  125. if hasPermission {
  126. let assetCollection = PHAssetCollection.fetchAssetCollections(with: PHAssetCollectionType.smartAlbum, subtype: PHAssetCollectionSubtype.smartAlbumUserLibrary, options: nil)
  127. if assetCollection.count > 0 {
  128. let predicateImage = NSPredicate(format: "mediaType == %i", PHAssetMediaType.image.rawValue)
  129. let predicateVideo = NSPredicate(format: "mediaType == %i", PHAssetMediaType.video.rawValue)
  130. var predicate: NSPredicate?
  131. let fetchOptions = PHFetchOptions()
  132. var newAssets: [PHAsset] = []
  133. if alignPhotoLibrary || (account.autoUploadImage && account.autoUploadVideo) {
  134. predicate = NSCompoundPredicate(orPredicateWithSubpredicates: [predicateImage, predicateVideo])
  135. } else if account.autoUploadImage {
  136. predicate = predicateImage
  137. } else if account.autoUploadVideo {
  138. predicate = predicateVideo
  139. }
  140. fetchOptions.predicate = predicate
  141. let assets: PHFetchResult<PHAsset> = PHAsset.fetchAssets(in: assetCollection.firstObject!, options: fetchOptions)
  142. if selector == NCBrandGlobal.shared.selectorUploadAutoUpload {
  143. var creationDate = ""
  144. var idAsset = ""
  145. let idsAsset = NCManageDatabase.shared.getPhotoLibraryIdAsset(image: account.autoUploadImage, video: account.autoUploadVideo, account: account.account)
  146. assets.enumerateObjects { (asset, _, _) in
  147. if asset.creationDate != nil { creationDate = String(describing: asset.creationDate) }
  148. idAsset = account.account + asset.localIdentifier + creationDate
  149. if !(idsAsset?.contains(idAsset) ?? false) {
  150. newAssets.append(asset)
  151. }
  152. }
  153. } else {
  154. assets.enumerateObjects { (asset, _, _) in
  155. newAssets.append(asset)
  156. }
  157. }
  158. completion(newAssets)
  159. }
  160. } else {
  161. completion(nil)
  162. }
  163. }
  164. }
  165. }