-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAFHTTPFileUpdateOperation.m
More file actions
162 lines (138 loc) · 4 KB
/
AFHTTPFileUpdateOperation.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>
#define HTTP_NOT_MODIFIED_CODE 304
@implementation AFHTTPFileUpdateOperation
- (NSString *) formatDate:(NSDate *)date
{
static dispatch_once_t onceToken;
static NSDateFormatter * formatter = nil;
dispatch_once(&onceToken, ^{
formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"EEE, dd MMM yyyy HH:mm:ss 'GMT'";
formatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];
formatter.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
});
return [formatter stringFromDate:date];
}
- (dispatch_queue_t) processingQueue
{
static dispatch_once_t onceToken;
static dispatch_queue_t queue;
dispatch_once(&onceToken, ^{
queue = dispatch_queue_create("com.alamofire.networking.local-file-update.processing", 0);
});
return queue;
}
- (instancetype) initWithRequest:(NSURLRequest *)urlRequest localPath:(NSString *)path
{
NSFileManager * mgr = [NSFileManager defaultManager];
BOOL dir = NO;
// needToDownload = file didn't exists and is not a folder
BOOL needToDownload = ! [mgr fileExistsAtPath:path isDirectory:&dir] && ! dir;
if ( needToDownload )
{
self = [super initWithRequest:urlRequest];
}
else if ( 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:[self formatDate:time] forHTTPHeaderField:@"If-Modified-Since"];
self = [super initWithRequest:request];
}
if ( self )
{
_localPath = path;
}
return self;
}
- (instancetype) initWithRequest:(NSURLRequest *)urlRequest
{
NSLog(@"Don't use %s in class %@, instead use initWithRequest:localPath: !", __PRETTY_FUNCTION__, [[self class] description]);
return nil;
}
+ (NSSet *) acceptableContentTypes
{
return nil;
}
+ (BOOL) canProcessRequest:(NSURLRequest *)request
{
return YES;
}
+ (NSIndexSet *) acceptableStatusCodes
{
NSMutableIndexSet * set = [super acceptableStatusCodes].mutableCopy;
[set addIndex:HTTP_NOT_MODIFIED_CODE];
return set;
}
- (void) setCompletionBlockWithSuccess:(void (^)(AFHTTPFileUpdateOperation *operation, NSData * data))success failure:(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 = [NSData dataWithContentsOfFile:_self.localPath options:NSDataReadingMappedIfSafe error:nil];
dispatch_async( _self.successCallbackQueue ?: dispatch_get_main_queue(), ^{
success(_self, data);
});
}
}
else if (_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