-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASIS3ObjectRequest.m
152 lines (130 loc) · 4.6 KB
/
ASIS3ObjectRequest.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
//
// ASIS3ObjectRequest.m
// Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest
//
// Created by Ben Copsey on 16/03/2010.
// Copyright 2010 All-Seeing Interactive. All rights reserved.
//
#import "ASIS3ObjectRequest.h"
@implementation ASIS3ObjectRequest
- (ASIHTTPRequest *)HEADRequest
{
ASIS3ObjectRequest *headRequest = (ASIS3ObjectRequest *)[super HEADRequest];
[headRequest setKey:[self key]];
[headRequest setBucket:[self bucket]];
return headRequest;
}
+ (id)requestWithBucket:(NSString *)bucket key:(NSString *)key
{
NSString *path = [ASIS3Request stringByURLEncodingForS3Path:key];
ASIS3ObjectRequest *request = [[[self alloc] initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@.s3.amazonaws.com%@",bucket,path]]] autorelease];
[request setBucket:bucket];
[request setKey:key];
return request;
}
+ (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];
[request setSubResource:subResource];
[request setBucket:bucket];
[request setKey:key];
return request;
}
+ (id)PUTRequestForData:(NSData *)data withBucket:(NSString *)bucket key:(NSString *)key
{
ASIS3ObjectRequest *request = [self requestWithBucket:bucket key:key];
[request appendPostData:data];
[request setRequestMethod:@"PUT"];
return request;
}
+ (id)PUTRequestForFile:(NSString *)filePath withBucket:(NSString *)bucket key:(NSString *)key
{
ASIS3ObjectRequest *request = [self requestWithBucket:bucket key:key];
[request setPostBodyFilePath:filePath];
[request setShouldStreamPostDataFromDisk:YES];
[request setRequestMethod:@"PUT"];
[request setMimeType:[ASIHTTPRequest mimeTypeForFileAtPath:filePath]];
return request;
}
+ (id)DELETERequestWithBucket:(NSString *)bucket key:(NSString *)key
{
ASIS3ObjectRequest *request = [self requestWithBucket:bucket key:key];
[request setRequestMethod:@"DELETE"];
return request;
}
+ (id)COPYRequestFromBucket:(NSString *)sourceBucket key:(NSString *)sourceKey toBucket:(NSString *)bucket key:(NSString *)key
{
ASIS3ObjectRequest *request = [self requestWithBucket:bucket key:key];
[request setRequestMethod:@"PUT"];
[request setSourceBucket:sourceBucket];
[request setSourceKey:sourceKey];
return request;
}
+ (id)HEADRequestWithBucket:(NSString *)bucket key:(NSString *)key
{
ASIS3ObjectRequest *request = [self requestWithBucket:bucket key:key];
[request setRequestMethod:@"HEAD"];
return request;
}
- (id)copyWithZone:(NSZone *)zone
{
ASIS3ObjectRequest *newRequest = [super copyWithZone:zone];
[newRequest setBucket:[self bucket]];
[newRequest setKey:[self key]];
[newRequest setMimeType:[self mimeType]];
[newRequest setSourceBucket:[self sourceBucket]];
[newRequest setSourceKey:[self sourceKey]];
return newRequest;
}
- (void)dealloc
{
[bucket release];
[key release];
[mimeType release];
[sourceKey release];
[sourceBucket release];
[subResource release];
[super dealloc];
}
- (NSString *)mimeType
{
if (mimeType) {
return mimeType;
} else if ([self postBodyFilePath]) {
return [ASIHTTPRequest mimeTypeForFileAtPath:[self postBodyFilePath]];
} else {
return @"application/octet-stream";
}
}
- (NSString *)canonicalizedResource
{
if ([[self subResource] length] > 0) {
return [NSString stringWithFormat:@"/%@%@?%@",[self bucket],[ASIS3Request stringByURLEncodingForS3Path:[self key]], [self subResource]];
}
return [NSString stringWithFormat:@"/%@%@",[self bucket],[ASIS3Request stringByURLEncodingForS3Path:[self key]]];
}
- (NSMutableDictionary *)S3Headers
{
NSMutableDictionary *headers = [super S3Headers];
if ([self sourceKey]) {
NSString *path = [ASIS3Request stringByURLEncodingForS3Path:[self sourceKey]];
[headers setObject:[[self sourceBucket] stringByAppendingString:path] forKey:@"x-amz-copy-source"];
}
return headers;
}
- (NSString *)stringToSignForHeaders:(NSString *)canonicalizedAmzHeaders resource:(NSString *)canonicalizedResource
{
if ([[self requestMethod] isEqualToString:@"PUT"] && ![self sourceKey]) {
[self addRequestHeader:@"Content-Type" value:[self mimeType]];
return [NSString stringWithFormat:@"PUT\n\n%@\n%@\n%@%@",[self mimeType],dateString,canonicalizedAmzHeaders,canonicalizedResource];
}
return [super stringToSignForHeaders:canonicalizedAmzHeaders resource:canonicalizedResource];
}
@synthesize bucket;
@synthesize key;
@synthesize sourceBucket;
@synthesize sourceKey;
@synthesize mimeType;
@synthesize subResource;
@end