Create application/directory for subdir, if metadata or permissions are applied.
[pithos-ios] / Classes / UploadGenericFileViewController.m
index 44dce32..1952324 100755 (executable)
 #pragma mark Save Button
 
 - (void)saveButtonPressed:(id)sender {
+    [nameTextField resignFirstResponder];
+    NSString *objectName;
+    if (nameTextField.text && ![nameTextField.text isEqualToString:@""]) {
+        objectName = [NSString stringWithFormat:@"%@%@", nameTextField.text, format];
+    } else {
+        objectName = [NSString stringWithFormat:@"%@%@", nameTextField.placeholder, format];
+    }
+    
+    BOOL objectNameHasTrailingSlash = [objectName hasSuffix:@"/"];
+    NSUInteger indexOfFirstSlash = [objectName rangeOfString:@"/"].location;
+    
+    if (!objectNameHasTrailingSlash && indexOfFirstSlash != NSNotFound || 
+        objectNameHasTrailingSlash && indexOfFirstSlash < objectName.length - 1) {
+        [self alert:@"Invalid folder name" message:@"'/' characters are only allowed at the end of folder names"];
+        return;
+    }
+
+    if ([folderViewController.folder.objects objectForKey:objectName] && !allowOverwrite) {
+        NSString *alertMessage = [NSString stringWithFormat:@"An object with path '%@' in the container '%@' already exists, do you want to replace it?",objectName, folderViewController.container.name];
+        NSString *alertTitle = @"Apply changes";
+        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:alertTitle
+                                                        message:alertMessage
+                                                       delegate:self
+                                              cancelButtonTitle:@"Cancel"
+                                              otherButtonTitles:@"OK", nil];
+        [alert show];
+        return;
+    }
+
     activityIndicatorView = [[ActivityIndicatorView alloc] initWithFrame:[ActivityIndicatorView frameForText:@"Uploading..." withProgress:YES] text:@"Uploading..." withProgress:YES];
     [activityIndicatorView addToView:self.view];
     
     StorageObject *object = [[StorageObject alloc] init];
-    
-    if (nameTextField.text && ![nameTextField.text isEqualToString:@""]) {
-        object.name = [NSString stringWithFormat:@"%@%@", nameTextField.text, format];
-    } else {
-        object.name = [NSString stringWithFormat:@"%@%@", nameTextField.placeholder, format];
-    }
+    object.name = objectName;
     object.fullPath = [NSString stringWithFormat:@"%@/%@", [folder fullPath], object.name];
     object.fullPath = [object.fullPath substringFromIndex:1];
     object.contentType = contentTypeTextField.text;
                            object.sharing = folder.sharing;
                            [folder.objects setObject:object forKey:object.name];
                            [folderViewController.tableView reloadData];
+                           allowOverwrite = NO;
                            [self dismissModalViewControllerAnimated:YES];
                            [[NSNotificationCenter defaultCenter] removeObserver:successObserver];
                        }];
     
     failureObserver = [[NSNotificationCenter defaultCenter] addObserverForName:@"writeObjectFailed" object:object queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* notification) 
                        {
+                           allowOverwrite = NO;
                            [activityIndicatorView removeFromSuperviewAndRelease];
                            [self alert:@"There was a problem uploading the file." request:[notification.userInfo objectForKey:@"request"]];
                            [[NSNotificationCenter defaultCenter] removeObserver:failureObserver];
                        }];
 }
 
+#pragma mark - Alertview delegate
+
+- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
+{
+    if (buttonIndex == 1) {
+        allowOverwrite = YES;
+        [self saveButtonPressed:nil];
+    } 
+}
+
+
 #pragma mark -
 #pragma mark Text Field Delegate