Browse Source

new auto upload

marinofaggiana 4 years ago
parent
commit
eb9bc90d10
2 changed files with 88 additions and 5 deletions
  1. 87 5
      iOSClient/Networking/NCAutoUpload.swift
  2. 1 0
      iOSClient/Nextcloud-Bridging-Header.h

+ 87 - 5
iOSClient/Networking/NCAutoUpload.swift

@@ -33,6 +33,8 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
     
     private let appDelegate = UIApplication.shared.delegate as! AppDelegate
     public var locationManager: CLLocationManager?
+    private var hud: CCHud?
+    private var endForAssetToUpload: Bool = false
 
     // MARK: -
     
@@ -123,16 +125,96 @@ class NCAutoUpload: NSObject, CLLocationManagerDelegate {
     private func uploadAssetsNewAndFull(viewController: UIViewController?, selector: String) {
         
         if appDelegate.account == nil || appDelegate.account.count == 0 { return }
-        guard let account = NCManageDatabase.shared.getAccountActive() else { return }
-        
-        let autoUploadPath = NCManageDatabase.shared.getAccountAutoUploadPath(urlBase: appDelegate.urlBase, account: appDelegate.account)
-        let newAssetToUpload = self.getCameraRollAssets(viewController: viewController, account: account, selector: selector, alignPhotoLibrary: false) { (assets) in
+        guard let account = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", appDelegate.account)) else { return }
+        let autoUploadPath = NCManageDatabase.shared.getAccountAutoUploadPath(urlBase: account.urlBase, account: account.account)
+        var counterLivePhoto: Int = 0
+        var metadataFull: [tableMetadata] = []
+        self.getCameraRollAssets(viewController: viewController, account: account, selector: selector, alignPhotoLibrary: false) { (assets) in
             
             if assets == nil || assets?.count == 0 {
                 NCCommunicationCommon.shared.writeLog("Automatic upload, no new assets found")
                 return
             } else {
-                NCCommunicationCommon.shared.writeLog("Automatic upload, new \(assets?.count) assets found")
+                NCCommunicationCommon.shared.writeLog("Automatic upload, new \(assets?.count ?? 0) assets found")
+            }
+            guard let assets = assets else { return }
+            
+            if selector == NCBrandGlobal.shared.selectorUploadAutoUploadAll {
+                DispatchQueue.main.async {
+                    self.hud = CCHud.init(view: self.appDelegate.window.rootViewController)
+                    NCContentPresenter.shared.messageNotification("_attention_", description: "_create_full_upload_", delay: NCBrandGlobal.shared.dismissAfterSecondLong, type: .info, errorCode: 0, forced: true)
+                    self.hud?.visibleHudTitle(NSLocalizedString("_wait_", comment: ""), mode: MBProgressHUDMode.indeterminate, color: NCBrandColor.shared.brand)
+                }
+            }
+            
+            // Create the folder for auto upload & if request the subfolders
+            if NCNetworking.shared.createFolder(assets: assets, selector: selector, useSubFolder: account.autoUploadCreateSubfolder, account: account.account, urlBase: account.urlBase) {
+                if selector == NCBrandGlobal.shared.selectorUploadAutoUploadAll {
+                    DispatchQueue.main.async {
+                        NCContentPresenter.shared.messageNotification("_error_", description: "_error_createsubfolders_upload_", delay: NCBrandGlobal.shared.dismissAfterSecond, type: .error, errorCode: NCBrandGlobal.shared.ErrorInternalError, forced: true)
+                        self.hud?.hideHud()
+                    }
+                    return
+                }
+            }
+            
+            self.endForAssetToUpload = false
+            
+            for asset in assets {
+                
+                var livePhoto = false
+                var session: String = ""
+                guard let assetDate = asset.creationDate else { continue }
+                let assetMediaType = asset.mediaType
+                var formatter = DateFormatter()
+                var serverUrl: String = ""
+                
+                let fileName = CCUtility.createFileName(asset.value(forKey: "filename") as? String, fileDate: assetDate, fileType: assetMediaType, keyFileName: NCBrandGlobal.shared.keyFileNameAutoUploadMask, keyFileNameType: NCBrandGlobal.shared.keyFileNameAutoUploadType, keyFileNameOriginal: NCBrandGlobal.shared.keyFileNameOriginalAutoUpload)!
+                
+                if (asset.mediaSubtypes.rawValue == PHAssetMediaSubtype.photoLive.rawValue || asset.mediaSubtypes.rawValue == (PHAssetMediaSubtype.photoHDR.rawValue + PHAssetMediaSubtype.photoLive.rawValue)) && CCUtility.getLivePhoto() {
+                    livePhoto = true
+                }
+                
+                if selector == NCBrandGlobal.shared.selectorUploadAutoUploadAll {
+                    session = NCCommunicationCommon.shared.sessionIdentifierUpload
+                } else {
+                    if assetMediaType == PHAssetMediaType.image && account.autoUploadWWAnPhoto == false { session = NCNetworking.shared.sessionIdentifierBackground }
+                    else if assetMediaType == PHAssetMediaType.video && account.autoUploadWWAnVideo == false { session = NCNetworking.shared.sessionIdentifierBackground }
+                    else if assetMediaType == PHAssetMediaType.image && account.autoUploadWWAnPhoto { session = NCNetworking.shared.sessionIdentifierBackgroundWWan }
+                    else if assetMediaType == PHAssetMediaType.video && account.autoUploadWWAnVideo { session = NCNetworking.shared.sessionIdentifierBackgroundWWan }
+                    else { session = NCNetworking.shared.sessionIdentifierBackground }
+                }
+                
+                formatter.dateFormat = "yyyy"
+                let yearString = formatter.string(from: assetDate)
+                formatter.dateFormat = "MM"
+                let monthString = formatter.string(from: assetDate)
+                
+                if account.autoUploadCreateSubfolder {
+                    serverUrl = autoUploadPath + "/" + yearString + "/" + monthString
+                } else {
+                    serverUrl = autoUploadPath
+                }
+
+                if let metadata = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView == %@", account.account, serverUrl, fileName)) {
+                    
+                    if selector == NCBrandGlobal.shared.selectorUploadAutoUpload {
+                        NCManageDatabase.shared.addPhotoLibrary([asset], account: account.account)
+                    }
+                    
+                } else {
+                    
+                }
+            }
+            
+            self.endForAssetToUpload = true
+            
+            
+            if counterLivePhoto == 0 && selector == NCBrandGlobal.shared.selectorUploadAutoUploadAll {
+                DispatchQueue.main.async {
+                    NCManageDatabase.shared.addMetadatas(metadataFull)
+                    self.hud?.hideHud()
+                }
             }
         }
         

+ 1 - 0
iOSClient/Nextcloud-Bridging-Header.h

@@ -8,3 +8,4 @@
 #import "NYMnemonic.h"
 #import "UIImage+animatedGIF.h"
 #import "CCUtility.h"
+#import "CCHud.h"