Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 73 additions & 62 deletions ios/ChunkManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -29,87 +29,93 @@ @implementation ChunkManager
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject)
{
// Cast `RCTBridge` to `RCTCxxBridge`.
__weak RCTCxxBridge *bridge = (RCTCxxBridge *)_bridge;

NSURL *chunkUrl = [NSURL URLWithString:chunkUrlString];

// Handle http & https
if ([[chunkUrl scheme] hasPrefix:@"http"]) {
if (fetch) {
[self downloadAndCache:chunkId chunkUrl:chunkUrl completionHandler:^(NSError *error) {
if (error) {
reject(ChunkDownloadFailure, error.localizedFailureReason, nil);
} else {
[self execute:bridge chunkId:chunkId url:chunkUrl withResolver:resolve withRejecter:reject];
}
}];
} else {
[self execute:bridge chunkId:chunkId url:chunkUrl withResolver:resolve withRejecter:reject];
}
[self runInBackground:^(){
// Cast `RCTBridge` to `RCTCxxBridge`.
__weak RCTCxxBridge *bridge = (RCTCxxBridge *)_bridge;

} else if ([[chunkUrl scheme] isEqualToString:@"file"]) {
[self executeFromFilesystem:bridge url:chunkUrl withResolver:resolve withRejecter:reject];
NSURL *chunkUrl = [NSURL URLWithString:chunkUrlString];

} else {
reject(UnsupportedScheme,
[NSString stringWithFormat:@"Scheme in URL '%@' is not supported", chunkUrlString], nil);
}
// Handle http & https
if ([[chunkUrl scheme] hasPrefix:@"http"]) {
if (fetch) {
[self downloadAndCache:chunkId chunkUrl:chunkUrl completionHandler:^(NSError *error) {
if (error) {
reject(ChunkDownloadFailure, error.localizedFailureReason, nil);
} else {
[self execute:bridge chunkId:chunkId url:chunkUrl withResolver:resolve withRejecter:reject];
}
}];
} else {
[self execute:bridge chunkId:chunkId url:chunkUrl withResolver:resolve withRejecter:reject];
}

} else if ([[chunkUrl scheme] isEqualToString:@"file"]) {
[self executeFromFilesystem:bridge url:chunkUrl withResolver:resolve withRejecter:reject];

} else {
reject(UnsupportedScheme,
[NSString stringWithFormat:@"Scheme in URL '%@' is not supported", chunkUrlString], nil);
}
}];
}

RCT_EXPORT_METHOD(preloadChunk:(nonnull NSString*)chunkId
chunkUrl:(nonnull NSString*)chunkUrlString
fetch:(BOOL)fetch
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject) {
withRejecter:(RCTPromiseRejectBlock)reject)
{
if (!fetch) {
// Do nothing, chunk is already preloaded
resolve(nil);
} else {
NSURL *chunkUrl = [NSURL URLWithString:chunkUrlString];
if ([[chunkUrl scheme] hasPrefix:@"http"]) {
[self downloadAndCache:chunkId chunkUrl:chunkUrl completionHandler:^(NSError *error) {
if (error) {
reject(ChunkDownloadFailure, error.localizedFailureReason, nil);
} else {
resolve(nil);
}
}];
} else {
reject(UnsupportedScheme,
[NSString stringWithFormat:@"Scheme in URL '%@' is not supported", chunkUrlString], nil);
}
[self runInBackground:^(){
NSURL *chunkUrl = [NSURL URLWithString:chunkUrlString];
if ([[chunkUrl scheme] hasPrefix:@"http"]) {
[self downloadAndCache:chunkId chunkUrl:chunkUrl completionHandler:^(NSError *error) {
if (error) {
reject(ChunkDownloadFailure, error.localizedFailureReason, nil);
} else {
resolve(nil);
}
}];
} else {
reject(UnsupportedScheme,
[NSString stringWithFormat:@"Scheme in URL '%@' is not supported", chunkUrlString], nil);
}
}];
}

}

RCT_EXPORT_METHOD(invalidateChunks:(nonnull NSArray*)chunks
withResolver:(RCTPromiseResolveBlock)resolve
withRejecter:(RCTPromiseRejectBlock)reject) {
NSFileManager* manager = [NSFileManager defaultManager];
NSString* chunksDirecotryPath = [self getChunksDirectoryPath];

NSError *error;
if (chunks.count == 0 && [manager fileExistsAtPath:chunksDirecotryPath]) {
[manager removeItemAtPath:chunksDirecotryPath error:&error];
} else {
for (int i = 0; i < chunks.count; i++) {
NSString* chunkFilePath = [self getChunkFilePath:chunks[i]];
if ([manager fileExistsAtPath:chunkFilePath]) {
[manager removeItemAtPath:[self getChunkFilePath:chunks[i]] error:&error];
}
if (error != nil) {
break;
withRejecter:(RCTPromiseRejectBlock)reject)
{
[self runInBackground:^(){
NSFileManager* manager = [NSFileManager defaultManager];
NSString* chunksDirecotryPath = [self getChunksDirectoryPath];

NSError *error;
if (chunks.count == 0 && [manager fileExistsAtPath:chunksDirecotryPath]) {
[manager removeItemAtPath:chunksDirecotryPath error:&error];
} else {
for (int i = 0; i < chunks.count; i++) {
NSString* chunkFilePath = [self getChunkFilePath:chunks[i]];
if ([manager fileExistsAtPath:chunkFilePath]) {
[manager removeItemAtPath:[self getChunkFilePath:chunks[i]] error:&error];
}
if (error != nil) {
break;
}
}
}
}

if (error != nil) {
reject(InvalidationFailure, error.localizedDescription, nil);
} else {
resolve(nil);
}


if (error != nil) {
reject(InvalidationFailure, error.localizedDescription, nil);
} else {
resolve(nil);
}
}];
}

- (void)execute:(RCTCxxBridge *)bridge
Expand Down Expand Up @@ -202,4 +208,9 @@ - (void)executeFromFilesystem:(RCTCxxBridge *)bridge

}

- (void)runInBackground:(void(^)())callback
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), callback);
}

@end