Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
407 lines (362 loc) · 17.2 KB

File metadata and controls

executable file
·
407 lines (362 loc) · 17.2 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
//
// Coding_FileManager.m
// Coding_iOS
//
// Created by Ease on 14/11/18.
// Copyright (c) 2014年 Coding. All rights reserved.
//
#import "Coding_FileManager.h"
@interface Coding_FileManager ()<DirectoryWatcherDelegate>
@property (nonatomic, strong) DirectoryWatcher *docDownloadWatcher, *docUploadWatcher;
@property (nonatomic, strong) NSMutableDictionary *downloadDict, *uploadDict, *diskDownloadDict, *diskUploadDict;
@property (nonatomic, strong) NSURL *downloadDirectoryURL;
@end
@implementation Coding_FileManager
+ (Coding_FileManager *)sharedManager {
static Coding_FileManager *_sharedManager = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedManager = [[Coding_FileManager alloc] init];
[_sharedManager urlForDownloadFolder];
});
return _sharedManager;
}
+ (AFURLSessionManager *)af_manager{
static AFURLSessionManager *_af_manager = nil;
static dispatch_once_t af_onceToken;
dispatch_once(&af_onceToken, ^{
_af_manager= [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
});
return _af_manager;
}
- (AFURLSessionManager *)af_manager{
return [Coding_FileManager af_manager];
}
- (instancetype)init
{
self = [super init];
if (self) {
[[self class] createFolder:[[self class] downloadPath]];
[[self class] createFolder:[[self class] uploadPath]];
_downloadDict = [[NSMutableDictionary alloc] init];
_uploadDict = [[NSMutableDictionary alloc] init];
_diskDownloadDict = [[NSMutableDictionary alloc] init];
_diskUploadDict = [[NSMutableDictionary alloc] init];
_downloadDirectoryURL = nil;
_docDownloadWatcher = [DirectoryWatcher watchFolderWithPath:[[self class] downloadPath] delegate:self];
[self directoryDidChange:_docDownloadWatcher];
_docUploadWatcher = [DirectoryWatcher watchFolderWithPath:[[self class] uploadPath] delegate:self];
[self directoryDidChange:_docUploadWatcher];
}
return self;
}
+ (NSString *)downloadPath{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *downloadPath = [documentPath stringByAppendingPathComponent:@"Coding_Download"];
return downloadPath;
}
+ (NSString *)uploadPath{
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
NSString *uploadPath = [documentPath stringByAppendingPathComponent:@"Coding_Upload"];
return uploadPath;
}
+ (BOOL)createFolder:(NSString *)path{
BOOL isDir = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:path isDirectory:&isDir];
BOOL isCreated = NO;
if (!(isDir == YES && existed == YES)){
isCreated = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}else{
isCreated = YES;
}
if (isCreated) {
[NSURL addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:path isDirectory:YES]];
}
return isCreated;
}
#pragma mark download
- (NSURL *)urlForDownloadFolder{
if (!_downloadDirectoryURL) {
if ([[self class] createFolder:[[self class] downloadPath]]) {
_downloadDirectoryURL = [NSURL fileURLWithPath:[[self class] downloadPath] isDirectory:YES];
}else{
kTipAlert(@"创建文件夹失败,无法继续下载!");
}
}
return _downloadDirectoryURL;
}
+ (NSArray *)localFileUrlList{
return [[[self sharedManager] diskDownloadDict] allValues];
}
+(NSURL *)diskDownloadUrlForKey:(NSString *)storage_key{
return [self.sharedManager.diskDownloadDict objectForKey:storage_key];
}
+ (Coding_DownloadTask *)cDownloadTaskForKey:(NSString *)storage_key{
if (!storage_key) {
return nil;
}
return [self.sharedManager.downloadDict objectForKey:storage_key];
}
+ (void)cancelCDownloadTaskForKey:(NSString *)storage_key{
if (!storage_key) {
return;
}
Coding_DownloadTask *cDownloadTack = [self.sharedManager.downloadDict objectForKey:storage_key];
if (cDownloadTack) {
[cDownloadTack cancel];
}
[self.sharedManager.downloadDict removeObjectForKey:storage_key];
}
+ (NSString *)keyStrFromResponse:(NSURLResponse *)response{
if (!response) {
return nil;
}
NSString *keyStr = response.URL.absoluteString;
keyStr = [[[[keyStr componentsSeparatedByString:@"?download"] firstObject] componentsSeparatedByString:@"/"] lastObject];
return keyStr;
}
+ (Coding_DownloadTask *)cDownloadTaskForResponse:(NSURLResponse *)response{
return [self cDownloadTaskForKey:[self keyStrFromResponse:response]];
}
+ (void)cancelCDownloadTaskForResponse:(NSURLResponse *)response{
[self cancelCDownloadTaskForKey:[self keyStrFromResponse:response]];
}
- (Coding_DownloadTask *)addDownloadTaskForObj:(id)obj completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler{
Coding_DownloadTask *cTask = nil;
if ([obj isKindOfClass:[ProjectFile class]]) {
ProjectFile *file = (ProjectFile*)obj;
cTask = [self addDownloadTaskWithPath:file.downloadPath diskFileName:file.diskFileName storage_key:file.storage_key completionHandler:completionHandler];
}else if ([obj isKindOfClass:[FileVersion class]]){
FileVersion *fileVersion = (FileVersion *)obj;
cTask = [self addDownloadTaskWithPath:fileVersion.downloadPath diskFileName:fileVersion.diskFileName storage_key:fileVersion.storage_key completionHandler:completionHandler];
}
return cTask;
}
- (Coding_DownloadTask *)addDownloadTaskWithPath:(NSString *)downloadPath
diskFileName:(NSString *)diskFileName
storage_key:(NSString *)storage_key
completionHandler:(void (^)(NSURLResponse *response, NSURL *filePath, NSError *error))completionHandler{
[MobClick event:kUmeng_Event_Request_ActionOfServer label:@"下载文件"];
NSProgress *progress;
NSURL *downloadURL = [NSURL URLWithString:downloadPath];
NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
NSURLSessionDownloadTask *downloadTask = [self.af_manager downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *downloadUrl = [[Coding_FileManager sharedManager] urlForDownloadFolder];
Coding_DownloadTask *cDownloadTask = [Coding_FileManager cDownloadTaskForResponse:response];
if (cDownloadTask) {
downloadUrl = [downloadUrl URLByAppendingPathComponent:cDownloadTask.diskFileName];
}else{
downloadUrl = [downloadUrl URLByAppendingPathComponent:[response suggestedFilename]];
}
[downloadUrl setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:nil];
DebugLog(@"download_destinationPath------\n%@", downloadUrl.absoluteString);
return downloadUrl;
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
if (error) {
[Coding_FileManager cancelCDownloadTaskForKey:storage_key];
}else{
[Coding_FileManager cancelCDownloadTaskForResponse:response];
}
if (completionHandler) {
completionHandler(response, filePath, error);
}
}];
Coding_DownloadTask *cDownloadTask = [Coding_DownloadTask cDownloadTaskWithTask:downloadTask progress:progress fileName:diskFileName];
[self.downloadDict setObject:cDownloadTask forKey:storage_key];
[downloadTask resume];
return cDownloadTask;
}
#pragma upload
+ (BOOL)writeUploadDataWithName:(NSString *)fileName andAsset:(PHAsset *)asset{
if (![self createFolder:[self uploadPath]]) {
return NO;
}
NSString *filePath = [[self uploadPath] stringByAppendingPathComponent:fileName];
[[NSFileManager defaultManager] createFileAtPath:filePath contents:nil attributes:nil];
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
if (!handle) {
return NO;
}
[handle writeData:asset.loadImageData];
return YES;
}
+ (BOOL)writeUploadDataWithName:(NSString *)fileName andImage:(UIImage *)image{
if (![self createFolder:[self uploadPath]]) {
return NO;
}
NSString *filePath = [[self uploadPath] stringByAppendingPathComponent:fileName];
return [[image dataForCodingUpload] writeToFile:filePath options:NSAtomicWrite error:nil];
}
+ (BOOL)deleteUploadDataWithName:(NSString *)fileName{
NSString *filePath = [[self uploadPath] stringByAppendingPathComponent:fileName];
NSFileManager *fm = [NSFileManager defaultManager];
if ([fm fileExistsAtPath:filePath]) {
return [fm removeItemAtPath:fileName error:nil];
}else{
return YES;
}
}
- (Coding_UploadTask *)addUploadTaskWithFileName:(NSString *)fileName projectIsPublic:(BOOL)is_public{
if (!fileName) {
return nil;
}
NSArray *fileInfos = [fileName componentsSeparatedByString:@"|||"];
if (fileInfos.count != 3) {
return nil;
}
NSString *project_id, *folder_id, *name;
project_id = fileInfos[0];
folder_id = fileInfos[1];
name = fileInfos[2];
NSString *filePath = [[[self class] uploadPath] stringByAppendingPathComponent:fileName];
NSURL *filePathUrl = [NSURL fileURLWithPath:filePath];
NSURL *uploadUrl;
if (is_public) {
uploadUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@api/project/%@/upload_public_image", [NSObject baseURLStr], project_id]];
}else{
uploadUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@api/project/%@/file/upload", [NSObject baseURLStr], project_id]];
}
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:uploadUrl.absoluteString parameters:@{@"dir": folder_id} constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileURL:filePathUrl name:@"file" fileName:name mimeType:@"image/jpeg, image/png, image/gif" error:nil];
} error:nil];
[MobClick event:kUmeng_Event_Request_ActionOfServer label:@"上传文件"];
NSProgress *progress = nil;
NSURLSessionUploadTask *uploadTask = [self.af_manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
Coding_FileManager *manager = [Coding_FileManager sharedManager];
if (!error) {
error = [manager handleResponse:responseObject];
}
response = response? response: [[NSURLResponse alloc] init];
if (error){
[NSObject showError:error];
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationUploadCompled object:manager userInfo:@{@"response" : response,
@"error" : error}];
}else if (responseObject) {
responseObject = [responseObject valueForKey:@"data"];
if ([responseObject isKindOfClass:[NSString class]]) {
//处理completionHandler
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationUploadCompled object:manager userInfo:@{@"response" : response,
@"data" : responseObject}];
}else{
ProjectFile *curFile = [NSObject objectOfClass:@"ProjectFile" fromJSON:responseObject];
NSString *block_fileName = [NSString stringWithFormat:@"%@|||%@|||%@", curFile.project_id.stringValue, curFile.parent_id.stringValue, curFile.name];
NSString *block_filePath = [[[manager class] uploadPath] stringByAppendingPathComponent:block_fileName];
//移动文件到已下载
NSString *diskFilePath = [[[manager class] downloadPath] stringByAppendingPathComponent:curFile.diskFileName];
[[NSFileManager defaultManager] moveItemAtPath:block_filePath toPath:diskFilePath error:nil];
[manager directoryDidChange:manager.docUploadWatcher];
[manager directoryDidChange:manager.docDownloadWatcher];
DebugLog(@"upload_fileName------\n%@", block_fileName);
//移除任务
[Coding_FileManager cancelCUploadTaskForFile:block_fileName hasError:(error != nil)];
//处理completionHandler
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationUploadCompled object:manager userInfo:@{@"response" : response,
@"data" : curFile}];
}
}
}];
[uploadTask resume];
Coding_UploadTask *cUploadTask = [Coding_UploadTask cUploadTaskWithTask:uploadTask progress:progress fileName:fileName];
[self.uploadDict setObject:cUploadTask forKey:fileName];
return cUploadTask;
}
+ (NSURL *)diskUploadUrlForFile:(NSString *)diskFileName{
return [self.sharedManager.diskUploadDict objectForKey:diskFileName];
}
+ (Coding_UploadTask *)cUploadTaskForFile:(NSString *)diskFileName{
return [self.sharedManager.uploadDict objectForKey:diskFileName];
}
+ (void)cancelCUploadTaskForFile:(NSString *)diskFileName hasError:(BOOL)hasError{
if (!diskFileName) {
return;
}
Coding_UploadTask *cUploadTack = [self.sharedManager.uploadDict objectForKey:diskFileName];
if (cUploadTack) {
[cUploadTack cancel];
}
[self.sharedManager.uploadDict removeObjectForKey:diskFileName];
if (!hasError) {
NSString *filePath = [[[self class] uploadPath] stringByAppendingPathComponent:diskFileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
}
}
}
+ (NSArray *)uploadFilesInProject:(NSString *)project_id andFolder:(NSString *)folder_id{
if (!project_id || !folder_id) {
return nil;
}
[self.sharedManager directoryDidChange:self.sharedManager.docUploadWatcher];
NSMutableArray *uploadFiles = [NSMutableArray array];
for (NSString *fileName in [self.sharedManager.diskUploadDict allKeys]) {
NSArray *fileInfos = [fileName componentsSeparatedByString:@"|||"];
if (fileInfos.count == 3 &&
([project_id isEqualToString:fileInfos[0]] && [folder_id isEqualToString:fileInfos[1]])) {
[uploadFiles addObject:fileName];
}
}
return uploadFiles;
}
#pragma mark DirectoryWatcherDelegate
- (void)directoryDidChange:(DirectoryWatcher *)folderWatcher{
NSMutableDictionary *diskDict;
NSString *path;
BOOL isDownload = NO;
if (folderWatcher == self.docDownloadWatcher) {
diskDict = self.diskDownloadDict;
path = [[self class] downloadPath];
isDownload = YES;
}else if (folderWatcher == self.docUploadWatcher){
diskDict = self.diskUploadDict;
path = [[self class] uploadPath];
}
[diskDict removeAllObjects];
NSArray *fileContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:NULL];
for (NSString *curFileName in [fileContents objectEnumerator]) {
NSString *filePath = [path stringByAppendingPathComponent:curFileName];
NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
BOOL isDirectory;
[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDirectory];
if (!isDirectory) {
NSString *keyStr;
if (isDownload) {//下载文件,用 storge_key 做键值
keyStr = [curFileName componentsSeparatedByString:@"|"].lastObject;
}else{
keyStr = curFileName;
}
[diskDict setObject:fileUrl forKey:keyStr];
}
}
}
@end
@implementation Coding_DownloadTask
+ (Coding_DownloadTask *)cDownloadTaskWithTask:(NSURLSessionDownloadTask *)task progress:(NSProgress *)progress fileName:(NSString *)fileName{
Coding_DownloadTask *cDownloadTask = [[Coding_DownloadTask alloc] init];
cDownloadTask.task = task;
cDownloadTask.progress = progress;
cDownloadTask.diskFileName = fileName;
return cDownloadTask;
}
- (void)cancel{
if (self.task &&
(self.task.state == NSURLSessionTaskStateRunning || self.task.state == NSURLSessionTaskStateSuspended)) {
[self.task cancel];
}
}
@end
@implementation Coding_UploadTask
+ (Coding_UploadTask *)cUploadTaskWithTask:(NSURLSessionUploadTask *)task progress:(NSProgress *)progress fileName:(NSString *)fileName{
Coding_UploadTask *cUploadTask = [[Coding_UploadTask alloc] init];
cUploadTask.task = task;
cUploadTask.progress = progress;
cUploadTask.fileName = fileName;
return cUploadTask;
}
- (void)cancel{
if (self.task &&
(self.task.state == NSURLSessionTaskStateRunning || self.task.state == NSURLSessionTaskStateSuspended)) {
[self.task cancel];
}
}
@end
Morty Proxy This is a proxified and sanitized view of the page, visit original site.