|
@@ -1,162 +0,0 @@
|
|
|
-//
|
|
|
-// KMResourceDownload.m
|
|
|
-// PDF Master DMG
|
|
|
-//
|
|
|
-// Created by wanjun on 2023/8/1.
|
|
|
-//
|
|
|
-
|
|
|
-#import "KMResourceDownload.h"
|
|
|
-#import <ZipArchive/ZipArchive.h>
|
|
|
-//#if __has_include(<DateTools/DateTools.h>)
|
|
|
-// #import <DateTools/DateTools.h>
|
|
|
-// #define HAS_DOCUMENTAI_FRAMEWORK 1
|
|
|
-//#else
|
|
|
-// #define HAS_DOCUMENTAI_FRAMEWORK 0
|
|
|
-//#endif
|
|
|
-
|
|
|
-#define kResourcePath [[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask].firstObject.path
|
|
|
-
|
|
|
-@interface KMResourceDownload()
|
|
|
-@property (nonatomic, strong) NSURLSessionDownloadTask *downloadTask;
|
|
|
-@end
|
|
|
-
|
|
|
-@implementation KMResourceDownload
|
|
|
-static KMResourceDownload *_manager;
|
|
|
-+ (KMResourceDownload *)manager {
|
|
|
- static dispatch_once_t onceToken;
|
|
|
- dispatch_once(&onceToken, ^{
|
|
|
- _manager = [[KMResourceDownload alloc] init];
|
|
|
- });
|
|
|
- return _manager;
|
|
|
-}
|
|
|
-
|
|
|
-- (void)downloadFrameworkProgress:(KMResourceDownloadProgress)currentProgress result:(KMResourceDownloadResult)result {
|
|
|
- self.downloadResultBlock = result;
|
|
|
- self.progressBlock = currentProgress;
|
|
|
-
|
|
|
- if (self.downloadTask == nil) {
|
|
|
-
|
|
|
- NSString *urlString = @"http://test-pdf-pro.kdan.cn:3021/downloads/DocumentAI.bundle.zip";
|
|
|
- NSURL *url = [NSURL URLWithString:urlString];
|
|
|
-
|
|
|
- NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
|
|
|
- NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
|
|
|
-
|
|
|
- NSURLSessionDownloadTask *downloadTask = [session downloadTaskWithURL:url];
|
|
|
- [downloadTask resume];
|
|
|
- self.downloadTask = downloadTask;
|
|
|
- } else {
|
|
|
- [self dealDownLoadResult:NO state:KMResourceDownloadStateNone];
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// 解压 framework 文件
|
|
|
-- (void)unzipFrameworkAtURL:(NSURL *)zipURL toDestination:(NSString *)destinationPath {
|
|
|
- // 解压逻辑,使用 Foundation 框架的 NSFileManager 或其他解压库
|
|
|
- NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
- // 根据压缩文件类型选择解压方法
|
|
|
- BOOL success = NO;
|
|
|
- if ([zipURL.path.pathExtension isEqualToString:@"zip"]) {
|
|
|
- // 使用系统自带的解压方法,需要引入 libz.tbd 库
|
|
|
- success = [SSZipArchive unzipFileAtPath:zipURL.path
|
|
|
- toDestination:destinationPath];
|
|
|
- } else {
|
|
|
- // 如果是其他类型的压缩文件,可以使用第三方库,如 SSZipArchive 等进行解压
|
|
|
- // success = [SSZipArchive unzipFileAtPath:tempFilePath toDestination:destinationPath];
|
|
|
- }
|
|
|
-
|
|
|
- if (success) {
|
|
|
- NSLog(@"File unzipped successfully!");
|
|
|
- [fileManager removeItemAtPath:zipURL.path error:nil];
|
|
|
-
|
|
|
- } else {
|
|
|
- NSLog(@"Failed to unzip file.");
|
|
|
- [self dealDownLoadResult:NO state:KMResourceDownloadStateUnzipFailed];
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-// 加载 framework 文件
|
|
|
-- (void)loadFramework:(NSString *)destinationPath {
|
|
|
- NSError *error = nil;
|
|
|
-
|
|
|
- NSBundle *frameworkBundle = [NSBundle bundleWithPath:kResourcePath];
|
|
|
- if (![frameworkBundle loadAndReturnError:&error]) {
|
|
|
- NSLog(@"Error loading bundle: %@", error);
|
|
|
- [self dealDownLoadResult:NO state:KMResourceDownloadStateNone];
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- // 使用 framework 中的代码和资源
|
|
|
-//#if HAS_DOCUMENTAI_FRAMEWORK
|
|
|
-// NSDate *selectedDate = [NSDate dateWithTimeIntervalSinceNow:-24*60*60*6+100];
|
|
|
-// NSString *week = [NSDate weekTimeAgoSinceDate:selectedDate];
|
|
|
-// NSLog(@"week == %@", week);
|
|
|
-//#else
|
|
|
-// // 其他处理或者警告用户没有导入 SomeFramework
|
|
|
-// NSLog(@"没有库");
|
|
|
-//#endif
|
|
|
-}
|
|
|
-
|
|
|
-- (BOOL)documentAIBundleIsExist {
|
|
|
- NSString *filePath = [kResourcePath stringByAppendingPathComponent:@"DocumentAI.bundle"];
|
|
|
-
|
|
|
- NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
- if ([fileManager fileExistsAtPath:filePath]) {
|
|
|
- return YES;
|
|
|
- } else {
|
|
|
- return NO;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-- (void)cancelDownload {
|
|
|
- [self.downloadTask cancel];
|
|
|
- self.downloadTask = nil;
|
|
|
- self.progressBlock = nil;
|
|
|
- self.downloadResultBlock = nil;
|
|
|
-}
|
|
|
-
|
|
|
-- (void)dealDownLoadResult:(BOOL)isSuccess state:(KMResourceDownloadState)state {
|
|
|
- dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
- if (self.downloadResultBlock) {
|
|
|
- self.downloadResultBlock(isSuccess, state);
|
|
|
- [self cancelDownload];
|
|
|
- }
|
|
|
- });
|
|
|
-}
|
|
|
-#pragma mark - NSURLSessionDelegate
|
|
|
-
|
|
|
-- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
|
|
|
- // 计算下载进度
|
|
|
- double progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
|
|
|
-
|
|
|
- // 可以在这里更新UI或执行其他操作
|
|
|
- NSLog(@"下载进度:%f", progress);
|
|
|
-
|
|
|
- if (self.progressBlock) {
|
|
|
- self.progressBlock(progress);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)location {
|
|
|
- // Move the downloaded file to APP Contents/Frameworks folder
|
|
|
- NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
- NSString *resourcePath = kResourcePath;
|
|
|
- NSString *destinationPath = [kResourcePath stringByAppendingPathComponent:@"DocumentAI.bundle.zip"];
|
|
|
-
|
|
|
- NSError *moveError = nil;
|
|
|
- [fileManager moveItemAtURL:location toURL:[NSURL fileURLWithPath:destinationPath] error:&moveError];
|
|
|
- if (moveError) {
|
|
|
- NSLog(@"Failed to move framework: %@", moveError);
|
|
|
- [self dealDownLoadResult:NO state:KMResourceDownloadStateMoveFailed];
|
|
|
- return;
|
|
|
- } else {
|
|
|
- NSLog(@"Framework downloaded and installed successfully!");
|
|
|
- }
|
|
|
-
|
|
|
- // 解压 framework 文件
|
|
|
- [self unzipFrameworkAtURL:[NSURL fileURLWithPath:destinationPath] toDestination:resourcePath];
|
|
|
-
|
|
|
- [self dealDownLoadResult:YES state:KMResourceDownloadStateSuccess];
|
|
|
-}
|
|
|
-
|
|
|
-@end
|