-
Notifications
You must be signed in to change notification settings - Fork 6
/
MVFileUploadManager.m
226 lines (200 loc) · 7.76 KB
/
MVFileUploadManager.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#import "MVFileUploadManager.h"
#import "MVUploadAuthorization.h"
#import "MVFileUpload.h"
#import "MVCloudAppFileUpload.h"
#import "MVAsset.h"
#import "MVAsset_Private.h"
#import "MVAssetsManager.h"
#import "MVAssetsManager_Private.h"
#import "MVFileUploadManager_Private.h"
#import "NSString+UUID.h"
@interface MVFileUploadManager () <MVFileUploadDelegate>
@property (strong, readwrite) NSOperationQueue *operationQueue;
@property (strong, readwrite) NSMutableArray *fileUploads;
@property (strong, readwrite) NSMutableDictionary *fileUploadsByRemoteURL;
@property (strong, readwrite) NSMutableArray *pendingNonExpiredAuthFileUploads;
@property (strong, readwrite) NSMutableDictionary *assetsByFileUpload;
- (MVAsset*)uploadFileWithFullKey:(NSString *)fullKey data:(NSData *)data;
@end
@implementation MVFileUploadManager
@synthesize uploadAuthorization = uploadAuthorization_,
assetsManager = assetsManager_,
operationQueue = operationQueue_,
fileUploads = fileUploads_,
fileUploadsByRemoteURL = fileUploadsByRemoteURL_,
pendingNonExpiredAuthFileUploads = pendingNonExpiredAuthFileUploads_,
assetsByFileUpload = assetsByFileUpload_,
delegate = delegate_;
- (id)initWithAssetsManager:(MVAssetsManager*)assetsManager
{
self = [super init];
if(self)
{
assetsManager_ = assetsManager;
operationQueue_ = [[NSOperationQueue alloc] init];
fileUploads_ = [NSMutableArray array];
fileUploadsByRemoteURL_ = [NSMutableDictionary dictionary];
pendingNonExpiredAuthFileUploads_ = [NSMutableArray array];
assetsByFileUpload_ = [NSMutableDictionary dictionary];
delegate_ = nil;
}
return self;
}
- (MVAsset*)uploadFileWithKey:(NSString*)key
data:(NSData*)data
{
NSString *fullKey = key;
if([self.uploadAuthorization.service isEqualToString:kMVUploadAuthorizationServiceS3])
{
NSString *uniqueID = [NSString mv_generateUUID];
fullKey = [NSString stringWithFormat:@"%@%@/%@",
self.uploadAuthorization.startsWith,
uniqueID,
key];
}
return [self uploadFileWithFullKey:fullKey data:data];
}
- (MVAsset*)uploadAvatar:(NSData*)data
{
NSString *uniqueID = [NSString mv_generateUUID];
NSString *fullKey = [NSString stringWithFormat:@"%@avatars/%@.png",
self.uploadAuthorization.startsWith,
uniqueID];
return [self uploadFileWithFullKey:fullKey data:data];
}
#pragma mark -
#pragma mark MVFileUploadDelegate Methods
- (BOOL)fileUploadShouldStart:(MVFileUpload*)fileUpload
{
if(fileUpload.uploadAuthorization != self.uploadAuthorization)
fileUpload.uploadAuthorization = self.uploadAuthorization;
if(fileUpload.uploadAuthorization.isExpired)
{
if(![self.pendingNonExpiredAuthFileUploads containsObject:fileUpload])
[self.pendingNonExpiredAuthFileUploads addObject:fileUpload];
if([self.delegate respondsToSelector:@selector(fileUploadManager:
uploadAuthorizationDidExpired:)])
[self.delegate fileUploadManager:self
uploadAuthorizationDidExpired:fileUpload.uploadAuthorization];
return NO;
}
return YES;
}
- (void)fileUploadDidStart:(MVFileUpload *)fileUpload
{
}
- (void)fileUpload:(MVFileUpload *)fileUpload didProgress:(float)percent
{
}
- (void)fileUploadDidFinish:(MVFileUpload *)fileUpload
{
[self.fileUploads removeObject:fileUpload];
NSObject *key;
for(key in self.fileUploadsByRemoteURL.allKeys)
{
if([self.fileUploadsByRemoteURL objectForKey:key] == fileUpload)
{
[self.fileUploadsByRemoteURL removeObjectForKey:key];
break;
}
}
[self.assetsByFileUpload removeObjectForKey:fileUpload.key];
}
- (void)fileUpload:(MVFileUpload *)fileUpload didFailWithError:(NSError *)error
{
}
#pragma mark -
#pragma mark Private Methods
- (MVAsset*)uploadFileWithFullKey:(NSString *)fullKey data:(NSData *)data
{
NSString *remoteURLString;
MVFileUpload *fileUpload;
if([self.uploadAuthorization.service isEqualToString:kMVUploadAuthorizationServiceS3])
{
remoteURLString = [NSString stringWithFormat:@"%@%@",
self.uploadAuthorization.uploadURL,
fullKey];
fileUpload = [[MVFileUpload alloc] initWithKey:fullKey
data:data
operationQueue:self.operationQueue
uploadAuthorization:self.uploadAuthorization];
}
else
{
remoteURLString = [NSString stringWithFormat:@"cloudapp://example.com/%u/%@",
arc4random(),
fullKey];
fileUpload = [[MVCloudAppFileUpload alloc] initWithKey:fullKey
data:data
operationQueue:self.operationQueue
uploadAuthorization:self.uploadAuthorization];
}
remoteURLString = [remoteURLString stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSURL *remoteURL = [NSURL URLWithString:remoteURLString];
MVAsset *asset = [[MVAsset alloc] initWithRemoteURL:remoteURL
assetsManager:self.assetsManager];
NSURL *localURL = asset.localURL;
NSString *destinationDirectory = [[localURL path] stringByDeletingLastPathComponent];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createDirectoryAtPath:destinationDirectory
withIntermediateDirectories:YES
attributes:nil
error:nil];
[data writeToURL:localURL atomically:YES];
asset.fileUpload = fileUpload;
fileUpload.delegate = self;
[self.fileUploads addObject:fileUpload];
[self.fileUploadsByRemoteURL setObject:fileUpload forKey:remoteURL];
[self.assetsByFileUpload setObject:[NSArray arrayWithObject:asset] forKey:fileUpload.key];
[fileUpload start];
return asset;
}
- (MVFileUpload*)fileUploadWithRemoteURL:(NSURL*)remoteURL
{
return [self.fileUploadsByRemoteURL objectForKey:remoteURL];
}
- (void)addAsset:(MVAsset*)asset usingFileUpload:(MVFileUpload*)fileUpload
{
NSArray *array = [self.assetsByFileUpload objectForKey:fileUpload.key];
if(!array)
array = [NSArray array];
array = [array arrayByAddingObject:asset];
[self.assetsByFileUpload setObject:array forKey:fileUpload.key];
}
- (void)retryUpload:(MVAsset *)asset
{
MVFileUpload *fileUpload = [self.fileUploadsByRemoteURL objectForKey:asset.remoteURL];
if(!fileUpload)
return;
MVFileUpload *newFileUpload = [[MVFileUpload alloc] initWithKey:fileUpload.key
data:fileUpload.data
operationQueue:self.operationQueue
uploadAuthorization:self.uploadAuthorization];
NSArray *assets = [self.assetsByFileUpload objectForKey:fileUpload.key];
if(assets)
{
MVAsset *assetUsingFileUpload;
for(assetUsingFileUpload in assets)
{
assetUsingFileUpload.fileUpload = newFileUpload;
}
}
newFileUpload.delegate = self;
[self.fileUploads removeObject:fileUpload];
[self.assetsByFileUpload removeObjectForKey:fileUpload.key];
[self.fileUploads addObject:newFileUpload];
[self.fileUploadsByRemoteURL setObject:newFileUpload forKey:asset.remoteURL];
[self.assetsByFileUpload setObject:assets forKey:newFileUpload.key];
[newFileUpload start];
}
- (void)uploadPendingFiles
{
MVFileUpload *fileUpload;
for(fileUpload in self.pendingNonExpiredAuthFileUploads)
{
[fileUpload start];
}
[self.pendingNonExpiredAuthFileUploads removeAllObjects];
}
@end