Browse Source

#1955

Signed-off-by: Marino Faggiana <marino@marinofaggiana.com>
Marino Faggiana 2 years ago
parent
commit
38be50e7e2

+ 5 - 1
iOSClient/Data/NCManageDatabase+Metadata.swift

@@ -393,13 +393,17 @@ extension NCManageDatabase {
         return (metadatasUpdate, metadatasLocalUpdate, metadatasDelete)
     }
 
-    func setMetadataSession(ocId: String, session: String? = nil, sessionError: String? = nil, sessionSelector: String? = nil, sessionTaskIdentifier: Int? = nil, status: Int? = nil, etag: String? = nil) {
+    func setMetadataSession(ocId: String, newFileName: String? = nil, session: String? = nil, sessionError: String? = nil, sessionSelector: String? = nil, sessionTaskIdentifier: Int? = nil, status: Int? = nil, etag: String? = nil) {
 
         let realm = try! Realm()
 
         do {
             try realm.write {
                 let result = realm.objects(tableMetadata.self).filter("ocId == %@", ocId).first
+                if let newFileName = newFileName {
+                    result?.fileName = newFileName
+                    result?.fileNameView = newFileName
+                }
                 if let session = session {
                     result?.session = session
                 }

+ 26 - 0
iOSClient/Networking/NCNetworking.swift

@@ -494,6 +494,10 @@ import Photos
     }
 
     func uploadComplete(fileName: String, serverUrl: String, ocId: String?, etag: String?, date: NSDate?, size: Int64, description: String?, task: URLSessionTask, error: NKError) {
+        var isApplicationStateActive = false
+        #if !EXTENSION
+        isApplicationStateActive = UIApplication.shared.applicationState == .active
+        #endif
         DispatchQueue.global().async {
             guard self.delegate == nil, let metadata = NCManageDatabase.shared.getMetadataFromOcId(description) else {
                 self.delegate?.uploadComplete?(fileName: fileName, serverUrl: serverUrl, ocId: ocId, etag: etag, date: date, size: size, description: description, task: task, error: error)
@@ -545,6 +549,28 @@ import Photos
                     NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
                     NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterUploadCancelFile, userInfo: ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account])
 
+                } else if error.errorCode == NCGlobal.shared.errorForbidden && isApplicationStateActive {
+
+                    DispatchQueue.main.async {
+                        let newFileName = NCUtilityFileSystem.shared.createFileName(metadata.fileName, serverUrl: metadata.serverUrl, account: metadata.account)
+                        let alertController = UIAlertController(title: error.errorDescription, message: NSLocalizedString("_change_upload_filename_", comment: ""), preferredStyle: .alert)
+                        alertController.addAction(UIAlertAction(title: String(format: NSLocalizedString("_save_file_as_", comment: ""), newFileName), style: .default, handler: { action in
+                            let atpath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + metadata.fileName
+                            let toPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId) + "/" + newFileName
+                            NCUtilityFileSystem.shared.moveFile(atPath: atpath, toPath: toPath)
+                            NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, newFileName: newFileName, session: nil, sessionError: "", sessionTaskIdentifier: 0, status: NCGlobal.shared.metadataStatusWaitUpload)
+                        }))
+                        alertController.addAction(UIAlertAction(title: NSLocalizedString("_discard_changes_", comment: ""), style: .destructive, handler: { action in
+                            CCUtility.removeFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId))
+                            NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
+                            NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterUploadCancelFile, userInfo: ["ocId": metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account])
+                        }))
+                        #if !EXTENSION
+                        let appDelegate = UIApplication.shared.delegate as! AppDelegate
+                        appDelegate.window?.rootViewController?.present(alertController, animated: true)
+                        #endif
+                    }
+
                 } else {
                     
                     NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, session: nil, sessionError: error.errorDescription, sessionTaskIdentifier: 0, status: NCGlobal.shared.metadataStatusUploadError)

+ 2 - 0
iOSClient/Supporting Files/en.lproj/Localizable.strings

@@ -906,6 +906,8 @@
 "_status_in_progress_"      = "Status reading in progress …";
 "_status_e2ee_on_server_"   = "The End-toEnd encryption is already configured in the server but not yet enabled on this device";
 "_status_e2ee_not_setup_"   = "The End-toEnd encryption is not yet configured on the server";
+"_change_upload_filename_"  = "Do you want to save the file with a different name ?";
+"_save_file_as_"            = "Save file as %@";
 
 // Video
 "_select_trace_"            = "Select the trace";