Only try upload of modified item in FileProviderExtension if that had its contents changed and we have a valid content url

Signed-off-by: Claudio Cambra <claudio.cambra@nextcloud.com>
This commit is contained in:
Claudio Cambra 2023-03-06 16:58:36 +01:00
parent 0f2fbf7518
commit 020c5d5104
No known key found for this signature in database
GPG key ID: C839200C384636B0

View file

@ -396,54 +396,67 @@ class FileProviderExtension: NSObject, NSFileProviderReplicatedExtension, NKComm
completionHandler(fpItem, [], false, nil) completionHandler(fpItem, [], false, nil)
} }
return Progress() if itemTemplateIsFolder {
} else if itemTemplateIsFolder { NSLog("Only handling renaming for folders. ocId: %@", item.itemIdentifier.rawValue)
return Progress()
}
}
guard !itemTemplateIsFolder else {
NSLog("System requested modification for folder with ocID %@ (%@) of something other than folder name.", item.itemIdentifier.rawValue, newServerUrlFileName) NSLog("System requested modification for folder with ocID %@ (%@) of something other than folder name.", item.itemIdentifier.rawValue, newServerUrlFileName)
completionHandler(item, [], false, nil) completionHandler(item, [], false, nil)
return Progress() return Progress()
} }
self.ncKit.upload(serverUrlFileName: newServerUrlFileName, if changedFields.contains(.contents) {
fileNameLocalPath: fileNameLocalPath, guard newContents != nil else {
requestHandler: { _ in NSLog("WARNING. Could not upload modified contents as was provided nil contents url. ocId: %@", item.itemIdentifier.rawValue)
}, taskHandler: { task in completionHandler(item, [], false, NSFileProviderError(.noSuchItem))
self.outstandingSessionTasks[newServerUrlFileName] = task return Progress()
NSFileProviderManager(for: self.domain)?.register(task, forItemWithIdentifier: item.itemIdentifier, completionHandler: { _ in })
}, progressHandler: { _ in
}) { account, ocId, etag, date, size, _, _, error in
self.outstandingSessionTasks.removeValue(forKey: newServerUrlFileName)
guard error == .success, let ocId = ocId/*, size == itemTemplate.documentSize as! Int64*/ else {
NSLog("Could not upload item with filename: %@, received error: %@", item.filename, error.errorDescription)
completionHandler(nil, [], false, NSFileProviderError(.cannotSynchronize))
return
} }
NSLog("Successfully uploaded item with identifier: %@ and filename: %@", ocId, item.filename) self.ncKit.upload(serverUrlFileName: newServerUrlFileName,
fileNameLocalPath: fileNameLocalPath,
requestHandler: { _ in
}, taskHandler: { task in
self.outstandingSessionTasks[newServerUrlFileName] = task
NSFileProviderManager(for: self.domain)?.register(task, forItemWithIdentifier: item.itemIdentifier, completionHandler: { _ in })
}, progressHandler: { _ in
let newMetadata = NextcloudItemMetadataTable() }) { account, ocId, etag, date, size, _, _, error in
newMetadata.date = (date ?? NSDate()) as Date self.outstandingSessionTasks.removeValue(forKey: newServerUrlFileName)
newMetadata.etag = etag ?? ""
newMetadata.account = account
newMetadata.fileName = item.filename
newMetadata.fileNameView = item.filename
newMetadata.ocId = ocId
newMetadata.size = size
newMetadata.contentType = item.contentType?.preferredMIMEType ?? ""
newMetadata.directory = itemTemplateIsFolder
newMetadata.serverUrl = parentItemMetadata.serverUrl
newMetadata.session = ""
newMetadata.sessionError = ""
newMetadata.sessionTaskIdentifier = 0
newMetadata.status = NextcloudItemMetadataTable.Status.normal.rawValue
dbManager.addLocalFileMetadataFromItemMetadata(newMetadata) guard error == .success, let ocId = ocId/*, size == itemTemplate.documentSize as! Int64*/ else {
dbManager.addItemMetadata(newMetadata) NSLog("Could not upload item with filename: %@, received error: %@", item.filename, error.errorDescription)
completionHandler(nil, [], false, NSFileProviderError(.cannotSynchronize))
return
}
let fpItem = FileProviderItem(metadata: newMetadata, parentItemIdentifier: parentItemIdentifier, ncKit: self.ncKit) NSLog("Successfully uploaded item with identifier: %@ and filename: %@", ocId, item.filename)
completionHandler(fpItem, [], false, nil) let newMetadata = NextcloudItemMetadataTable()
newMetadata.date = (date ?? NSDate()) as Date
newMetadata.etag = etag ?? ""
newMetadata.account = account
newMetadata.fileName = item.filename
newMetadata.fileNameView = item.filename
newMetadata.ocId = ocId
newMetadata.size = size
newMetadata.contentType = item.contentType?.preferredMIMEType ?? ""
newMetadata.directory = itemTemplateIsFolder
newMetadata.serverUrl = parentItemMetadata.serverUrl
newMetadata.session = ""
newMetadata.sessionError = ""
newMetadata.sessionTaskIdentifier = 0
newMetadata.status = NextcloudItemMetadataTable.Status.normal.rawValue
dbManager.addLocalFileMetadataFromItemMetadata(newMetadata)
dbManager.addItemMetadata(newMetadata)
let fpItem = FileProviderItem(metadata: newMetadata, parentItemIdentifier: parentItemIdentifier, ncKit: self.ncKit)
completionHandler(fpItem, [], false, nil)
}
} }
return Progress() return Progress()