@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;
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]];
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];
}
}
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];
}
}
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;
#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]);
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:@"/"];