-
Notifications
You must be signed in to change notification settings - Fork 17
/
MVFTPFileUpload.m
executable file
·101 lines (86 loc) · 4.15 KB
/
MVFTPFileUpload.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
//
// MVFTPFileUpload.m
// FileShuttle
//
// Created by Michael Villar on 12/10/11.
//
#import "MVFTPFileUpload.h"
#import <CURLHandle/CURLFTPSession.h>
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@interface MVFTPFileUpload ()
@property (assign) long fileSize;
@property (assign) long totalBytesWritten;
@property (strong, readwrite) CURLFTPSession *ftpSession;
@end
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
@implementation MVFTPFileUpload
@synthesize fileSize = fileSize_,
totalBytesWritten = totalBytesWritten_,
ftpSession = ftpSession_;
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark -
#pragma mark Public Methods
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)cancel
{
[self.ftpSession cancel];
}
///////////////////////////////////////////////////////////////////////////////////////////////////
- (void)start {
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:[self.source path]
error:nil];
if(attrs) {
self.fileSize = [((NSNumber*)[attrs valueForKey:NSFileSize]) longValue];
}
NSURL *destination = [NSURL URLWithString:self.destination];
NSURLRequest *ftpReq = [NSURLRequest requestWithURL:destination
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:20];
self.ftpSession = [[CURLFTPSession alloc] initWithRequest:ftpReq];
NSURLCredential *credential = [[NSURLCredential alloc] initWithUser:self.username
password:self.password
persistence:NSURLCredentialPersistenceNone];
[self.ftpSession useCredential:credential];
if([self.delegate respondsToSelector:@selector(fileUploadDidStartUpload:)])
[self.delegate fileUploadDidStartUpload:self];
__block __weak MVFTPFileUpload *weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSError *error = nil;
[weakSelf.ftpSession createFileAtPath:destination.path
withContentsOfURL:weakSelf.source
withIntermediateDirectories:YES
error:&error
progressBlock:^(NSUInteger bytesWritten)
{
weakSelf.totalBytesWritten += bytesWritten;
dispatch_async(dispatch_get_main_queue(), ^{
float progression = ((float)weakSelf.totalBytesWritten / (float)weakSelf.fileSize);
if([weakSelf.delegate respondsToSelector:
@selector(fileUpload:didChangeProgression:bytesRead:totalBytes:)]) {
[weakSelf.delegate fileUpload:weakSelf
didChangeProgression:progression
bytesRead:weakSelf.totalBytesWritten
totalBytes:weakSelf.fileSize];
}
});
}];
dispatch_async(dispatch_get_main_queue(), ^{
if(!error) {
if([weakSelf.delegate respondsToSelector:@selector(fileUploadDidSuccess:)])
[weakSelf.delegate fileUploadDidSuccess:self];
}
else {
NSLog(@"Upload Failed with error %@",error);
if([weakSelf.delegate respondsToSelector:@selector(fileUpload:didFailWithError:)])
[weakSelf.delegate fileUpload:self
didFailWithError:error.description];
}
});
});
}
@end