1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee
react-native-pushy/ios/RCTPushy/RCTPushyManager.m
sunnylqm a4a372f17e Revert "Exclude backup"
This reverts commit 2d805fb38e.
2020-08-08 21:09:15 +08:00

191 lines
6.1 KiB
Objective-C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// RCTPushyManager.m
// RCTPushy
//
// Created by lvbingru on 16/4/1.
// Copyright © 2016年 erica. All rights reserved.
//
#import "RCTPushyManager.h"
#import "ZipArchive.h"
#import "BSDiff.h"
#import "bspatch.h"
@implementation RCTPushyManager {
dispatch_queue_t _opQueue;
}
- (instancetype)init
{
self = [super init];
if (self) {
_opQueue = dispatch_queue_create("cn.reactnative.pushy", DISPATCH_QUEUE_SERIAL);
}
return self;
}
- (BOOL)createDir:(NSString *)dir
{
__block BOOL success = false;
dispatch_sync(_opQueue, ^{
BOOL isDir;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:dir isDirectory:&isDir]) {
if (isDir) {
success = true;
return;
}
}
NSError *error;
[fileManager createDirectoryAtPath:dir
withIntermediateDirectories:YES
attributes:nil
error:&error];
if (!error) {
success = true;
return;
}
});
return success;
}
- (void)unzipFileAtPath:(NSString *)path
toDestination:(NSString *)destination
progressHandler:(void (^)(NSString *entry, long entryNumber, long total))progressHandler
completionHandler:(void (^)(NSString *path, BOOL succeeded, NSError *error))completionHandler
{
dispatch_async(_opQueue, ^{
if ([[NSFileManager defaultManager] fileExistsAtPath:destination]) {
[[NSFileManager defaultManager] removeItemAtPath:destination error:nil];
}
[SSZipArchive unzipFileAtPath:path toDestination:destination progressHandler:^(NSString *entry, unz_file_info zipInfo, long entryNumber, long total) {
progressHandler(entry, entryNumber, total);
} completionHandler:^(NSString *path, BOOL succeeded, NSError *error) {
// 解压完移除zip文件
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
if (completionHandler) {
completionHandler(path, succeeded, error);
}
}];
});
}
- (void)bsdiffFileAtPath:(NSString *)path
fromOrigin:(NSString *)origin
toDestination:(NSString *)destination
completionHandler:(void (^)(BOOL success))completionHandler
{
dispatch_async(_opQueue, ^{
BOOL success = [BSDiff bsdiffPatch:path origin:origin toDestination:destination];
if (completionHandler) {
completionHandler(success);
}
});
}
- (void)copyFiles:(NSDictionary *)filesDic
fromDir:(NSString *)fromDir
toDir:(NSString *)toDir
deletes:(NSDictionary *)deletes
completionHandler:(void (^)(NSError *error))completionHandler
{
dispatch_async(_opQueue, ^{
NSFileManager *fm = [NSFileManager defaultManager];
// merge old files
if (deletes!= nil) {
NSString *srcDir = [fromDir stringByAppendingPathComponent:@"assets"];
NSString *desDir = [toDir stringByAppendingPathComponent:@"assets"];
[self _mergeContentsOfPath:srcDir intoPath:desDir deletes:deletes];
}
// copy files
for (NSString *to in filesDic.allKeys) {
NSString *from = filesDic[to];
if (from.length <=0) {
from = to;
}
NSString *fromPath = [fromDir stringByAppendingPathComponent:from];
NSString *toPath = [toDir stringByAppendingPathComponent:to];
if ([fm fileExistsAtPath:toPath]) {
[fm removeItemAtPath:toPath error:nil];
}
NSError *error = nil;
[fm copyItemAtPath:fromPath toPath:toPath error:&error];
if (error) {
NSLog(@"Pushy copy error: %@", error.localizedDescription);
// if (completionHandler) {
// completionHandler(error);
// }
// return;
}
}
if (completionHandler) {
completionHandler(nil);
}
});
}
- (void)removeFile:(NSString *)filePath
completionHandler:(void (^)(NSError *error))completionHandler
{
dispatch_async(_opQueue, ^{
NSError *error = nil;
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
if (completionHandler) {
completionHandler(error);
}
});
}
- (void)_mergeContentsOfPath:(NSString *)srcDir intoPath:(NSString *)dstDir deletes:(NSDictionary *)deletes
{
NSFileManager *fm = [NSFileManager defaultManager];
NSDirectoryEnumerator *srcDirEnum = [fm enumeratorAtPath:srcDir];
NSString *subPath;
while ((subPath = [srcDirEnum nextObject])) {
NSString *srcFullPath = [srcDir stringByAppendingPathComponent:subPath];
NSString *potentialDstPath = [dstDir stringByAppendingPathComponent:subPath];
NSError *error = nil;
BOOL inDeletes = NO;
if (deletes) {
NSString *path = [@"assets" stringByAppendingPathComponent:subPath];
if (deletes[path]) {
inDeletes = YES;
}
}
if (!inDeletes) {
BOOL isDirectory = ([fm fileExistsAtPath:srcFullPath isDirectory:&isDirectory] && isDirectory);
if (isDirectory) {
if (![fm fileExistsAtPath:potentialDstPath isDirectory:nil]) {
[fm createDirectoryAtPath:potentialDstPath withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"Pushy merge error: %@", error.localizedDescription);
// return;
}
}
}
else {
if (![fm fileExistsAtPath:potentialDstPath]) {
[fm copyItemAtPath:srcFullPath toPath:potentialDstPath error:&error];
if (error) {
NSLog(@"Pushy merge error: %@", error.localizedDescription);
// return;
}
}
}
}
}
}
@end