1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
react-native-pushy/ios/RCTPushy/RCTPushy.m

529 lines
19 KiB
Mathematica
Raw Normal View History

2016-02-23 17:31:47 +08:00
//
2019-11-16 00:31:30 +08:00
// RCTPushy.m
// RCTPushy
2016-02-23 17:31:47 +08:00
//
// Created by LvBingru on 2/19/16.
// Copyright © 2016 erica. All rights reserved.
//
2019-11-16 00:31:30 +08:00
#import "RCTPushy.h"
#import "RCTPushyDownloader.h"
#import "RCTPushyManager.h"
2017-03-01 12:52:37 +08:00
2020-09-16 13:01:14 +08:00
2017-02-28 15:48:16 +08:00
#import <React/RCTConvert.h>
#import <React/RCTLog.h>
2020-09-17 23:18:17 +08:00
// #import <React/RCTReloadCommand.h>
2017-02-28 15:48:16 +08:00
2019-11-16 00:31:30 +08:00
static NSString *const keyPushyInfo = @"REACTNATIVECN_PUSHY_INFO_KEY";
2016-04-05 22:58:50 +08:00
static NSString *const paramPackageVersion = @"packageVersion";
2016-04-05 17:51:02 +08:00
static NSString *const paramLastVersion = @"lastVersion";
static NSString *const paramCurrentVersion = @"currentVersion";
static NSString *const paramIsFirstTime = @"isFirstTime";
static NSString *const paramIsFirstLoadOk = @"isFirstLoadOK";
2020-08-31 11:47:08 +08:00
static NSString *const keyBlockUpdate = @"REACTNATIVECN_PUSHY_BLOCKUPDATE";
static NSString *const keyUuid = @"REACTNATIVECN_PUSHY_UUID";
2019-11-16 00:31:30 +08:00
static NSString *const keyFirstLoadMarked = @"REACTNATIVECN_PUSHY_FIRSTLOADMARKED_KEY";
static NSString *const keyRolledBackMarked = @"REACTNATIVECN_PUSHY_ROLLEDBACKMARKED_KEY";
static NSString *const KeyPackageUpdatedMarked = @"REACTNATIVECN_PUSHY_ISPACKAGEUPDATEDMARKED_KEY";
2016-02-23 19:51:58 +08:00
2016-04-02 14:24:33 +08:00
// app info
static NSString * const AppVersionKey = @"appVersion";
static NSString * const BuildVersionKey = @"buildVersion";
// file def
static NSString * const BUNDLE_FILE_NAME = @"index.bundlejs";
static NSString * const SOURCE_PATCH_NAME = @"__diff.json";
static NSString * const BUNDLE_PATCH_NAME = @"index.bundlejs.patch";
// error def
static NSString * const ERROR_OPTIONS = @"options error";
static NSString * const ERROR_BSDIFF = @"bsdiff error";
static NSString * const ERROR_FILE_OPERATION = @"file operation error";
// event def
2019-11-16 00:31:30 +08:00
static NSString * const EVENT_PROGRESS_DOWNLOAD = @"RCTPushyDownloadProgress";
// static NSString * const EVENT_PROGRESS_UNZIP = @"RCTPushyUnzipProgress";
2020-09-24 22:44:37 +08:00
static NSString * const PARAM_PROGRESS_HASH = @"hash";
2016-04-02 14:24:33 +08:00
static NSString * const PARAM_PROGRESS_RECEIVED = @"received";
static NSString * const PARAM_PROGRESS_TOTAL = @"total";
2019-11-16 00:31:30 +08:00
typedef NS_ENUM(NSInteger, PushyType) {
PushyTypeFullDownload = 1,
PushyTypePatchFromPackage = 2,
PushyTypePatchFromPpk = 3,
2016-04-02 14:24:33 +08:00
};
static BOOL ignoreRollback = false;
2019-11-16 00:31:30 +08:00
@implementation RCTPushy {
RCTPushyManager *_fileManager;
2020-09-16 13:01:14 +08:00
bool hasListeners;
2016-04-02 14:24:33 +08:00
}
2016-02-23 17:31:47 +08:00
@synthesize methodQueue = _methodQueue;
2019-11-16 00:31:30 +08:00
RCT_EXPORT_MODULE(RCTPushy);
2016-02-23 17:31:47 +08:00
2016-04-05 17:51:02 +08:00
+ (NSURL *)bundleURL
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2019-11-16 00:31:30 +08:00
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
if (pushyInfo) {
NSString *curPackageVersion = [RCTPushy packageVersion];
NSString *packageVersion = [pushyInfo objectForKey:paramPackageVersion];
2016-04-05 17:51:02 +08:00
2019-11-16 00:31:30 +08:00
BOOL needClearPushyInfo = ![curPackageVersion isEqualToString:packageVersion];
if (needClearPushyInfo) {
[defaults setObject:nil forKey:keyPushyInfo];
2016-04-05 22:58:50 +08:00
[defaults setObject:@(YES) forKey:KeyPackageUpdatedMarked];
2016-04-05 17:51:02 +08:00
[defaults synchronize];
2016-04-05 22:58:50 +08:00
// ...need clear files later
2016-04-05 17:51:02 +08:00
}
2016-04-05 22:58:50 +08:00
else {
2019-11-16 00:31:30 +08:00
NSString *curVersion = pushyInfo[paramCurrentVersion];
NSString *lastVersion = pushyInfo[paramLastVersion];
2016-04-05 22:58:50 +08:00
2019-11-16 00:31:30 +08:00
BOOL isFirstTime = [pushyInfo[paramIsFirstTime] boolValue];
BOOL isFirstLoadOK = [pushyInfo[paramIsFirstLoadOk] boolValue];
2016-04-05 22:58:50 +08:00
2020-08-04 13:03:37 +08:00
NSString *loadVersion = curVersion;
BOOL needRollback = (!ignoreRollback && isFirstTime == NO && isFirstLoadOK == NO) || loadVersion.length<=0;
2016-04-05 22:58:50 +08:00
if (needRollback) {
2020-08-04 13:03:37 +08:00
loadVersion = lastVersion;
2016-04-05 22:58:50 +08:00
if (lastVersion.length) {
// roll back to last version
[defaults setObject:@{paramCurrentVersion:lastVersion,
paramIsFirstTime:@(NO),
paramIsFirstLoadOk:@(YES),
paramPackageVersion:curPackageVersion}
2019-11-16 00:31:30 +08:00
forKey:keyPushyInfo];
2016-04-05 22:58:50 +08:00
}
else {
// roll back to bundle
2019-11-16 00:31:30 +08:00
[defaults setObject:nil forKey:keyPushyInfo];
2016-04-05 22:58:50 +08:00
}
[defaults setObject:@(YES) forKey:keyRolledBackMarked];
[defaults synchronize];
// ...need clear files later
}
else if (isFirstTime && !ignoreRollback){
// bundleURL may be called many times, ignore rollbacks before process restarted again.
ignoreRollback = true;
2019-11-16 00:31:30 +08:00
NSMutableDictionary *newInfo = [[NSMutableDictionary alloc] initWithDictionary:pushyInfo];
2016-04-06 09:42:34 +08:00
newInfo[paramIsFirstTime] = @(NO);
2019-11-16 00:31:30 +08:00
[defaults setObject:newInfo forKey:keyPushyInfo];
2016-06-27 21:15:32 +08:00
[defaults setObject:@(YES) forKey:keyFirstLoadMarked];
2016-04-06 09:42:34 +08:00
[defaults synchronize];
}
2016-04-05 17:51:02 +08:00
2020-08-04 13:03:37 +08:00
if (loadVersion.length) {
2019-11-16 00:31:30 +08:00
NSString *downloadDir = [RCTPushy downloadDir];
2016-04-05 22:58:50 +08:00
2020-08-04 13:03:37 +08:00
NSString *bundlePath = [[downloadDir stringByAppendingPathComponent:loadVersion] stringByAppendingPathComponent:BUNDLE_FILE_NAME];
2016-04-05 22:58:50 +08:00
if ([[NSFileManager defaultManager] fileExistsAtPath:bundlePath isDirectory:NULL]) {
NSURL *bundleURL = [NSURL fileURLWithPath:bundlePath];
return bundleURL;
}
2016-04-05 17:51:02 +08:00
}
}
}
2019-11-16 00:31:30 +08:00
return [RCTPushy binaryBundleURL];
2016-04-05 17:51:02 +08:00
}
+ (BOOL)requiresMainQueueSetup {
2019-04-27 11:21:50 +08:00
// only set to YES if your module initialization relies on calling UIKit!
return NO;
}
2016-02-23 17:31:47 +08:00
- (NSDictionary *)constantsToExport
{
2016-04-05 17:51:02 +08:00
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2016-05-04 10:31:02 +08:00
NSMutableDictionary *ret = [NSMutableDictionary new];
2019-11-16 00:31:30 +08:00
ret[@"downloadRootDir"] = [RCTPushy downloadDir];
ret[@"packageVersion"] = [RCTPushy packageVersion];
ret[@"buildTime"] = [RCTPushy buildTime];
2016-04-05 22:58:50 +08:00
ret[@"isRolledBack"] = [defaults objectForKey:keyRolledBackMarked];
2016-06-27 21:15:32 +08:00
ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked];
2020-08-31 01:17:28 +08:00
ret[@"blockUpdate"] = [defaults objectForKey:keyBlockUpdate];
2020-08-31 11:47:08 +08:00
ret[@"uuid"] = [defaults objectForKey:keyUuid];
2019-11-16 00:31:30 +08:00
NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo];
ret[@"currentVersion"] = [pushyInfo objectForKey:paramCurrentVersion];
2016-04-05 17:58:12 +08:00
2016-06-27 21:15:32 +08:00
// clear isFirstTimemarked
if ([[defaults objectForKey:keyFirstLoadMarked] boolValue]) {
[defaults setObject:nil forKey:keyFirstLoadMarked];
2016-04-05 22:58:50 +08:00
}
// clear rolledbackmark
if ([[defaults objectForKey:keyRolledBackMarked] boolValue]) {
[defaults setObject:nil forKey:keyRolledBackMarked];
[self clearInvalidFiles];
2016-04-05 17:51:02 +08:00
}
2016-04-05 22:58:50 +08:00
// clear packageupdatemarked
if ([[defaults objectForKey:KeyPackageUpdatedMarked] boolValue]) {
[defaults setObject:nil forKey:KeyPackageUpdatedMarked];
[self clearInvalidFiles];
}
[defaults synchronize];
2016-04-05 17:51:02 +08:00
return ret;
2016-02-23 17:31:47 +08:00
}
- (instancetype)init
{
self = [super init];
if (self) {
2019-11-16 00:31:30 +08:00
_fileManager = [RCTPushyManager new];
2016-02-23 17:31:47 +08:00
}
return self;
}
2020-08-31 01:17:28 +08:00
RCT_EXPORT_METHOD(setBlockUpdate:(NSDictionary *)options)
{
// NSMutableDictionary *blockUpdateInfo = [NSMutableDictionary new];
// blockUpdateInfo[@"reason"] = options[@"reason"];
// blockUpdateInfo[@"until"] = options[@"until"];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:options forKey:keyBlockUpdate];
[defaults synchronize];
}
2020-08-31 11:47:08 +08:00
RCT_EXPORT_METHOD(setUuid:(NSString *)uuid)
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:uuid forKey:keyUuid];
[defaults synchronize];
}
2016-04-02 14:24:33 +08:00
RCT_EXPORT_METHOD(downloadUpdate:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
2016-02-23 17:31:47 +08:00
{
2019-11-16 00:31:30 +08:00
[self doPushy:PushyTypeFullDownload options:options callback:^(NSError *error) {
2016-04-02 14:24:33 +08:00
if (error) {
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
2016-04-02 14:24:33 +08:00
}
else {
resolve(nil);
}
}];
2016-02-23 17:31:47 +08:00
}
2016-04-05 17:51:02 +08:00
RCT_EXPORT_METHOD(downloadPatchFromPackage:(NSDictionary *)options
2016-04-02 14:24:33 +08:00
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
2016-02-23 17:31:47 +08:00
{
2019-11-16 00:31:30 +08:00
[self doPushy:PushyTypePatchFromPackage options:options callback:^(NSError *error) {
2016-02-23 17:31:47 +08:00
if (error) {
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
2016-02-23 17:31:47 +08:00
}
else {
2016-04-02 14:24:33 +08:00
resolve(nil);
}
}];
}
2016-04-05 17:51:02 +08:00
RCT_EXPORT_METHOD(downloadPatchFromPpk:(NSDictionary *)options
2016-04-02 14:24:33 +08:00
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
2019-11-16 00:31:30 +08:00
[self doPushy:PushyTypePatchFromPpk options:options callback:^(NSError *error) {
2016-04-02 14:24:33 +08:00
if (error) {
reject([NSString stringWithFormat: @"%lu", (long)error.code], error.localizedDescription, error);
2016-04-02 14:24:33 +08:00
}
else {
resolve(nil);
2016-02-23 17:31:47 +08:00
}
}];
}
RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options)
{
2020-09-24 22:44:37 +08:00
NSString *hash = options[@"hash"];
if (hash.length) {
2016-04-05 22:58:50 +08:00
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *lastVersion = nil;
2019-11-16 00:31:30 +08:00
if ([defaults objectForKey:keyPushyInfo]) {
NSDictionary *pushyInfo = [defaults objectForKey:keyPushyInfo];
lastVersion = pushyInfo[paramCurrentVersion];
2016-04-05 22:58:50 +08:00
}
NSMutableDictionary *newInfo = [[NSMutableDictionary alloc] init];
2020-09-24 22:44:37 +08:00
newInfo[paramCurrentVersion] = hash;
2016-04-05 22:58:50 +08:00
newInfo[paramLastVersion] = lastVersion;
newInfo[paramIsFirstTime] = @(YES);
newInfo[paramIsFirstLoadOk] = @(NO);
2019-11-16 00:31:30 +08:00
newInfo[paramPackageVersion] = [RCTPushy packageVersion];
[defaults setObject:newInfo forKey:keyPushyInfo];
2016-04-05 22:58:50 +08:00
[defaults synchronize];
2016-02-23 19:51:58 +08:00
}
2016-02-23 17:31:47 +08:00
}
RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options)
{
2020-09-24 22:44:37 +08:00
NSString *hash = options[@"hash"];
if (hash.length) {
2016-04-05 22:58:50 +08:00
[self setNeedUpdate:options];
2020-09-17 23:18:17 +08:00
// reload 0.62+
// RCTReloadCommandSetBundleURL([[self class] bundleURL]);
// RCTTriggerReloadCommandListeners(@"pushy reload");
dispatch_async(dispatch_get_main_queue(), ^{
[self.bridge setValue:[[self class] bundleURL] forKey:@"bundleURL"];
[self.bridge reload];
});
2016-02-23 19:51:58 +08:00
}
2016-02-23 17:31:47 +08:00
}
2016-04-06 10:21:00 +08:00
RCT_EXPORT_METHOD(markSuccess)
2016-02-23 17:31:47 +08:00
{
2019-11-16 00:31:30 +08:00
// up package info
2016-04-05 17:51:02 +08:00
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2019-11-16 00:31:30 +08:00
NSMutableDictionary *packageInfo = [[NSMutableDictionary alloc] initWithDictionary:[defaults objectForKey:keyPushyInfo]];
2016-04-05 17:51:02 +08:00
[packageInfo setObject:@(NO) forKey:paramIsFirstTime];
[packageInfo setObject:@(YES) forKey:paramIsFirstLoadOk];
2019-11-16 00:31:30 +08:00
[defaults setObject:packageInfo forKey:keyPushyInfo];
2016-04-05 17:51:02 +08:00
[defaults synchronize];
// clear other package dir
2016-04-05 22:58:50 +08:00
[self clearInvalidFiles];
2016-02-23 17:31:47 +08:00
}
2020-09-16 13:01:14 +08:00
2016-04-02 14:24:33 +08:00
#pragma mark - private
2020-09-16 13:01:14 +08:00
- (NSArray<NSString *> *)supportedEvents
{
return @[
EVENT_PROGRESS_DOWNLOAD,
// EVENT_PROGRESS_UNZIP
];
2020-09-16 13:01:14 +08:00
}
// Will be called when this module's first listener is added.
-(void)startObserving {
hasListeners = YES;
// Set up any upstream listeners or background tasks as necessary
}
// Will be called when this module's last listener is removed, or on dealloc.
-(void)stopObserving {
hasListeners = NO;
// Remove upstream listeners, stop unnecessary background tasks
}
2019-11-16 00:31:30 +08:00
- (void)doPushy:(PushyType)type options:(NSDictionary *)options callback:(void (^)(NSError *error))callback
2016-02-23 17:31:47 +08:00
{
2016-04-02 14:24:33 +08:00
NSString *updateUrl = [RCTConvert NSString:options[@"updateUrl"]];
2020-09-24 22:44:37 +08:00
NSString *hash = [RCTConvert NSString:options[@"hash"]];
if (updateUrl.length <= 0 || hash.length <= 0) {
2016-04-02 14:24:33 +08:00
callback([self errorWithMessage:ERROR_OPTIONS]);
return;
}
2020-09-24 22:44:37 +08:00
NSString *originHash = [RCTConvert NSString:options[@"originHash"]];
if (type == PushyTypePatchFromPpk && originHash <= 0) {
2016-04-02 14:24:33 +08:00
callback([self errorWithMessage:ERROR_OPTIONS]);
return;
2016-02-23 17:31:47 +08:00
}
2019-11-16 00:31:30 +08:00
NSString *dir = [RCTPushy downloadDir];
2016-04-02 14:24:33 +08:00
BOOL success = [_fileManager createDir:dir];
if (!success) {
callback([self errorWithMessage:ERROR_FILE_OPERATION]);
return;
}
2020-08-04 13:03:37 +08:00
2020-09-24 22:44:37 +08:00
NSString *zipFilePath = [dir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",hash, [self zipExtension:type]]];
// NSString *unzipDir = [dir stringByAppendingPathComponent:hash];
2016-04-02 14:24:33 +08:00
2019-11-16 00:31:30 +08:00
RCTLogInfo(@"RCTPushy -- download file %@", updateUrl);
[RCTPushyDownloader download:updateUrl savePath:zipFilePath progressHandler:^(long long receivedBytes, long long totalBytes) {
2020-09-16 13:01:14 +08:00
if (self->hasListeners) {
[self sendEventWithName:EVENT_PROGRESS_DOWNLOAD body:@{
2020-09-24 22:44:37 +08:00
PARAM_PROGRESS_HASH:hash,
2020-09-16 13:01:14 +08:00
PARAM_PROGRESS_RECEIVED:[NSNumber numberWithLongLong:receivedBytes],
PARAM_PROGRESS_TOTAL:[NSNumber numberWithLongLong:totalBytes]
}];
}
2016-04-02 14:24:33 +08:00
} completionHandler:^(NSString *path, NSError *error) {
if (error) {
callback(error);
}
else {
2019-11-16 00:31:30 +08:00
RCTLogInfo(@"RCTPushy -- unzip file %@", zipFilePath);
2020-09-24 22:44:37 +08:00
NSString *unzipFilePath = [dir stringByAppendingPathComponent:hash];
2020-09-16 13:01:14 +08:00
[self->_fileManager unzipFileAtPath:zipFilePath toDestination:unzipFilePath progressHandler:^(NSString *entry,long entryNumber, long total) {
// if (self->hasListeners) {
// [self sendEventWithName:EVENT_PROGRESS_UNZIP
// body:@{
2020-09-24 22:44:37 +08:00
// PARAM_PROGRESS_HASH:hash,
// PARAM_PROGRESS_RECEIVED:[NSNumber numberWithLong:entryNumber],
// PARAM_PROGRESS_TOTAL:[NSNumber numberWithLong:total]
// }];
// }
2016-04-02 14:24:33 +08:00
} completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
2020-09-16 13:01:14 +08:00
dispatch_async(self->_methodQueue, ^{
2016-04-02 14:24:33 +08:00
if (error) {
callback(error);
}
else {
switch (type) {
2019-11-16 00:31:30 +08:00
case PushyTypePatchFromPackage:
2016-04-02 14:24:33 +08:00
{
2016-04-05 15:25:45 +08:00
NSString *sourceOrigin = [[NSBundle mainBundle] resourcePath];
2019-11-16 00:31:30 +08:00
NSString *bundleOrigin = [[RCTPushy binaryBundleURL] path];
2020-09-24 22:44:37 +08:00
[self patch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback];
2016-04-02 14:24:33 +08:00
}
break;
2019-11-16 00:31:30 +08:00
case PushyTypePatchFromPpk:
2016-04-02 14:24:33 +08:00
{
2020-09-24 22:44:37 +08:00
NSString *lastVersionDir = [dir stringByAppendingPathComponent:originHash];
2016-04-02 14:24:33 +08:00
2020-05-23 00:17:30 +08:00
NSString *sourceOrigin = lastVersionDir;
NSString *bundleOrigin = [lastVersionDir stringByAppendingPathComponent:BUNDLE_FILE_NAME];
2020-09-24 22:44:37 +08:00
[self patch:hash fromBundle:bundleOrigin source:sourceOrigin callback:callback];
2016-04-02 14:24:33 +08:00
}
break;
default:
callback(nil);
break;
}
}
});
}];
}
}];
2016-02-23 17:31:47 +08:00
}
2020-09-24 22:44:37 +08:00
- (void)patch:(NSString *)hash fromBundle:(NSString *)bundleOrigin source:(NSString *)sourceOrigin callback:(void (^)(NSError *error))callback
2016-02-23 17:31:47 +08:00
{
2020-09-24 22:44:37 +08:00
NSString *unzipDir = [[RCTPushy downloadDir] stringByAppendingPathComponent:hash];
2016-04-02 14:24:33 +08:00
NSString *sourcePatch = [unzipDir stringByAppendingPathComponent:SOURCE_PATCH_NAME];
NSString *bundlePatch = [unzipDir stringByAppendingPathComponent:BUNDLE_PATCH_NAME];
2016-02-23 17:31:47 +08:00
2016-04-02 14:24:33 +08:00
NSString *destination = [unzipDir stringByAppendingPathComponent:BUNDLE_FILE_NAME];
[_fileManager bsdiffFileAtPath:bundlePatch fromOrigin:bundleOrigin toDestination:destination completionHandler:^(BOOL success) {
if (success) {
NSData *data = [NSData dataWithContentsOfFile:sourcePatch];
NSError *error = nil;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error];
if (error) {
callback(error);
return;
}
2016-04-06 13:05:52 +08:00
2016-04-02 14:24:33 +08:00
NSDictionary *copies = json[@"copies"];
2016-04-06 13:05:52 +08:00
NSDictionary *deletes = json[@"deletes"];
2020-09-16 13:01:14 +08:00
[self->_fileManager copyFiles:copies fromDir:sourceOrigin toDir:unzipDir deletes:deletes completionHandler:^(NSError *error) {
2016-04-02 14:24:33 +08:00
if (error) {
callback(error);
}
else {
callback(nil);
}
}];
2016-02-23 17:31:47 +08:00
}
2016-04-02 14:24:33 +08:00
else {
callback([self errorWithMessage:ERROR_BSDIFF]);
}
}];
}
2016-04-05 22:58:50 +08:00
- (void)clearInvalidFiles
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
2019-11-16 00:31:30 +08:00
NSDictionary *pushyInfo = [defaults objectForKey:keyPushyInfo];
NSString *curVersion = [pushyInfo objectForKey:paramCurrentVersion];
2016-04-05 22:58:50 +08:00
2019-11-16 00:31:30 +08:00
NSString *downloadDir = [RCTPushy downloadDir];
2016-04-05 22:58:50 +08:00
NSError *error = nil;
NSArray *list = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:downloadDir error:&error];
if (error) {
return;
}
for(NSString *fileName in list) {
if (![fileName isEqualToString:curVersion]) {
2016-04-06 13:05:52 +08:00
[_fileManager removeFile:[downloadDir stringByAppendingPathComponent:fileName] completionHandler:nil];
2016-04-05 22:58:50 +08:00
}
}
}
2019-11-16 00:31:30 +08:00
- (NSString *)zipExtension:(PushyType)type
2016-04-02 14:24:33 +08:00
{
switch (type) {
2019-11-16 00:31:30 +08:00
case PushyTypeFullDownload:
2016-04-02 14:24:33 +08:00
return @".ppk";
2019-11-16 00:31:30 +08:00
case PushyTypePatchFromPackage:
2020-02-27 00:00:49 +08:00
return @".ipa.patch";
2019-11-16 00:31:30 +08:00
case PushyTypePatchFromPpk:
2016-04-02 14:24:33 +08:00
return @".ppk.patch";
default:
break;
2016-02-23 17:31:47 +08:00
}
2016-04-02 14:24:33 +08:00
}
- (NSError *)errorWithMessage:(NSString *)errorMessage
{
2019-11-16 00:31:30 +08:00
return [NSError errorWithDomain:@"cn.reactnative.pushy"
2016-04-02 14:24:33 +08:00
code:-1
2016-04-05 17:51:02 +08:00
userInfo:@{ NSLocalizedDescriptionKey: errorMessage}];
2016-04-02 14:24:33 +08:00
}
2016-04-05 17:51:02 +08:00
+ (NSString *)downloadDir
2016-04-02 14:24:33 +08:00
{
NSString *directory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES) firstObject];
2019-11-16 00:31:30 +08:00
NSString *downloadDir = [directory stringByAppendingPathComponent:@"rctpushy"];
2016-04-02 14:24:33 +08:00
return downloadDir;
}
+ (NSURL *)binaryBundleURL
{
NSURL *url = [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
return url;
}
2016-04-05 22:58:50 +08:00
+ (NSString *)packageVersion
{
static NSString *version = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
version = [infoDictionary objectForKey:@"CFBundleShortVersionString"];
});
return version;
}
2019-10-06 21:56:58 +08:00
+ (NSString *)buildTime
{
#if DEBUG
return @"0";
#else
2019-10-06 22:20:57 +08:00
static NSString *buildTime;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSString *buildTimePath = [[NSBundle mainBundle] pathForResource:@"pushy_build_time" ofType:@"txt"];
buildTime = [[NSString stringWithContentsOfFile:buildTimePath encoding:NSUTF8StringEncoding error:nil]
stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
});
return buildTime;
2019-10-06 21:56:58 +08:00
#endif
}
2017-02-28 15:48:16 +08:00
@end