From fbdeda97dde97fdfdc0c57ac93eec9f9e07b1699 Mon Sep 17 00:00:00 2001 From: Felix Schwarz Date: Thu, 22 Nov 2018 15:27:41 +0100 Subject: [PATCH] - Added detection of name conflicts with existing items for creating folders - Addresses issue where folders couldn't be duplicated in the Files app --- ios-sdk | 2 +- .../FileProviderExtension.m | 69 ++++++++++++++----- 2 files changed, 52 insertions(+), 19 deletions(-) diff --git a/ios-sdk b/ios-sdk index eaf8fb65c..b31f81384 160000 --- a/ios-sdk +++ b/ios-sdk @@ -1 +1 @@ -Subproject commit eaf8fb65c563fee8f61b7b8f278742e63fad3234 +Subproject commit b31f813843203fbaf1db5928a553e7989d0db923 diff --git a/ownCloud File Provider/FileProviderExtension.m b/ownCloud File Provider/FileProviderExtension.m index 082ad846c..a91c9be2a 100644 --- a/ownCloud File Provider/FileProviderExtension.m +++ b/ownCloud File Provider/FileProviderExtension.m @@ -302,6 +302,28 @@ - (void)stopProvidingItemAtURL:(NSURL *)url // } } +#pragma mark - Helpers +- (OCItem *)findKnownExistingItemInParent:(OCItem *)parentItem withName:(NSString *)name +{ + OCPath parentPath; + __block OCItem *existingItem = nil; + + if (((parentPath = parentItem.path) != nil) && (name != nil)) + { + OCPath destinationPath = [parentPath stringByAppendingPathComponent:name]; + + OCSyncExec(retrieveExistingItem, { + [self.core.vault.database retrieveCacheItemsAtPath:destinationPath itemOnly:YES completionHandler:^(OCDatabase *db, NSError *error, OCSyncAnchor syncAnchor, NSArray *items) { + existingItem = items.firstObject; + OCSyncExecDone(retrieveExistingItem); + }]; + }); + } + + return (existingItem); +} + + #pragma mark - Actions // ### Apple template comments: ### @@ -320,7 +342,31 @@ - (void)createDirectoryWithName:(NSString *)directoryName inParentItemIdentifier if ((parentItem = (OCItem *)[self itemForIdentifier:parentItemIdentifier error:&error]) != nil) { + // Detect collission with existing items + OCItem *existingItem; + + if ((existingItem = [self findKnownExistingItemInParent:parentItem withName:directoryName]) != nil) + { + completionHandler(nil, [NSError fileProviderErrorForCollisionWithItem:existingItem]); + return; + } + [self.core createFolder:directoryName inside:parentItem options:nil resultHandler:^(NSError *error, OCCore *core, OCItem *item, id parameter) { + if (error != nil) + { + if (error.HTTPStatus.code == OCHTTPStatusCodeMETHOD_NOT_ALLOWED) + { + // Folder already exists on the server + OCItem *existingItem; + + if ((existingItem = [self findKnownExistingItemInParent:parentItem withName:directoryName]) != nil) + { + completionHandler(nil, [NSError fileProviderErrorForCollisionWithItem:existingItem]); + return; + } + } + + } completionHandler(item, error); }]; } @@ -427,26 +473,13 @@ - (void)importDocumentAtURL:(NSURL *)fileURL toParentItemIdentifier:(NSFileProvi if ((parentItem = (OCItem *)[self itemForIdentifier:parentItemIdentifier error:&error]) != nil) { // Detect name collissions - OCPath parentPath; + OCItem *existingItem; - if (((parentPath = parentItem.path) != nil) && (importFileName != nil)) + if ((existingItem = [self findKnownExistingItemInParent:parentItem withName:importFileName]) != nil) { - OCPath destinationPath = [parentPath stringByAppendingPathComponent:importFileName]; - __block OCItem *existingItem = nil; - - OCSyncExec(retrieveExistingItem, { - [self.core.vault.database retrieveCacheItemsAtPath:destinationPath itemOnly:YES completionHandler:^(OCDatabase *db, NSError *error, OCSyncAnchor syncAnchor, NSArray *items) { - existingItem = items.firstObject; - OCSyncExecDone(retrieveExistingItem); - }]; - }); - - if (existingItem != nil) - { - // Return collission error - completionHandler(nil, [NSError fileProviderErrorForCollisionWithItem:existingItem]); - return; - } + // Return collission error + completionHandler(nil, [NSError fileProviderErrorForCollisionWithItem:existingItem]); + return; } // Start import