// // FKRemotefileDownload.m // FirstLink // // Created by ascii on 16/1/8. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKDownloadRemotefile.h" #import "FKDownloadRequest.h" #import "FKDownloadStore.h" #import "FLRequestHelper.h" #import "SSZipArchive.h" @interface FKDownloadRemotefile () @end @implementation FKDownloadRemotefile + (FKDownloadRemotefile *)sharedInstance { static FKDownloadRemotefile *sharedRemotefileDownloadInstance = nil; static dispatch_once_t once_token; dispatch_once(&once_token, ^{ sharedRemotefileDownloadInstance = [[self alloc] init]; }); return sharedRemotefileDownloadInstance; } + (void)asyncDownloadReactNativeSuccess:(void (^)())success failure:(void (^)(NSError *))failure { NSString *urlString = [NSString stringWithFormat:@"%@/link-site/api/config/get_react_native_file.json", [[FKServerUtil sharedInstance] apiServer]]; [[FLDataCenter sharedDataCenter] POST:urlString parameters:[FLRequestHelper commonParamaterForLogin] success:^(MSGHeader *header, id responseObject) { if ([header.code intValue] == RESPONSE_MSG_NORMAL) { [FKDownloadRemotefile parseReactNativeResponse:responseObject success:success failure:failure]; } else { failure(nil); } } failure:^(MSGHeader *header, NSError *error) { failure(error); }]; } + (void)parseReactNativeResponse:(id)response success:(void (^)())success failure:(void (^)(NSError *))failure { NSString *dirPath = [FKDownloadStore directoryPath:REACTNATIVE_DIRECTORY_NAME]; if ([response isKindOfClass:[NSDictionary class]]) { if (response[@"data"][@"file_info"] != [NSNull null]) { NSDictionary *fileInfo = response[@"data"][@"file_info"]; NSString *remoteFileURL = fileInfo[@"file_url"]; NSString *remoteFileMd5 = fileInfo[@"md5"]; NSString *localFilePath = [FKDownloadStore firstFilePathInDirectoryPath:dirPath]; if (remoteFileMd5.length > 0 && remoteFileURL.length > 0 && ![remoteFileMd5 isEqualToString:localFilePath.lastPathComponent]) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSError *error; NSURL *url = [NSURL URLWithString:remoteFileURL]; NSData *content = [NSData dataWithContentsOfURL:url options:0 error:&error]; if (!error) { [FKDownloadStore makeDirectory:REACTNATIVE_DIRECTORY_NAME]; [FKDownloadStore removeFilesInDirectoryPath:dirPath]; // save content to local path NSString *zipPath = [NSString stringWithFormat:@"%@/%@.zip", dirPath, remoteFileMd5]; [content writeToFile:zipPath options:0 error:&error]; if (!error) { [SSZipArchive unzipFileAtPath:zipPath toDestination:dirPath]; [FKDownloadStore removeFileAtPath:zipPath]; NSString *upzipFilePath = [FKDownloadStore firstFilePathInDirectoryPath:dirPath]; NSString *renameFilePath = [dirPath stringByAppendingPathComponent:remoteFileMd5]; [FKDownloadStore renameFileAtPath:upzipFilePath toPath:renameFilePath]; NSString *renamedFileMd5 = [FKDownloadStore md5OfFileAtPath:renameFilePath]; if ([remoteFileMd5 isEqualToString:renamedFileMd5]) { dispatch_async(dispatch_get_main_queue(), ^{ success(); }); } else { [FKDownloadStore removeFilesInDirectoryPath:dirPath]; dispatch_async(dispatch_get_main_queue(), ^{ failure(nil); }); } return; } else { [FKDownloadStore removeFilesInDirectoryPath:dirPath]; } } dispatch_async(dispatch_get_main_queue(), ^{ failure(nil); }); }); } else { success(); } } else { [FKDownloadStore removeFilesInDirectoryPath:dirPath]; success(); } } else { failure(nil); } } @end