From 1e7fb38f17bdae9e1c39925af80a489ce1c3e002 Mon Sep 17 00:00:00 2001 From: "Pan, Xinlei (Ryan)" Date: Thu, 10 May 2018 11:32:08 -0400 Subject: [PATCH] update freebee method --- .../LoadHandling/FreeBee/MFFreebeeHandler.h | 3 +- .../LoadHandling/FreeBee/MFFreebeeHandler.m | 46 +++++-------------- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 12 ++--- 3 files changed, 19 insertions(+), 42 deletions(-) diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h index 9ab7393..fb148d5 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.h @@ -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 diff --git a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m index 698043b..e52684e 100644 --- a/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m +++ b/MVMCore/MVMCore/LoadHandling/FreeBee/MFFreebeeHandler.m @@ -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 diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 45e9603..aea1f46 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -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