-
Notifications
You must be signed in to change notification settings - Fork 17
/
MVFileUploader.m
executable file
·230 lines (194 loc) · 8.44 KB
/
MVFileUploader.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
227
228
229
230
//
// MVFileUploader.m
// FileShuttle
//
// Created by Michaël on 26/04/11.
//
#import "MVFileUploader.h"
#import "AHKeychain.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@interface MVFileUploader ()
@property (retain) MVFileUpload *fileUpload;
@property (retain) NSString *filename;
@property (retain) NSString *filepath;
@property (assign) BOOL deleteFile;
@property (assign) int tries;
- (void)deleteFile:(NSURL*)fileURL;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation MVFileUploader
@synthesize delegate = delegate_,
fileUpload = fileUpload_,
filename = filename_,
filepath = filepath_,
deleteFile = deleteFile_,
tries = tries_;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Memory Management
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc {
[fileUpload_ release];
[filename_ release];
[filepath_ release];
[super dealloc];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (id)init {
self = [super init];
if(self) {
fileUpload_ = nil;
filename_ = nil;
filepath_ = nil;
deleteFile_ = NO;
tries_ = -1;
}
return self;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Private Methods
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)deleteFile:(NSURL*)fileURL
{
if(!self.deleteFile)
return;
[[NSFileManager defaultManager] removeItemAtURL:fileURL
error:nil];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public Methods
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)uploadFile:(NSString*)filepath
toFilename:(NSString*)filename
deleteFile:(BOOL)deleteFile
{
if(self.fileUpload)
return;
self.tries = 3;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSURL *source = [NSURL fileURLWithPath:filepath];
self.filepath = filepath;
self.filename = filename;
NSString *encodedFilename = [self.filename stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSMutableString *path = [NSMutableString string];
NSString *defaultPath = [defaults stringForKey:@"path"];
if([defaultPath length] == 0 || ![[defaultPath substringToIndex:1] isEqualToString:@"/"])
[path appendString:@"/"];
if(defaultPath)
[path appendString:defaultPath];
if([path length] > 0 && ![[path substringFromIndex:[path length] - 1] isEqualToString:@"/"])
[path appendString:@"/"];
[path appendString:encodedFilename];
BOOL changePermissions = [defaults boolForKey:@"change_permissions"];
NSString *permissionString = nil;
if (changePermissions) {
permissionString = [defaults stringForKey:@"permission_string"];
}
NSString *protocol = [defaults stringForKey:@"protocol"];
NSString *hostname = [defaults stringForKey:@"host"];
int port = [[defaults stringForKey:@"port"] intValue];
NSString *username = [defaults stringForKey:@"username"];
NSString *password = @"";
NSError *keychainError;
AHKeychain *keychain = [AHKeychain loginKeychain];
AHKeychainItem *keychainItem = [[AHKeychainItem alloc] init];
keychainItem.service = @"FileShuttle";
keychainItem.account = @"";
if ([keychain getItem:keychainItem error:&keychainError]) {
password = keychainItem.password;
}
MVFileUpload *tmpFileUpload = nil;
if([protocol isEqualToString:@"FTP"]) {
NSString *destination = [NSString stringWithFormat:
@"ftp://%@:%i/%@",hostname,port,path];
tmpFileUpload = [[MVFTPFileUpload alloc] initWithDestination:destination
username:username
password:password
source:source
delegate:self];
}
else if([protocol isEqualToString:@"SFTP"] || [protocol isEqualToString:@"SCP"]) {
NSString *destination = [NSString stringWithFormat:
@"ssh://%@:%i/%@",hostname,port,path];
tmpFileUpload = [[MVSCPFileUpload alloc] initWithDestination:destination
username:username
password:password
source:source
delegate:self];
tmpFileUpload.changePermissions = changePermissions;
tmpFileUpload.permissionString = permissionString;
}
self.fileUpload = tmpFileUpload;
[tmpFileUpload release];
self.deleteFile = deleteFile;
if(self.fileUpload)
[self.fileUpload start];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark MVFileUploadDelegate methods
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)fileUpload:(MVFileUpload*)fileUpload
didFailWithError:(NSString*)error
{
self.tries--;
[self.fileUpload cancel];
if(self.tries > 0) {
[self.fileUpload start];
}
else {
[self deleteFile:fileUpload.source];
if([self.delegate respondsToSelector:@selector(fileUploader:didFailWithError:)])
[self.delegate fileUploader:self
didFailWithError:error];
self.fileUpload = nil;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)fileUploadDidStartUpload:(MVFileUpload *)fileUpload
{
if([self.delegate respondsToSelector:@selector(fileUploaderDidStart:)])
[self.delegate fileUploaderDidStart:self];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)fileUploadDidSuccess:(MVFileUpload*)fileUpload
{
[self deleteFile:fileUpload.source];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *baseurl = [defaults stringForKey:@"baseurl"];
if(![[baseurl substringFromIndex:[baseurl length] - 1] isEqualToString:@"/"])
baseurl = [NSString stringWithFormat:@"%@/",baseurl];
if([baseurl length] < 7 ||
(![[[baseurl substringToIndex:7] lowercaseString] isEqualToString:@"http://"]
&& ![[[baseurl substringToIndex:8] lowercaseString] isEqualToString:@"https://"]))
baseurl = [NSString stringWithFormat:@"http://%@",baseurl];
NSString *filename = [fileUpload.destination lastPathComponent];
NSString *url = [NSString stringWithFormat:@"%@%@",baseurl,filename];
if([self.delegate respondsToSelector:@selector(fileUploader:didSuccess:fileName:filePath:)])
[self.delegate fileUploader:self
didSuccess:url
fileName:self.filename
filePath:self.filepath];
[self.fileUpload cancel];
self.fileUpload = nil;
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)fileUpload:(MVFileUpload *)fileUpload
didChangeProgression:(float)progression
bytesRead:(long)bytesRead
totalBytes:(long)totalBytes
{
if([self.delegate respondsToSelector:@selector(fileUploader:didChangeProgression:)])
[self.delegate fileUploader:self didChangeProgression:progression];
}
@end