|
@@ -90,6 +90,7 @@ class FileProvider: NSFileProviderExtension, CCNetworkingDelegate {
|
|
}
|
|
}
|
|
|
|
|
|
override func itemChanged(at url: URL) {
|
|
override func itemChanged(at url: URL) {
|
|
|
|
+
|
|
// Called at some point after the file has changed; the provider may then trigger an upload
|
|
// Called at some point after the file has changed; the provider may then trigger an upload
|
|
|
|
|
|
let fileSize = (try! FileManager.default.attributesOfItem(atPath: url.path)[FileAttributeKey.size] as! NSNumber).uint64Value
|
|
let fileSize = (try! FileManager.default.attributesOfItem(atPath: url.path)[FileAttributeKey.size] as! NSNumber).uint64Value
|
|
@@ -99,24 +100,30 @@ class FileProvider: NSFileProviderExtension, CCNetworkingDelegate {
|
|
self.stopProvidingItem(at: url)
|
|
self.stopProvidingItem(at: url)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
-
|
|
|
|
- let directoryUser = CCUtility.getDirectoryActiveUser(account.user, activeUrl: account.url)
|
|
|
|
-
|
|
|
|
- guard let fileID = CCUtility.getFileIDExt() else {
|
|
|
|
|
|
+ guard let fileName = CCUtility.getFileNameExt() else {
|
|
|
|
+ self.stopProvidingItem(at: url)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ if (fileName != url.lastPathComponent) {
|
|
|
|
+ self.stopProvidingItem(at: url)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ guard let serverUrl = CCUtility.getServerUrlExt() else {
|
|
|
|
+ self.stopProvidingItem(at: url)
|
|
|
|
+ return
|
|
|
|
+ }
|
|
|
|
+ guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(serverUrl) else {
|
|
self.stopProvidingItem(at: url)
|
|
self.stopProvidingItem(at: url)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- let fileName = url.lastPathComponent
|
|
|
|
-
|
|
|
|
- if fileID == "NEW" {
|
|
|
|
-
|
|
|
|
- let destinationURLDirectoryUser = URL(string: "file://\(directoryUser!)/\(fileName)".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)!
|
|
|
|
- guard let serverUrl = CCUtility.getServerUrlExt() else {
|
|
|
|
- self.stopProvidingItem(at: url)
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
|
|
+ if let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileName == %@ AND directoryID == %@", fileName, directoryID)) {
|
|
|
|
|
|
|
|
+ // Update
|
|
|
|
+ let uploadID = k_uploadSessionID + CCUtility.createRandomString(16)
|
|
|
|
+ let directoryUser = CCUtility.getDirectoryActiveUser(account.user, activeUrl: account.url)
|
|
|
|
+ let destinationURLDirectoryUser = URL(string: "file://\(directoryUser!)/\(uploadID)".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)!
|
|
|
|
+
|
|
// copy sourceURL on directoryUser
|
|
// copy sourceURL on directoryUser
|
|
do {
|
|
do {
|
|
try FileManager.default.removeItem(at: destinationURLDirectoryUser)
|
|
try FileManager.default.removeItem(at: destinationURLDirectoryUser)
|
|
@@ -128,49 +135,45 @@ class FileProvider: NSFileProviderExtension, CCNetworkingDelegate {
|
|
try FileManager.default.copyItem(at: url, to: destinationURLDirectoryUser)
|
|
try FileManager.default.copyItem(at: url, to: destinationURLDirectoryUser)
|
|
} catch _ {
|
|
} catch _ {
|
|
print("file do not exists")
|
|
print("file do not exists")
|
|
|
|
+ self.stopProvidingItem(at: url)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
- CCNetworking.shared().settingDelegate(self)
|
|
|
|
- CCNetworking.shared().uploadFile(fileName, serverUrl: serverUrl, cryptated: false, onlyPlist: false, session: k_upload_session, taskStatus: Int(k_taskStatusResume), selector: nil, selectorPost: nil, errorCode: 0, delegate: self)
|
|
|
|
-
|
|
|
|
- } else {
|
|
|
|
-
|
|
|
|
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", fileID)) else {
|
|
|
|
|
|
+ if NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileName == %@ AND directoryID == %@ AND session == ''", fileName, directoryID)) != nil {
|
|
|
|
+ print("already exist in upload queue")
|
|
self.stopProvidingItem(at: url)
|
|
self.stopProvidingItem(at: url)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // Prepare for send Metadata
|
|
|
|
+ metadata.fileID = uploadID
|
|
|
|
+ metadata.sessionID = uploadID
|
|
|
|
+ metadata.session = k_upload_session
|
|
|
|
+ metadata.sessionTaskIdentifier = Int(k_taskIdentifierWaitStart)
|
|
|
|
+ _ = NCManageDatabase.sharedInstance.updateMetadata(metadata)
|
|
|
|
|
|
- if (fileName != metadata.fileName) {
|
|
|
|
- self.stopProvidingItem(at: url)
|
|
|
|
- return
|
|
|
|
- }
|
|
|
|
|
|
+ } else {
|
|
|
|
|
|
- let uploadID = k_uploadSessionID + CCUtility.createRandomString(16)
|
|
|
|
- let destinationURLDirectoryUser = URL(string: "file://\(directoryUser!)/\(uploadID)".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)!
|
|
|
|
|
|
+ // New
|
|
|
|
+ let directoryUser = CCUtility.getDirectoryActiveUser(account.user, activeUrl: account.url)
|
|
|
|
+ let destinationURLDirectoryUser = URL(string: "file://\(directoryUser!)/\(fileName)".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed)!)!
|
|
|
|
|
|
- // copy sourceURL on directoryUser
|
|
|
|
do {
|
|
do {
|
|
try FileManager.default.removeItem(at: destinationURLDirectoryUser)
|
|
try FileManager.default.removeItem(at: destinationURLDirectoryUser)
|
|
} catch _ {
|
|
} catch _ {
|
|
print("file do not exists")
|
|
print("file do not exists")
|
|
}
|
|
}
|
|
-
|
|
|
|
do {
|
|
do {
|
|
try FileManager.default.copyItem(at: url, to: destinationURLDirectoryUser)
|
|
try FileManager.default.copyItem(at: url, to: destinationURLDirectoryUser)
|
|
} catch _ {
|
|
} catch _ {
|
|
print("file do not exists")
|
|
print("file do not exists")
|
|
|
|
+ self.stopProvidingItem(at: url)
|
|
return
|
|
return
|
|
}
|
|
}
|
|
-
|
|
|
|
- // Prepare for send Metadata
|
|
|
|
- metadata.fileID = uploadID
|
|
|
|
- metadata.sessionID = uploadID
|
|
|
|
- metadata.session = k_upload_session
|
|
|
|
- metadata.sessionTaskIdentifier = Int(k_taskIdentifierWaitStart)
|
|
|
|
- _ = NCManageDatabase.sharedInstance.updateMetadata(metadata)
|
|
|
|
|
|
+
|
|
|
|
+ CCNetworking.shared().uploadFile(fileName, serverUrl: serverUrl, cryptated: false, onlyPlist: false, session: k_upload_session, taskStatus: Int(k_taskStatusResume), selector: nil, selectorPost: nil, errorCode: 0, delegate: self)
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
self.stopProvidingItem(at: url)
|
|
self.stopProvidingItem(at: url)
|
|
}
|
|
}
|
|
|
|
|