mirror of
https://gitcode.com/gh_mirrors/re/react-native-pushy.git
synced 2025-09-17 19:46:10 +08:00
文件下载&&解压缩接口
This commit is contained in:
138
ios/RCTHotUpdate/RCTHotUpdate.m
Normal file
138
ios/RCTHotUpdate/RCTHotUpdate.m
Normal file
@@ -0,0 +1,138 @@
|
||||
//
|
||||
// RCTHotUpdate.m
|
||||
// RCTHotUpdate
|
||||
//
|
||||
// Created by LvBingru on 2/19/16.
|
||||
// Copyright © 2016 erica. All rights reserved.
|
||||
//
|
||||
|
||||
#import "RCTHotUpdate.h"
|
||||
#import "ZipArchive.h"
|
||||
#import "RCTHotUpdateDownloader.h"
|
||||
#import "RCTEventDispatcher.h"
|
||||
#import "RCTConvert.h"
|
||||
|
||||
@implementation RCTHotUpdate
|
||||
|
||||
@synthesize bridge = _bridge;
|
||||
@synthesize methodQueue = _methodQueue;
|
||||
|
||||
RCT_EXPORT_MODULE(RCTHotUpdate);
|
||||
|
||||
- (NSDictionary *)constantsToExport
|
||||
{
|
||||
return @{ @"downloadRootDir": [self constDir] };
|
||||
}
|
||||
|
||||
- (instancetype)init
|
||||
{
|
||||
self = [super init];
|
||||
if (self) {
|
||||
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(checkForUpdate:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
// dispatch_queue_t fileOpQueue = dispatch_queue_create("reactnative.cn.hotupdate", DISPATCH_QUEUE_SERIAL);
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback)
|
||||
{
|
||||
NSString *updateUrl = options[@"updateUrl"];
|
||||
NSString *unzipName = options[@"unzipName"]?:@"unzipped";
|
||||
|
||||
NSString *dir = [self getDownloadDir];
|
||||
NSString *savePath = [dir stringByAppendingPathComponent:@"zipfile"];
|
||||
|
||||
[RCTHotUpdateDownloader download:updateUrl savePath:savePath progressHandler:^(long long receivedBytes, long long totalBytes) {
|
||||
[self.bridge.eventDispatcher sendAppEventWithName:@"RCTHotUpdateDownloadProgress"
|
||||
body:@{
|
||||
@"receivedBytes":[NSNumber numberWithLongLong:receivedBytes],
|
||||
@"totalBytes":[NSNumber numberWithLongLong:totalBytes]
|
||||
}];
|
||||
} completionHandler:^(NSString *path, NSError *error) {
|
||||
if (error) {
|
||||
callback(@[error.description]);
|
||||
}
|
||||
else {
|
||||
NSString *unzipFilePath = [dir stringByAppendingPathComponent:unzipName];
|
||||
[SSZipArchive unzipFileAtPath:savePath toDestination:unzipFilePath progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
|
||||
[self.bridge.eventDispatcher sendAppEventWithName:@"RCTHotUpdateUnzipProgress"
|
||||
body:@{
|
||||
@"receivedBytes":[NSNumber numberWithLong:entryNumber],
|
||||
@"totalBytes":[NSNumber numberWithLong:total]
|
||||
}];
|
||||
|
||||
// NSLog(@"%ld %ld", entryNumber, total);
|
||||
} completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
|
||||
if (error) {
|
||||
callback(@[error.description]);
|
||||
}
|
||||
else {
|
||||
callback(@[[NSNull null]]);
|
||||
}
|
||||
}];
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options)
|
||||
{
|
||||
}
|
||||
|
||||
RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options)
|
||||
{
|
||||
}
|
||||
|
||||
#pragma mark - private
|
||||
|
||||
- (NSString *)donwloadDirPath
|
||||
{
|
||||
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
|
||||
NSString *downloadDir = [directory stringByAppendingPathComponent:@"reactnativecnhotupdate"];
|
||||
|
||||
return downloadDir;
|
||||
}
|
||||
|
||||
- (NSString *)constDir
|
||||
{
|
||||
NSString *downloadDir = [self donwloadDirPath];
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
BOOL isDir;
|
||||
if ([fileManager fileExistsAtPath:downloadDir isDirectory:&isDir]) {
|
||||
if (isDir) {
|
||||
return downloadDir;
|
||||
}
|
||||
}
|
||||
|
||||
return @"";
|
||||
}
|
||||
|
||||
- (NSString *)getDownloadDir
|
||||
{
|
||||
NSString *downloadDir = [self donwloadDirPath];
|
||||
|
||||
NSFileManager *fileManager = [NSFileManager defaultManager];
|
||||
|
||||
BOOL isDir;
|
||||
if ([fileManager fileExistsAtPath:downloadDir isDirectory:&isDir]) {
|
||||
if (isDir) {
|
||||
return downloadDir;
|
||||
}
|
||||
}
|
||||
|
||||
NSError *error;
|
||||
if (![fileManager createDirectoryAtPath:downloadDir
|
||||
withIntermediateDirectories:YES
|
||||
attributes:nil
|
||||
error:&error])
|
||||
{
|
||||
return nil;
|
||||
}
|
||||
|
||||
return downloadDir;
|
||||
}
|
||||
|
||||
@end
|
Reference in New Issue
Block a user