Skip to content

Commit

Permalink
- Added detection of name conflicts with existing items for creating …
Browse files Browse the repository at this point in the history
…folders

- Addresses issue where folders couldn't be duplicated in the Files app
  • Loading branch information
felix-schwarz committed Nov 22, 2018
1 parent 1989d48 commit fbdeda9
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 19 deletions.
69 changes: 51 additions & 18 deletions ownCloud File Provider/FileProviderExtension.m
Original file line number Diff line number Diff line change
Expand Up @@ -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<OCItem *> *items) {
existingItem = items.firstObject;
OCSyncExecDone(retrieveExistingItem);
}];
});
}

return (existingItem);
}


#pragma mark - Actions

// ### Apple template comments: ###
Expand All @@ -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);
}];
}
Expand Down Expand Up @@ -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<OCItem *> *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
Expand Down

0 comments on commit fbdeda9

Please sign in to comment.