forked from this-sam/myFlock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASIS3ListRequest.m
136 lines (120 loc) · 4.51 KB
/
ASIS3ListRequest.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
//
// ASIS3ListRequest.m
// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest
//
// Created by Ben Copsey on 13/07/2009.
// Copyright 2009 All-Seeing Interactive. All rights reserved.
//
#import "ASIS3ListRequest.h"
#import "ASIS3BucketObject.h"
static NSDateFormatter *dateFormatter = nil;
// Private stuff
@interface ASIS3ListRequest ()
@property (retain, nonatomic) NSString *currentContent;
@property (retain, nonatomic) NSString *currentElement;
@property (retain, nonatomic) ASIS3BucketObject *currentObject;
@property (retain, nonatomic) NSMutableArray *objects;
@end
@implementation ASIS3ListRequest
+ (void)initialize
{
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"] autorelease]];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"UTC"]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss'.000Z'"];
}
+ (id)listRequestWithBucket:(NSString *)bucket
{
ASIS3ListRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com",bucket]]] autorelease];
[request setBucket:bucket];
return request;
}
- (void)dealloc
{
[currentObject release];
[currentElement release];
[currentContent release];
[objects release];
[prefix release];
[marker release];
[delimiter release];
[super dealloc];
}
- (void)createQueryString
{
NSMutableArray *queryParts = [[[NSMutableArray alloc] init] autorelease];
if ([self prefix]) {
[queryParts addObject:[NSString stringWithFormat:@"prefix=%@",[[self prefix] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
}
if ([self marker]) {
[queryParts addObject:[NSString stringWithFormat:@"marker=%@",[[self marker] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
}
if ([self delimiter]) {
[queryParts addObject:[NSString stringWithFormat:@"delimiter=%@",[[self delimiter] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]];
}
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:@"&"]]]];
}
}
- (void)main
{
[self createQueryString];
[super main];
}
- (NSArray *)bucketObjects
{
if ([self objects]) {
return [self objects];
}
[self setObjects:[[[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];
return [self objects];
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
[self setCurrentElement:elementName];
if ([elementName isEqualToString:@"Contents"]) {
[self setCurrentObject:[ASIS3BucketObject objectWithBucket:[self bucket]]];
}
[self setCurrentContent:@""];
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{
if ([elementName isEqualToString:@"Contents"]) {
[objects addObject:currentObject];
[self setCurrentObject:nil];
} else if ([elementName isEqualToString:@"Key"]) {
[[self currentObject] setKey:[self currentContent]];
} else if ([elementName isEqualToString:@"LastModified"]) {
[[self currentObject] setLastModified:[dateFormatter dateFromString:[self currentContent]]];
} else if ([elementName isEqualToString:@"ETag"]) {
[[self currentObject] setETag:[self currentContent]];
} else if ([elementName isEqualToString:@"Size"]) {
[[self currentObject] setSize:(unsigned long long)[[self currentContent] longLongValue]];
} else if ([elementName isEqualToString:@"ID"]) {
[[self currentObject] setOwnerID:[self currentContent]];
} else if ([elementName isEqualToString:@"DisplayName"]) {
[[self currentObject] setOwnerName:[self currentContent]];
}
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
[self setCurrentContent:[[self currentContent] stringByAppendingString:string]];
}
@synthesize currentContent;
@synthesize currentElement;
@synthesize currentObject;
@synthesize objects;
@synthesize prefix;
@synthesize marker;
@synthesize maxResultCount;
@synthesize delimiter;
@end