Skip to content

Commit

Permalink
fix: #174, 之前版本忽略 URL 的 query 导致缓存错误。
Browse files Browse the repository at this point in the history
  • Loading branch information
dito010 committed Jun 30, 2018
1 parent 1e8f62c commit 16cd621
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 22 deletions.
9 changes: 5 additions & 4 deletions JPVideoPlayer/JPVideoPlayerCache.m
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,13 @@ - (NSString *)cacheFileNameForKey:(NSString *)key{

- (NSString *)cachedFileNameForKey:(NSString *)key {
NSParameterAssert(key);
if(!key){
if(!key.length){
return nil;
}
if ([key length]) {
NSString *strippedQueryKey = [[NSURL URLWithString:key] absoluteStringByStrippingQuery];
key = [strippedQueryKey length] ? strippedQueryKey : key;

NSURL *url = [NSURL URLWithString:key];
if(url){
key = [url jp_cURLCommand];
}
const char *str = key.UTF8String;
if (str == NULL) str = "";
Expand Down
2 changes: 1 addition & 1 deletion JPVideoPlayer/JPVideoPlayerCacheFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ - (void)mergeRangesIfNeed {
NSRange range = [rangeValue rangeValue];
string = [string stringByAppendingString:[NSString stringWithFormat:@"%@; ", NSStringFromRange(range)]];
}
JPDebugLog(@"合并后已缓存区间: %@", string);
/// JPDebugLog(@"合并后已缓存区间: %@", string);
}
}

Expand Down
7 changes: 7 additions & 0 deletions JPVideoPlayer/JPVideoPlayerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,13 @@ shouldResumePlaybackFromPlaybackRecordForURL:(NSURL *)videoURL
*/
- (NSString *_Nullable)cacheKeyForURL:(NSURL *)url;

#pragma mark - Version

/**
* Return the version of SDK;
*/
- (NSString *)SDKVersion;

@end

NS_ASSUME_NONNULL_END
19 changes: 16 additions & 3 deletions JPVideoPlayer/JPVideoPlayerManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,18 +55,26 @@ @interface JPVideoPlayerManager()<JPVideoPlayerInternalDelegate,

@end

static NSString * const JPVideoPlayerSDKVersionKey = @"com.jpvideoplayer.sdk.version.www";
@implementation JPVideoPlayerManager
@synthesize volume;
@synthesize muted;
@synthesize rate;

+ (nonnull instancetype)sharedManager {
static dispatch_once_t once;
static id instance;
static JPVideoPlayerManager *jpVideoPlayerManagerInstance;
dispatch_once(&once, ^{
instance = [self new];
jpVideoPlayerManagerInstance = [self new];
[[NSUserDefaults standardUserDefaults] setObject:@"3.1.1" forKey:JPVideoPlayerSDKVersionKey];
[[NSUserDefaults standardUserDefaults] synchronize];
[JPMigration migrateToSDKVersion:@"3.1.1" block:^{

[jpVideoPlayerManagerInstance.videoCache clearDiskOnCompletion:nil];

}];
});
return instance;
return jpVideoPlayerManagerInstance;
}

- (nonnull instancetype)init {
Expand Down Expand Up @@ -259,6 +267,11 @@ - (NSString *_Nullable)cacheKeyForURL:(NSURL *)url {
return [url absoluteString];
}

- (NSString *)SDKVersion {
NSString *res = [[NSUserDefaults standardUserDefaults] valueForKey:JPVideoPlayerSDKVersionKey];
return (res ? res : @"");
}


#pragma mark - JPVideoPlayerPlaybackProtocol

Expand Down
26 changes: 20 additions & 6 deletions JPVideoPlayer/JPVideoPlayerSupportUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@

NS_ASSUME_NONNULL_BEGIN

@interface NSURL (StripQuery)
@interface NSURL (cURL)

/*
* Returns absolute string of URL with the query stripped out.
* If there is no query, returns a copy of absolute string.
/**
* Returns a `curl` command string equivalent of the current object.
*
* @return The URL to format.
*/

- (NSString *)absoluteStringByStrippingQuery;
- (NSString *)jp_cURLCommand;

@end

Expand Down Expand Up @@ -222,4 +222,18 @@ typedef NS_ENUM(NSInteger, JPApplicationState) {

@end

@interface JPMigration : NSObject

/**
* Executes a block of code for a specific version number and remembers this version as the latest migration done.
*
* @param version A string with a specific version number.
* @param migrationBlock A block object to be executed when the SDK version matches the string 'version'.
* This parameter can't be nil.
*/
+ (void)migrateToSDKVersion:(NSString *)version
block:(dispatch_block_t)migrationBlock;

@end

NS_ASSUME_NONNULL_END
105 changes: 97 additions & 8 deletions JPVideoPlayer/JPVideoPlayerSupportUtils.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,79 @@
#import "JPVideoPlayerCompat.h"
#import <MobileCoreServices/MobileCoreServices.h>

@implementation NSURL (StripQuery)
NS_ASSUME_NONNULL_BEGIN

- (NSString *)absoluteStringByStrippingQuery{
NSString *absoluteString = [self absoluteString];
NSUInteger queryLength = [[self query] length];
NSString* strippedString = (queryLength ? [absoluteString substringToIndex:[absoluteString length] - (queryLength + 1)] : absoluteString);
@interface NSMutableString (JPURLRequestFormatter)

if ([strippedString hasSuffix:@"?"]) {
strippedString = [strippedString substringToIndex:absoluteString.length-1];
- (void)jp_appendCommandLineArgument:(NSString *)arg;

@end

@implementation NSMutableString (JPURLRequestFormatter)

- (void)jp_appendCommandLineArgument:(NSString *)arg {
[self appendFormat:@" %@", [arg stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
}

@end

@interface JPURLRequestFormatter : NSObject

@end

@implementation JPURLRequestFormatter

+ (NSString *)cURLCommandFromURLRequest:(NSURLRequest *)request {
NSMutableString *command = [NSMutableString stringWithString:@"curl"];

[command jp_appendCommandLineArgument:[NSString stringWithFormat:@"-X %@", [request HTTPMethod]]];

if ([[request HTTPBody] length] > 0) {
NSMutableString *HTTPBodyString = [[NSMutableString alloc] initWithData:[request HTTPBody] encoding:NSUTF8StringEncoding];
[HTTPBodyString replaceOccurrencesOfString:@"\\" withString:@"\\\\" options:0 range:NSMakeRange(0, [HTTPBodyString length])];
[HTTPBodyString replaceOccurrencesOfString:@"`" withString:@"\\`" options:0 range:NSMakeRange(0, [HTTPBodyString length])];
[HTTPBodyString replaceOccurrencesOfString:@"\"" withString:@"\\\"" options:0 range:NSMakeRange(0, [HTTPBodyString length])];
[HTTPBodyString replaceOccurrencesOfString:@"$" withString:@"\\$" options:0 range:NSMakeRange(0, [HTTPBodyString length])];
[command jp_appendCommandLineArgument:[NSString stringWithFormat:@"-d \"%@\"", HTTPBodyString]];
}
return strippedString;

NSString *acceptEncodingHeader = [[request allHTTPHeaderFields] valueForKey:@"Accept-Encoding"];
if ([acceptEncodingHeader rangeOfString:@"gzip"].location != NSNotFound) {
[command jp_appendCommandLineArgument:@"--compressed"];
}

if ([request URL]) {
NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:[request URL]];
if (cookies.count) {
NSMutableString *mutableCookieString = [NSMutableString string];
for (NSHTTPCookie *cookie in cookies) {
[mutableCookieString appendFormat:@"%@=%@;", cookie.name, cookie.value];
}

[command jp_appendCommandLineArgument:[NSString stringWithFormat:@"--cookie \"%@\"", mutableCookieString]];
}
}

for (id field in [request allHTTPHeaderFields]) {
[command jp_appendCommandLineArgument:[NSString stringWithFormat:@"-H %@", [NSString stringWithFormat:@"'%@: %@'", field, [[request valueForHTTPHeaderField:field] stringByReplacingOccurrencesOfString:@"\'" withString:@"\\\'"]]]];
}

[command jp_appendCommandLineArgument:[NSString stringWithFormat:@"\"%@\"", [[request URL] absoluteString]]];

return [NSString stringWithString:command];
}

@end

@implementation NSURL (cURL)

- (NSString *)jp_cURLCommand {
NSURLRequest *request = [NSURLRequest requestWithURL:self];
NSParameterAssert(request);
if(!request){
return nil;
}
return [JPURLRequestFormatter cURLCommandFromURLRequest:request];
}

@end
Expand Down Expand Up @@ -623,3 +685,30 @@ - (void)handleScrollStopIfNeed {
}

@end

static NSString * const JPMigrationLastSDKVersionKey = @"com.jpvideoplayer.last.migration.version.www";
@implementation JPMigration

+ (void)migrateToSDKVersion:(NSString *)version
block:(dispatch_block_t)migrationBlock {
// version > lastMigrationVersion
if ([version compare:[self lastMigrationVersion] options:NSNumericSearch] == NSOrderedDescending) {
migrationBlock();
JPDebugLog(@"JPMigration: Running migration for version %@", version);
[self setLastMigrationVersion:version];
}
}

+ (NSString *)lastMigrationVersion {
NSString *res = [[NSUserDefaults standardUserDefaults] valueForKey:JPMigrationLastSDKVersionKey];
return (res ? res : @"");
}

+ (void)setLastMigrationVersion:(NSString *)version {
[[NSUserDefaults standardUserDefaults] setValue:version forKey:JPMigrationLastSDKVersionKey];
[[NSUserDefaults standardUserDefaults] synchronize];
}

@end

NS_ASSUME_NONNULL_END

0 comments on commit 16cd621

Please sign in to comment.