- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAFHTTPFileUpdateOperation.m
More file actions
Latest commit
162 lines (138 loc) · 4 KB
/
Copy pathAFHTTPFileUpdateOperation.m
File metadata and controls
162 lines (138 loc) · 4 KB
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
//
// AFHTTPFileUpdateOperation.m
//
// Created by Roman Kříž on 19.07.13.
// Copyright (c) 2013 samnung. All rights reserved.
//
#import"AFHTTPFileUpdateOperation.h"
// WEAK()
#import<SAMWeak/SAMWeak.h>
#defineHTTP_NOT_MODIFIED_CODE304
@implementationAFHTTPFileUpdateOperation
- (NSString *) formatDate:(NSDate *)date
{
staticdispatch_once_t onceToken;
staticNSDateFormatter * formatter = nil;
dispatch_once(&onceToken, ^{
formatter = [[NSDateFormatteralloc] init];
formatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss 'GMT'";
formatter.locale = [[NSLocalealloc] initWithLocaleIdentifier:@"en_US"];
formatter.timeZone = [NSTimeZonetimeZoneForSecondsFromGMT:0];
});
return [formatter stringFromDate:date];
}
- (dispatch_queue_t) processingQueue
{
staticdispatch_once_t onceToken;
staticdispatch_queue_t queue;
dispatch_once(&onceToken, ^{
queue = dispatch_queue_create("com.alamofire.networking.local-file-update.processing", 0);
});
return queue;
}
- (instancetype) initWithRequest:(NSURLRequest *)urlRequestlocalPath:(NSString *)path
{
NSFileManager * mgr = [NSFileManagerdefaultManager];
BOOL dir = NO;
// needToDownload = file didn't exists and is not a folder
BOOL needToDownload = ! [mgr fileExistsAtPath:path isDirectory:&dir] && ! dir;
if ( needToDownload )
{
self = [superinitWithRequest:urlRequest];
}
elseif ( dir )
{
NSLog(@"Cannot update file at: `%@', is a folder!", path);
self = nil;
}
else
{
// --- getting info about file
NSDictionary * info = [mgr attributesOfItemAtPath:path error:nil];
NSDate * time = info[NSFileModificationDate];
// --- add If-Modified-Since header to request
NSMutableURLRequest * request = urlRequest.mutableCopy;
[request setValue:[selfformatDate:time] forHTTPHeaderField:@"If-Modified-Since"];
self = [superinitWithRequest:request];
}
if ( self )
{
_localPath = path;
}
return self;
}
- (instancetype) initWithRequest:(NSURLRequest *)urlRequest
{
NSLog(@"Don't use %s in class %@, instead use initWithRequest:localPath: !", __PRETTY_FUNCTION__, [[selfclass] description]);
returnnil;
}
+ (NSSet *) acceptableContentTypes
{
returnnil;
}
+ (BOOL) canProcessRequest:(NSURLRequest *)request
{
returnYES;
}
+ (NSIndexSet *) acceptableStatusCodes
{
NSMutableIndexSet * set = [superacceptableStatusCodes].mutableCopy;
[set addIndex:HTTP_NOT_MODIFIED_CODE];
return set;
}
- (void) setCompletionBlockWithSuccess:(void (^)(AFHTTPFileUpdateOperation *operation, NSData * data))successfailure:(void (^)(AFHTTPFileUpdateOperation *operation, NSError *error))failure
{
WEAK(self);
self.completionBlock = ^
{
if ( [_self isCancelled] )
return;
// not modified
if ( _self.response.statusCode == HTTP_NOT_MODIFIED_CODE )
{
if ( success )
{
NSData * data = [NSDatadataWithContentsOfFile:_self.localPath options:NSDataReadingMappedIfSafe error:nil];
dispatch_async( _self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
success(_self, data);
});
}
}
elseif (_self.error)
{
if (failure)
{
dispatch_async(_self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
failure(_self, _self.error);
});
}
}
else
{
dispatch_async([_self processingQueue], ^
{
NSData * data = _self.responseData;
NSError * error = nil;
if ( ! [data writeToFile:_self.localPath options:NSDataWritingAtomic error:&error] )
{
if ( failure )
{
dispatch_async( _self.failureCallbackQueue ?: dispatch_get_main_queue(), ^{
failure(_self, error);
});
}
}
else
{
if ( success )
{
dispatch_async( _self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
success(_self, data);
});
}
}
});
}
};
}
@end