KMResourceDownload.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. //
  2. // KMResourceDownload.m
  3. // PDF Master DMG
  4. //
  5. // Created by wanjun on 2023/8/1.
  6. //
  7. #import "KMResourceDownload.h"
  8. #import <ZipArchive/ZipArchive.h>
  9. //#if __has_include(<DateTools/DateTools.h>)
  10. // #import <DateTools/DateTools.h>
  11. // #define HAS_DOCUMENTAI_FRAMEWORK 1
  12. //#else
  13. // #define HAS_DOCUMENTAI_FRAMEWORK 0
  14. //#endif
  15. #define kResourcePath [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask].firstObject.path
  16. @interface KMResourceDownload()
  17. @property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
  18. @end
  19. @implementation KMResourceDownload
  20. static KMResourceDownload *_manager;
  21. + (KMResourceDownload *)manager {
  22. static dispatch_once_t onceToken;
  23. dispatch_once(&onceToken, ^{
  24. _manager = [[KMResourceDownload alloc] init];
  25. });
  26. return _manager;
  27. }
  28. - (void)downloadFrameworkProgress:(KMResourceDownloadProgress)currentProgress result:(KMResourceDownloadResult)result {
  29. self.downloadResultBlock = result;
  30. self.progressBlock = currentProgress;
  31. if (self.downloadTask == nil) {
  32. NSString *urlString = @"http://test-pdf-pro.kdan.cn:3021/downloads/DocumentAI.bundle.zip";
  33. NSURL *url = [NSURL URLWithString:urlString];
  34. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  35. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  36. NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url];
  37. [downloadTask resume];
  38. self.downloadTask = downloadTask;
  39. } else {
  40. [self dealDownLoadResult:NO state:KMResourceDownloadStateNone];
  41. }
  42. }
  43. // 解压 framework 文件
  44. - (void)unzipFrameworkAtURL:(NSURL *)zipURL toDestination:(NSString *)destinationPath {
  45. // 解压逻辑,使用 Foundation 框架的 NSFileManager 或其他解压库
  46. NSFileManager *fileManager = [NSFileManager defaultManager];
  47. // 根据压缩文件类型选择解压方法
  48. BOOL success = NO;
  49. if ([zipURL.path.pathExtension isEqualToString:@"zip"]) {
  50. // 使用系统自带的解压方法,需要引入 libz.tbd 库
  51. success = [SSZipArchive unzipFileAtPath:zipURL.path
  52. toDestination:destinationPath];
  53. } else {
  54. // 如果是其他类型的压缩文件,可以使用第三方库,如 SSZipArchive 等进行解压
  55. // success = [SSZipArchive unzipFileAtPath:tempFilePath toDestination:destinationPath];
  56. }
  57. if (success) {
  58. NSLog(@"File unzipped successfully!");
  59. [fileManager removeItemAtPath:zipURL.path error:nil];
  60. } else {
  61. NSLog(@"Failed to unzip file.");
  62. [self dealDownLoadResult:NO state:KMResourceDownloadStateUnzipFailed];
  63. }
  64. }
  65. // 加载 framework 文件
  66. - (void)loadFramework:(NSString *)destinationPath {
  67. NSError *error = nil;
  68. NSBundle *frameworkBundle = [NSBundle bundleWithPath:kResourcePath];
  69. if (![frameworkBundle loadAndReturnError:&error]) {
  70. NSLog(@"Error loading bundle: %@", error);
  71. [self dealDownLoadResult:NO state:KMResourceDownloadStateNone];
  72. return;
  73. }
  74. // 使用 framework 中的代码和资源
  75. //#if HAS_DOCUMENTAI_FRAMEWORK
  76. // NSDate *selectedDate = [NSDate dateWithTimeIntervalSinceNow:-24*60*60*6+100];
  77. // NSString *week = [NSDate weekTimeAgoSinceDate:selectedDate];
  78. // NSLog(@"week == %@", week);
  79. //#else
  80. // // 其他处理或者警告用户没有导入 SomeFramework
  81. // NSLog(@"没有库");
  82. //#endif
  83. }
  84. - (BOOL)documentAIBundleIsExist {
  85. NSString *filePath = [kResourcePath stringByAppendingPathComponent:@"DocumentAI.bundle"];
  86. NSFileManager *fileManager = [NSFileManager defaultManager];
  87. if ([fileManager fileExistsAtPath:filePath]) {
  88. return YES;
  89. } else {
  90. return NO;
  91. }
  92. }
  93. - (void)cancelDownload {
  94. [self.downloadTask cancel];
  95. self.downloadTask = nil;
  96. self.progressBlock = nil;
  97. self.downloadResultBlock = nil;
  98. }
  99. - (void)dealDownLoadResult:(BOOL)isSuccess state:(KMResourceDownloadState)state {
  100. dispatch_async(dispatch_get_main_queue(), ^{
  101. if (self.downloadResultBlock) {
  102. self.downloadResultBlock(isSuccess, state);
  103. [self cancelDownload];
  104. }
  105. });
  106. }
  107. #pragma mark - NSURLSessionDelegate
  108. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
  109. // 计算下载进度
  110. double progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
  111. // 可以在这里更新UI或执行其他操作
  112. NSLog(@"下载进度:%f", progress);
  113. if (self.progressBlock) {
  114. self.progressBlock(progress);
  115. }
  116. }
  117. - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
  118. // Move the downloaded file to APP Contents/Frameworks folder
  119. NSFileManager *fileManager = [NSFileManager defaultManager];
  120. NSString *resourcePath = kResourcePath;
  121. NSString *destinationPath = [kResourcePath stringByAppendingPathComponent:@"DocumentAI.bundle.zip"];
  122. NSError *moveError = nil;
  123. [fileManager moveItemAtURL:location toURL:[NSURL fileURLWithPath:destinationPath] error:&moveError];
  124. if (moveError) {
  125. NSLog(@"Failed to move framework: %@", moveError);
  126. [self dealDownLoadResult:NO state:KMResourceDownloadStateMoveFailed];
  127. return;
  128. } else {
  129. NSLog(@"Framework downloaded and installed successfully!");
  130. }
  131. // 解压 framework 文件
  132. [self unzipFrameworkAtURL:[NSURL fileURLWithPath:destinationPath] toDestination:resourcePath];
  133. [self dealDownLoadResult:YES state:KMResourceDownloadStateSuccess];
  134. }
  135. @end