Drag and drop download fixes.
authorMiltiadis Vasilakis <mvasilak@gmail.com>
Wed, 14 Sep 2011 15:15:14 +0000 (18:15 +0300)
committerMiltiadis Vasilakis <mvasilak@gmail.com>
Wed, 14 Sep 2011 15:15:14 +0000 (18:15 +0300)
pithos-macos/PithosBrowserController.m
pithos-macos/PithosFileUtilities.m

index a1d4cd8..64916ba 100644 (file)
 @interface PithosBrowserController (Private) {}
 - (void)resetContainers;
 - (void)getInfo:(NSMenuItem *)sender;
+- (void)downloadObjectFinished:(ASIPithosObjectRequest *)objectRequest;
+- (void)downloadObjectFailed:(ASIPithosObjectRequest *)objectRequest;
 - (void)uploadObjectUsingHashMapFinished:(ASIPithosObjectRequest *)objectRequest;
 - (void)uploadObjectUsingHashMapFailed:(ASIPithosObjectRequest *)objectRequest;
 - (void)uploadMissingHashesFinished:(ASIPithosObjectRequest *)objectRequest;
@@ -372,7 +374,7 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column {
         PithosNode *node = [browser itemAtIndexPath:[baseIndexPath indexPathByAddingIndex:i]];
         
         // If the node is a subdir ask if the whole tree should be downloaded
-        if (node.pithosObject.subdir) {
+        if ([node class] == [PithosSubdirNode class]) {
             NSAlert *alert = [[[NSAlert alloc] init] autorelease];
             [alert setMessageText:@"Download directory"];
             [alert setInformativeText:[NSString stringWithFormat:@"'%@' is a directory, do you want to download its contents?", node.displayName]];
@@ -387,13 +389,9 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column {
                 if (objectRequests) {
                     for (ASIPithosObjectRequest *objectRequest in objectRequests) {
                         [names addObject:[objectRequest.userInfo valueForKey:@"fileName"]];
-                        // XXX set delegates and queue
-                        [objectRequest setCompletionBlock:^{
-                            NSLog(@"dl completed: %@", [objectRequest url]);
-                        }];
-                        [objectRequest setFailedBlock:^{
-                            NSLog(@"dl failed: %@, error: %@", [objectRequest url], [objectRequest error]);
-                        }];
+                        objectRequest.delegate = self;
+                        objectRequest.didFinishSelector = @selector(downloadObjectFinished:);
+                        objectRequest.didFailSelector = @selector(downloadObjectFailed:);
                         [objectRequest startAsynchronous];
                     }
                 }
@@ -405,13 +403,9 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column {
                                                                                               checkIfExists:YES];
             if (objectRequest) {
                 [names addObject:[objectRequest.userInfo valueForKey:@"fileName"]];
-                // XXX set delegates and queue
-                [objectRequest setCompletionBlock:^{
-                    NSLog(@"dl completed: %@", [objectRequest url]);
-                }];
-                [objectRequest setFailedBlock:^{
-                    NSLog(@"dl failed: %@, error: %@", [objectRequest url], [objectRequest error]);
-                }];
+                objectRequest.delegate = self;
+                objectRequest.didFinishSelector = @selector(downloadObjectFinished:);
+                objectRequest.didFailSelector = @selector(downloadObjectFailed:);
                 [objectRequest startAsynchronous];
             }
         }
@@ -480,7 +474,6 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column {
                 if (!isDirectory) {
                     // Upload file
                     NSString *objectName = [objectNamePrefix stringByAppendingPathComponent:[filePath lastPathComponent]];
-                    // XXX request
                     dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                     dispatch_async(queue, ^{
                         NSError *error = nil;
@@ -541,7 +534,34 @@ forDraggedRowsWithIndexes:(NSIndexSet *)rowIndexes inColumn:(NSInteger)column {
 #pragma mark -
 #pragma mark ASIHTTPRequestDelegate
 
-- (void)uploadObjectUsingHashMapFinished:(ASIPithosObjectRequest *)objectRequest {    
+- (void)downloadObjectFinished:(ASIPithosObjectRequest *)objectRequest {
+    NSLog(@"download completed: %@", [objectRequest url]);
+    if ([objectRequest bytes] == 0) {
+        NSLog(@"downloaded  0 bytes");
+        NSFileManager *defaultManager = [NSFileManager defaultManager];
+        NSString *filePath = [objectRequest.userInfo objectForKey:@"filePath"];
+        if (![defaultManager fileExistsAtPath:filePath]) {
+            if (![defaultManager createFileAtPath:filePath contents:nil attributes:nil]) {
+                NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+                [alert setMessageText:@"File Creation Error"];
+                [alert setInformativeText:[NSString stringWithFormat:@"Couldn't create zero length file at %@", filePath]];
+                [alert addButtonWithTitle:@"OK"];
+                [alert runModal];
+            }
+        }
+    }
+}
+
+- (void)downloadObjectFailed:(ASIPithosObjectRequest *)objectRequest {
+    NSLog(@"download failed: %@, error: %@", [objectRequest url], [objectRequest error]);
+    NSAlert *alert = [[[NSAlert alloc] init] autorelease];
+    [alert setMessageText:@"HTTP Request Error"];
+    [alert setInformativeText:[NSString stringWithFormat:@"An error occured: %@", [objectRequest error]]];
+    [alert addButtonWithTitle:@"OK"];
+    [alert runModal];
+}
+
+- (void)uploadObjectUsingHashMapFinished:(ASIPithosObjectRequest *)objectRequest {
     NSLog(@"upload using hashmap completed: %@", [objectRequest url]);
     if (objectRequest.responseStatusCode == 201) {
         NSLog(@"object created: %@", [objectRequest url]);
index 177a429..dd55d03 100644 (file)
 
     ASIPithosObjectRequest *objectRequest = [ASIPithosObjectRequest objectDataRequestWithContainerName:containerName 
                                                                                             objectName:objectName];
-    [objectRequest setDownloadDestinationPath:destinationPath];
-    [objectRequest setUserInfo:[NSDictionary dictionaryWithObjectsAndKeys:fileName, @"fileName", nil]];
+    objectRequest.downloadDestinationPath = destinationPath;
+    objectRequest.userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
+                              fileName, @"fileName", 
+                              destinationPath, @"filePath", 
+                              nil];
     return objectRequest;
 }
 
             marker = [[someObjects lastObject] name];
     } while (marker);
     
+    NSFileManager *defaultManager = [NSFileManager defaultManager];
     NSMutableArray *objectRequests = [NSMutableArray arrayWithCapacity:[objects count]];
     NSUInteger subdirPrefixLength = [objectName length];
     for (ASIPithosObject *object in objects) {
-        if (![objectName isEqualToString:object.name] && !object.subdir) {
+        if (object.subdir || [object.contentType isEqualToString:@"application/directory"]) {
+            NSString *subdirDirectoryPath = [directoryPath stringByAppendingPathComponent:subdirName];
+            subdirDirectoryPath = [subdirDirectoryPath stringByAppendingPathComponent:[object.name substringFromIndex:subdirPrefixLength]];
+            
+            BOOL directoryIsDirectory;
+            BOOL directoryExists = [defaultManager fileExistsAtPath:subdirDirectoryPath isDirectory:&directoryIsDirectory];
+            NSError *error = nil;
+            if (!directoryExists) {
+                [defaultManager createDirectoryAtPath:subdirDirectoryPath withIntermediateDirectories:YES attributes:nil error:&error];
+            } else if (!directoryIsDirectory) {
+                [defaultManager removeItemAtPath:directoryPath error:&error];
+            }
+            if (error) {
+                NSLog(@"error: %@", error);
+                // XXX do something on error, an alert maybe
+            }
+        } else {
             NSString *fileName = [object.name lastPathComponent];
             if([object.name hasSuffix:@"/"])
                 fileName = [fileName stringByAppendingString:@"/"];