2014-09-26 17:38:35 -07:00
|
|
|
//
|
|
|
|
// NBURLCache.m
|
|
|
|
// NewsBlur
|
|
|
|
//
|
|
|
|
// Created by Samuel Clay on 9/26/14.
|
|
|
|
// Copyright (c) 2014 NewsBlur. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "NBURLCache.h"
|
|
|
|
#import "Utilities.h"
|
|
|
|
|
|
|
|
@implementation NBURLCache
|
|
|
|
|
|
|
|
- (NSString *)substitutePath:(NSString *)pathString {
|
|
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
|
|
|
NSString *storyImagesDirectory = [[paths objectAtIndex:0]
|
|
|
|
stringByAppendingPathComponent:@"story_images"];
|
|
|
|
NSString *cachedImage = [[storyImagesDirectory
|
|
|
|
stringByAppendingPathComponent:[Utilities md5:pathString]] stringByAppendingPathExtension:[pathString pathExtension]];
|
|
|
|
return cachedImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
|
|
|
|
{
|
|
|
|
// Get the path for the request
|
|
|
|
NSString *pathString = [[request URL] absoluteString];
|
|
|
|
|
2014-10-01 15:40:33 -07:00
|
|
|
if (!pathString) return [super cachedResponseForRequest:request];
|
|
|
|
|
2014-09-26 17:38:35 -07:00
|
|
|
// See if we have a substitution file for this path
|
|
|
|
NSString *substitutionFileName = [self substitutePath:pathString];
|
|
|
|
BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:substitutionFileName];
|
|
|
|
if (!substitutionFileName || !exists)
|
|
|
|
{
|
2014-09-26 18:35:40 -07:00
|
|
|
// NSLog(@"No cache found: %@ / %@", pathString, substitutionFileName);
|
2014-09-26 17:38:35 -07:00
|
|
|
// No substitution file, return the default cache response
|
|
|
|
return [super cachedResponseForRequest:request];
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've already created a cache entry for this path, then return it.
|
|
|
|
NSCachedURLResponse *cachedResponse = [cachedResponses objectForKey:pathString];
|
|
|
|
if (cachedResponse)
|
|
|
|
{
|
2014-09-26 18:35:40 -07:00
|
|
|
// NSLog(@"Memory cached: %@", pathString);
|
|
|
|
return cachedResponse;
|
2014-09-26 17:38:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the path to the substitution file
|
|
|
|
NSString *substitutionFilePath = substitutionFileName;
|
|
|
|
|
|
|
|
// Load the data
|
|
|
|
NSData *data = [NSData dataWithContentsOfFile:substitutionFilePath];
|
|
|
|
|
|
|
|
// Create the cacheable response
|
|
|
|
NSURLResponse *response = [[NSURLResponse alloc]
|
|
|
|
initWithURL:[request URL]
|
|
|
|
MIMEType:nil
|
|
|
|
expectedContentLength:[data length]
|
|
|
|
textEncodingName:nil];
|
|
|
|
cachedResponse = [[NSCachedURLResponse alloc] initWithResponse:response
|
|
|
|
data:data];
|
|
|
|
|
2014-09-26 18:35:40 -07:00
|
|
|
// NSLog(@"Got cached response: %@ / %@", pathString, substitutionFileName);
|
2014-09-26 17:38:35 -07:00
|
|
|
// Add it to our cache dictionary for subsequent responses
|
|
|
|
if (!cachedResponses)
|
|
|
|
{
|
|
|
|
cachedResponses = [[NSMutableDictionary alloc] init];
|
|
|
|
}
|
|
|
|
[cachedResponses setObject:cachedResponse forKey:pathString];
|
|
|
|
|
|
|
|
return cachedResponse;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|