Bez popisu

FKDownloadRemotefile.m 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // FKRemotefileDownload.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/1/8.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKDownloadRemotefile.h"
  9. #import "FKDownloadRequest.h"
  10. #import "FKDownloadStore.h"
  11. #import "FLRequestHelper.h"
  12. #import "SSZipArchive.h"
  13. @interface FKDownloadRemotefile ()
  14. @end
  15. @implementation FKDownloadRemotefile
  16. + (FKDownloadRemotefile *)sharedInstance {
  17. static FKDownloadRemotefile *sharedRemotefileDownloadInstance = nil;
  18. static dispatch_once_t once_token;
  19. dispatch_once(&once_token, ^{
  20. sharedRemotefileDownloadInstance = [[self alloc] init];
  21. });
  22. return sharedRemotefileDownloadInstance;
  23. }
  24. + (void)asyncDownloadReactNativeSuccess:(void (^)())success
  25. failure:(void (^)(NSError *))failure {
  26. NSString *urlString = [NSString stringWithFormat:@"%@/link-site/api/config/get_react_native_file.json", [[FKServerUtil sharedInstance] apiServer]];
  27. [[FLDataCenter sharedDataCenter] POST:urlString
  28. parameters:[FLRequestHelper commonParamaterForLogin]
  29. success:^(MSGHeader *header, id responseObject)
  30. {
  31. if ([header.code intValue] == RESPONSE_MSG_NORMAL) {
  32. [FKDownloadRemotefile parseReactNativeResponse:responseObject
  33. success:success
  34. failure:failure];
  35. } else {
  36. failure(nil);
  37. }
  38. } failure:^(MSGHeader *header, NSError *error) {
  39. failure(error);
  40. }];
  41. }
  42. + (void)parseReactNativeResponse:(id)response
  43. success:(void (^)())success
  44. failure:(void (^)(NSError *))failure {
  45. NSString *dirPath = [FKDownloadStore directoryPath:REACTNATIVE_DIRECTORY_NAME];
  46. if ([response isKindOfClass:[NSDictionary class]]) {
  47. if (response[@"data"][@"file_info"] != [NSNull null]) {
  48. NSDictionary *fileInfo = response[@"data"][@"file_info"];
  49. NSString *remoteFileURL = fileInfo[@"file_url"];
  50. NSString *remoteFileMd5 = fileInfo[@"md5"];
  51. NSString *localFilePath = [FKDownloadStore firstFilePathInDirectoryPath:dirPath];
  52. if (remoteFileMd5.length > 0 && remoteFileURL.length > 0 && ![remoteFileMd5 isEqualToString:localFilePath.lastPathComponent]) {
  53. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  54. NSError *error;
  55. NSURL *url = [NSURL URLWithString:remoteFileURL];
  56. NSData *content = [NSData dataWithContentsOfURL:url options:0 error:&error];
  57. if (!error) {
  58. [FKDownloadStore makeDirectory:REACTNATIVE_DIRECTORY_NAME];
  59. [FKDownloadStore removeFilesInDirectoryPath:dirPath];
  60. // save content to local path
  61. NSString *zipPath = [NSString stringWithFormat:@"%@/%@.zip", dirPath, remoteFileMd5];
  62. [content writeToFile:zipPath options:0 error:&error];
  63. if (!error) {
  64. [SSZipArchive unzipFileAtPath:zipPath toDestination:dirPath];
  65. [FKDownloadStore removeFileAtPath:zipPath];
  66. NSString *upzipFilePath = [FKDownloadStore firstFilePathInDirectoryPath:dirPath];
  67. NSString *renameFilePath = [dirPath stringByAppendingPathComponent:remoteFileMd5];
  68. [FKDownloadStore renameFileAtPath:upzipFilePath toPath:renameFilePath];
  69. NSString *renamedFileMd5 = [FKDownloadStore md5OfFileAtPath:renameFilePath];
  70. if ([remoteFileMd5 isEqualToString:renamedFileMd5]) {
  71. dispatch_async(dispatch_get_main_queue(), ^{
  72. success();
  73. });
  74. } else {
  75. [FKDownloadStore removeFilesInDirectoryPath:dirPath];
  76. dispatch_async(dispatch_get_main_queue(), ^{
  77. failure(nil);
  78. });
  79. }
  80. return;
  81. } else {
  82. [FKDownloadStore removeFilesInDirectoryPath:dirPath];
  83. }
  84. }
  85. dispatch_async(dispatch_get_main_queue(), ^{
  86. failure(nil);
  87. });
  88. });
  89. } else {
  90. success();
  91. }
  92. } else {
  93. [FKDownloadStore removeFilesInDirectoryPath:dirPath];
  94. success();
  95. }
  96. } else {
  97. failure(nil);
  98. }
  99. }
  100. @end