2019-11-16 00:31:30 +08:00
|
|
|
|
#import "RCTPushyManager.h"
|
2016-04-02 14:24:33 +08:00
|
|
|
|
#import "ZipArchive.h"
|
2021-04-06 17:07:33 +08:00
|
|
|
|
#import "HDiffPatch.h"
|
2016-04-02 14:24:33 +08:00
|
|
|
|
|
2019-11-16 00:31:30 +08:00
|
|
|
|
@implementation RCTPushyManager {
|
2016-04-02 14:24:33 +08:00
|
|
|
|
dispatch_queue_t _opQueue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (instancetype)init
|
|
|
|
|
{
|
|
|
|
|
self = [super init];
|
|
|
|
|
if (self) {
|
2019-11-16 00:31:30 +08:00
|
|
|
|
_opQueue = dispatch_queue_create("cn.reactnative.pushy", DISPATCH_QUEUE_SERIAL);
|
2016-04-02 14:24:33 +08:00
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-08-08 21:09:15 +08:00
|
|
|
|
|
2016-04-02 14:24:33 +08:00
|
|
|
|
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, ^{
|
2016-04-05 18:24:19 +08:00
|
|
|
|
|
|
|
|
|
if ([[NSFileManager defaultManager] fileExistsAtPath:destination]) {
|
|
|
|
|
[[NSFileManager defaultManager] removeItemAtPath:destination error:nil];
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 14:24:33 +08:00
|
|
|
|
[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);
|
|
|
|
|
}
|
|
|
|
|
}];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-05 18:45:42 +08:00
|
|
|
|
- (void)hdiffFileAtPath:(NSString *)path
|
|
|
|
|
fromOrigin:(NSString *)origin
|
|
|
|
|
toDestination:(NSString *)destination
|
|
|
|
|
completionHandler:(void (^)(BOOL success))completionHandler
|
|
|
|
|
{
|
|
|
|
|
dispatch_async(_opQueue, ^{
|
2021-04-06 17:07:33 +08:00
|
|
|
|
BOOL success = [HDiffPatch hdiffPatch:path origin:origin toDestination:destination];
|
2021-04-05 18:45:42 +08:00
|
|
|
|
if (completionHandler) {
|
|
|
|
|
completionHandler(success);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 14:24:33 +08:00
|
|
|
|
- (void)copyFiles:(NSDictionary *)filesDic
|
|
|
|
|
fromDir:(NSString *)fromDir
|
|
|
|
|
toDir:(NSString *)toDir
|
2016-04-06 13:05:52 +08:00
|
|
|
|
deletes:(NSDictionary *)deletes
|
2016-04-02 14:24:33 +08:00
|
|
|
|
completionHandler:(void (^)(NSError *error))completionHandler
|
|
|
|
|
{
|
|
|
|
|
dispatch_async(_opQueue, ^{
|
2016-04-06 13:05:52 +08:00
|
|
|
|
NSFileManager *fm = [NSFileManager defaultManager];
|
|
|
|
|
|
|
|
|
|
// merge old files
|
|
|
|
|
if (deletes!= nil) {
|
|
|
|
|
NSString *srcDir = [fromDir stringByAppendingPathComponent:@"assets"];
|
|
|
|
|
NSString *desDir = [toDir stringByAppendingPathComponent:@"assets"];
|
2020-05-23 00:17:30 +08:00
|
|
|
|
[self _mergeContentsOfPath:srcDir intoPath:desDir deletes:deletes];
|
2016-04-06 13:05:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// copy files
|
2016-04-02 14:24:33 +08:00
|
|
|
|
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];
|
|
|
|
|
|
2016-04-06 13:05:52 +08:00
|
|
|
|
if ([fm fileExistsAtPath:toPath]) {
|
|
|
|
|
[fm removeItemAtPath:toPath error:nil];
|
|
|
|
|
}
|
2016-04-02 14:24:33 +08:00
|
|
|
|
NSError *error = nil;
|
2016-04-06 13:05:52 +08:00
|
|
|
|
[fm copyItemAtPath:fromPath toPath:toPath error:&error];
|
2016-04-02 14:24:33 +08:00
|
|
|
|
if (error) {
|
2020-05-23 00:17:30 +08:00
|
|
|
|
NSLog(@"Pushy copy error: %@", error.localizedDescription);
|
|
|
|
|
// if (completionHandler) {
|
|
|
|
|
// completionHandler(error);
|
|
|
|
|
// }
|
|
|
|
|
// return;
|
2016-04-02 14:24:33 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-23 00:17:30 +08:00
|
|
|
|
- (void)_mergeContentsOfPath:(NSString *)srcDir intoPath:(NSString *)dstDir deletes:(NSDictionary *)deletes
|
2016-04-06 13:05:52 +08:00
|
|
|
|
{
|
|
|
|
|
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];
|
2020-05-23 00:17:30 +08:00
|
|
|
|
|
|
|
|
|
NSError *error = nil;
|
|
|
|
|
|
2016-04-06 13:58:20 +08:00
|
|
|
|
BOOL inDeletes = NO;
|
|
|
|
|
if (deletes) {
|
|
|
|
|
NSString *path = [@"assets" stringByAppendingPathComponent:subPath];
|
|
|
|
|
if (deletes[path]) {
|
|
|
|
|
inDeletes = YES;
|
2016-04-06 13:05:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-06 13:58:20 +08:00
|
|
|
|
if (!inDeletes) {
|
|
|
|
|
BOOL isDirectory = ([fm fileExistsAtPath:srcFullPath isDirectory:&isDirectory] && isDirectory);
|
|
|
|
|
if (isDirectory) {
|
|
|
|
|
if (![fm fileExistsAtPath:potentialDstPath isDirectory:nil]) {
|
2020-05-23 00:17:30 +08:00
|
|
|
|
[fm createDirectoryAtPath:potentialDstPath withIntermediateDirectories:YES attributes:nil error:&error];
|
|
|
|
|
if (error) {
|
|
|
|
|
NSLog(@"Pushy merge error: %@", error.localizedDescription);
|
|
|
|
|
// return;
|
2016-04-06 13:05:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-04-06 13:58:20 +08:00
|
|
|
|
else {
|
|
|
|
|
if (![fm fileExistsAtPath:potentialDstPath]) {
|
2020-05-23 00:17:30 +08:00
|
|
|
|
[fm copyItemAtPath:srcFullPath toPath:potentialDstPath error:&error];
|
|
|
|
|
if (error) {
|
|
|
|
|
NSLog(@"Pushy merge error: %@", error.localizedDescription);
|
|
|
|
|
// return;
|
2016-04-06 13:58:20 +08:00
|
|
|
|
}
|
2016-04-06 13:05:52 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-02 14:24:33 +08:00
|
|
|
|
@end
|