Merge pull request #2061 from dvkch/master

Cached image filename are sometimes generated with invalid path extensions
This commit is contained in:
Bogdan Poplauschi 2017-10-21 17:13:50 +03:00
commit 04855c945e
2 changed files with 16 additions and 2 deletions

View File

@ -168,10 +168,11 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) {
}
unsigned char r[CC_MD5_DIGEST_LENGTH];
CC_MD5(str, (CC_LONG)strlen(str), r);
NSURL *keyURL = [NSURL URLWithString:key];
NSString *ext = keyURL ? keyURL.pathExtension : key.pathExtension;
NSString *filename = [NSString stringWithFormat:@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%@",
r[0], r[1], r[2], r[3], r[4], r[5], r[6], r[7], r[8], r[9], r[10],
r[11], r[12], r[13], r[14], r[15], [key.pathExtension isEqualToString:@""] ? @"" : [NSString stringWithFormat:@".%@", key.pathExtension]];
r[11], r[12], r[13], r[14], r[15], ext.length == 0 ? @"" : [NSString stringWithFormat:@".%@", ext]];
return filename;
}

View File

@ -214,6 +214,19 @@ NSString *kImageTestKey = @"TestImageKey.jpg";
[self waitForExpectationsWithCommonTimeout];
}
- (void)test34CachePathForSimpleKeyWithExtension {
NSString *cachePath = [[SDImageCache sharedImageCache] cachePathForKey:kTestJpegURL inPath:@""];
expect(cachePath).toNot.beNil();
expect([cachePath pathExtension]).to.equal(@"jpg");
}
- (void)test35CachePathForKeyWithDotButNoExtension {
NSString *urlString = @"https://maps.googleapis.com/maps/api/staticmap?center=48.8566,2.3522&format=png&maptype=roadmap&scale=2&size=375x200&zoom=15";
NSString *cachePath = [[SDImageCache sharedImageCache] cachePathForKey:urlString inPath:@""];
expect(cachePath).toNot.beNil();
expect([cachePath pathExtension]).to.equal(@"");
}
- (void)test40InsertionOfImageData {
XCTestExpectation *expectation = [self expectationWithDescription:@"Insertion of image data works"];