Skip to content

Commit

Permalink
Added 'folder' support and fix bugs with S3
Browse files Browse the repository at this point in the history
  • Loading branch information
tomandersen authored and pokeb committed Apr 12, 2010
1 parent def8079 commit 34b1600
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
7 changes: 7 additions & 0 deletions Classes/S3/ASIS3BucketRequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@
NSString *currentElement;
ASIS3BucketObject *currentObject;
NSMutableArray *objects;

NSMutableArray* foundFolders;
BOOL isTruncated;
}

// Fetch a bucket
Expand All @@ -57,6 +60,9 @@
// Returns an array of ASIS3BucketObjects created from the XML response
- (NSArray *)bucketObjects;

// prefixes are like folders - get them by setting a delimiter string (usually always @"/")
-(NSArray*)commonPrefixes;

//Builds a query string out of the list parameters we supplied
- (void)createQueryString;

Expand All @@ -66,4 +72,5 @@
@property (retain) NSString *marker;
@property (assign) int maxResultCount;
@property (retain) NSString *delimiter;
@property (readonly) BOOL isTruncated;
@end
42 changes: 35 additions & 7 deletions Classes/S3/ASIS3BucketRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ @interface ASIS3BucketRequest ()
@property (retain, nonatomic) NSString *currentElement;
@property (retain, nonatomic) ASIS3BucketObject *currentObject;
@property (retain, nonatomic) NSMutableArray *objects;
@property (retain, nonatomic) NSMutableArray *foundFolders;
@property (readwrite) BOOL isTruncated;
@end

@implementation ASIS3BucketRequest
Expand Down Expand Up @@ -57,6 +59,7 @@ - (void)dealloc
[currentElement release];
[currentContent release];
[objects release];
[foundFolders release];
[prefix release];
[marker release];
[delimiter release];
Expand Down Expand Up @@ -88,8 +91,12 @@ - (void)createQueryString
if ([self maxResultCount] > 0) {
[queryParts addObject:[NSString stringWithFormat:@"delimiter=%hi",[self maxResultCount]]];
}
if ([queryParts count]) {
[self setURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?%@",[[self url] absoluteString],[queryParts componentsJoinedByString:@"&"]]]];
if ([queryParts count])
{
NSString* template = @"%@?%@";
if ([self.subResource length] > 0)
template = @"%@&%@";
[self setURL:[NSURL URLWithString:[NSString stringWithFormat:template,[[self url] absoluteString],[queryParts componentsJoinedByString:@"&"]]]];
}
}

Expand All @@ -99,21 +106,36 @@ - (void)main
[super main];
}

- (NSArray *)bucketObjects
-(void)makeBucketsAndPrefixes;
{
if ([self objects]) {
return [self objects];
}
[self setObjects:[[[NSMutableArray alloc] init] autorelease]];
[self setFoundFolders:[[[NSMutableArray alloc] init] autorelease]];
NSXMLParser *parser = [[[NSXMLParser alloc] initWithData:[self responseData]] autorelease];
[parser setDelegate:self];
[parser setShouldProcessNamespaces:NO];
[parser setShouldReportNamespacePrefixes:NO];
[parser setShouldResolveExternalEntities:NO];
[parser parse];
}

- (NSArray *)bucketObjects
{
if ([self objects]) {
return [self objects];
}
[self makeBucketsAndPrefixes];
return [self objects];
}

-(NSArray*)commonPrefixes;
{
if ([self foundFolders]) {
return [self foundFolders];
}
[self makeBucketsAndPrefixes];
return [self foundFolders];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
[self setCurrentElement:elementName];
Expand Down Expand Up @@ -141,9 +163,12 @@ - (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName names
[[self currentObject] setOwnerID:[self currentContent]];
} else if ([elementName isEqualToString:@"DisplayName"]) {
[[self currentObject] setOwnerName:[self currentContent]];
} else if ([elementName isEqualToString:@"Prefix"]) {
[foundFolders addObject:[NSString stringWithString:[self currentContent]]];
} else if ([elementName isEqualToString:@"IsTruncated"]) {
self.isTruncated = [[NSString stringWithString:[self currentContent]] boolValue];
}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[self setCurrentContent:[[self currentContent] stringByAppendingString:string]];
Expand All @@ -169,8 +194,11 @@ - (id)copyWithZone:(NSZone *)zone
@synthesize currentElement;
@synthesize currentObject;
@synthesize objects;
@synthesize foundFolders;
@synthesize prefix;
@synthesize marker;
@synthesize maxResultCount;
@synthesize delimiter;
@synthesize isTruncated;

@end
2 changes: 1 addition & 1 deletion Classes/S3/ASIS3ObjectRequest.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ + (id)requestWithBucket:(NSString *)bucket key:(NSString *)key
+ (id)requestWithBucket:(NSString *)bucket key:(NSString *)key subResource:(NSString *)subResource
{
NSString *path = [ASIS3Request stringByURLEncodingForS3Path:key];
ASIS3ObjectRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com%@?",bucket,path,subResource]]] autorelease];
ASIS3ObjectRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com%@?%@",bucket,path,subResource]]] autorelease];
[request setBucket:bucket];
[request setKey:key];
return request;
Expand Down

0 comments on commit 34b1600

Please sign in to comment.