update freebee method

This commit is contained in:
Pan, Xinlei (Ryan) 2018-05-10 11:32:08 -04:00
parent 374ae5973a
commit 1e7fb38f17
3 changed files with 19 additions and 42 deletions

View File

@ -44,8 +44,7 @@ typedef void(^FreebeeLoadFinishedHandler)(MVMCoreOperation* _Nullable freebeeOpe
- (BOOL)isFreeBeeAuthorizedValidUrl:(nullable NSURL*)url;
- (nullable NSString*)urlForidFromConfigDict:(nonnull NSString*)urlId;
- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url;
- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *_Nullable)imageURL;
- (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *_Nullable)url;
- (void)configureFreeBeeWithDict:(nullable NSDictionary*)configDict withSessionReset:(BOOL)isReset;
@end

View File

@ -230,53 +230,31 @@ typedef NS_ENUM(NSUInteger, FreeBeeCampaignState) {
}
#pragma mark FreeBee Helper for NSData
- (nullable NSData*)freebee_dataWithContentsOfURL:(NSURL *)url {
- (nullable NSCachedURLResponse*)freebee_dataWithContentsOfURL:(NSURL *)url {
NSData* data = nil;
NSDictionary* proxyDict = [self proxyDictionaryforUrl:url];
NSURLResponse* response = nil;
NSError* error = nil;
if ([self isFreeBeeEnabled] &&
[self isValidCampaign] && proxyDict &&
![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) {
MVMCoreLog(@"Free Data Url, %@", url);
NSURLResponse* response = nil;
NSError* error = nil;
data = [self sendSynchronousRequest:url returningResponse:&response error:&error];
if (error)
data = nil;
MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length);
} else {
data = [NSData dataWithContentsOfURL:url];
}
return data;
}
- (nullable NSCachedURLResponse*)freebee_dataWithImageURL:(NSURL *)imageURL {
NSData* data = nil;
NSDictionary* proxyDict = [self proxyDictionaryforUrl:imageURL];
if ([self isFreeBeeEnabled] &&
[self isValidCampaign] && proxyDict &&
![self isExpired] && [self isFreeBeeEnabledForCurrentModule]) {
MVMCoreLog(@"Free Image Data Url, %@", imageURL);
NSURLResponse* response = nil;
NSError* error = nil;
data = [self sendSynchronousRequest:imageURL returningResponse:&response error:&error];
MVMCoreLog(@"Free Data Url, %@", url);
data = [self sendSynchronousRequest:url returningResponse:&response error:&error];
if (error) {
data = nil;
}
MVMCoreLog(@"freebee_dataWithContentsOfURL:Free Data, %lu", (unsigned long)data.length);
MVMCoreLog(@"freebee_dataWithImageURL:Free Image Data, %lu", (unsigned long)data.length);
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data];
return cachedResponse;
} else {
//create response for identify
response = [[NSURLResponse alloc] initWithURL:url MIMEType:nil expectedContentLength:100 textEncodingName:nil];
data = [NSData dataWithContentsOfURL:url];
}
return nil;
NSCachedURLResponse *cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response data:data];
return cachedResponse;
}
#pragma mark - FreeBee Registration

View File

@ -459,11 +459,12 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSData *imageData = nil;
if (pathOrName.length > 0) {
if(pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) {
if (pathOrName.length >=4 && [[pathOrName substringToIndex:4] isEqualToString:@"http"]) {
// Gets the full path
NSURL *s7URL = [self handleS7Path:pathOrName useWidth:useWidth widthForS7:widthForS7 useHeight:useHeight heightForS7:heightForS7 format:format finalRect:CGRectNull flipImage:NO];
if (s7URL) {
[self downloadImage:s7URL isGif:YES fallbackImageName:fallbackImageName completionHandler:completionHandler];
return;
}
} else {
// Try Local. Check in memory cache
@ -514,16 +515,15 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
}
//download image
- (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler{
- (void)downloadImage:(NSURL *)s7URL isGif:(BOOL)isGif fallbackImageName:(nullable NSString *)fallbackImageName completionHandler:(nonnull MVMCoreGetImageBlock)completionHandler {
NSURLCache *sharedCache = [NSURLCache sharedURLCache];
NSURLSessionConfiguration *configure = [NSURLSessionConfiguration defaultSessionConfiguration];
configure.URLCache = sharedCache;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configure];
NSURLSession *session = [MVMCoreSessionObject sharedGlobal].session;
NSURLRequest *request = [NSURLRequest requestWithURL:s7URL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:ImageTimeOut];
//use Freebee to download image first, then store image to cache
NSCachedURLResponse *cachedResponse = [sharedCache cachedResponseForRequest:request];
if (!cachedResponse.data) {
cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithImageURL:s7URL];
cachedResponse = [[MFFreebeeHandler sharedHandler] freebee_dataWithContentsOfURL:s7URL];
if (cachedResponse) {
//system stores cache based datatask and check cache based on response,
//need to manually store cache for response