diff --git a/Carthage/Checkouts/PINCache/Source/PINDiskCache.h b/Carthage/Checkouts/PINCache/Source/PINDiskCache.h index 81bb26a4..3a4985b4 100644 --- a/Carthage/Checkouts/PINCache/Source/PINDiskCache.h +++ b/Carthage/Checkouts/PINCache/Source/PINDiskCache.h @@ -19,8 +19,8 @@ extern NSErrorUserInfoKey const PINDiskCacheErrorWriteFailureCodeKey; extern NSString * const PINDiskCachePrefix; typedef NS_ENUM(NSInteger, PINDiskCacheError) { - PINDiskCacheErrorReadFailure = -1000, - PINDiskCacheErrorWriteFailure = -1001, + PINDiskCacheErrorReadFailure = -1000, + PINDiskCacheErrorWriteFailure = -1001, }; /** diff --git a/Carthage/Checkouts/PINCache/Source/PINDiskCache.m b/Carthage/Checkouts/PINCache/Source/PINDiskCache.m index f30a5ad7..61128c36 100644 --- a/Carthage/Checkouts/PINCache/Source/PINDiskCache.m +++ b/Carthage/Checkouts/PINCache/Source/PINDiskCache.m @@ -17,7 +17,7 @@ #import #endif -#define PINDiskCacheError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \ +#define PINDiskCacheLogError(error) if (error) { NSLog(@"%@ (%d) ERROR: %@", \ [[NSString stringWithUTF8String:__FILE__] lastPathComponent], \ __LINE__, [error localizedDescription]); } @@ -332,7 +332,7 @@ - (PINDiskCacheSerializerBlock)defaultSerializer if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) { NSError *error = nil; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:object requiringSecureCoding:NO error:&error]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); return data; } else { return [NSKeyedArchiver archivedDataWithRootObject:object]; @@ -343,7 +343,15 @@ - (PINDiskCacheSerializerBlock)defaultSerializer - (PINDiskCacheDeserializerBlock)defaultDeserializer { return ^id(NSData * data, NSString *key){ - return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) { + NSError *error = nil; + NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error]; + PINDiskCacheLogError(error); + unarchiver.requiresSecureCoding = NO; + return [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; + } else { + return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + } }; } @@ -436,7 +444,7 @@ + (NSURL *)sharedTrashURL withIntermediateDirectories:YES attributes:nil error:&error]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); } trashURL = _sharedTrashURL; [[PINDiskCache sharedLock] unlock]; @@ -453,7 +461,7 @@ + (BOOL)moveItemAtURLToTrash:(NSURL *)itemURL NSString *uniqueString = [[NSProcessInfo processInfo] globallyUniqueString]; NSURL *uniqueTrashURL = [[PINDiskCache sharedTrashURL] URLByAppendingPathComponent:uniqueString isDirectory:NO]; BOOL moved = [[NSFileManager defaultManager] moveItemAtURL:itemURL toURL:uniqueTrashURL error:&error]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); return moved; } @@ -475,7 +483,7 @@ + (void)emptyTrash if (trashURL != nil) { NSError *removeTrashedItemError = nil; [[NSFileManager defaultManager] removeItemAtURL:trashURL error:&removeTrashedItemError]; - PINDiskCacheError(removeTrashedItemError); + PINDiskCacheLogError(removeTrashedItemError); } }); } @@ -491,7 +499,7 @@ - (BOOL)_locked_createCacheDirectory withIntermediateDirectories:YES attributes:nil error:&error]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); } @@ -523,7 +531,7 @@ - (NSUInteger)_locked_initializeDiskPropertiesForFile:(NSURL *)fileURL fileKey:( NSError *error = nil; NSDictionary *dictionary = [fileURL resourceValuesForKeys:[PINDiskCache resourceKeys] error:&error]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); if (_metadata[fileKey] == nil) { _metadata[fileKey] = [[PINDiskCacheMetadata alloc] init]; @@ -552,7 +560,7 @@ - (NSUInteger)_locked_initializeDiskPropertiesForFile:(NSURL *)fileURL fileKey:( if (errno != ENOATTR) { NSDictionary *userInfo = @{ PINDiskCacheErrorReadFailureCodeKey : @(errno)}; error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorReadFailure userInfo:userInfo]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); } } } @@ -573,7 +581,7 @@ - (void)initializeDiskProperties error:&error]; [self unlock]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); for (NSURL *fileURL in files) { NSString *fileKey = [self keyForEncodedFileURL:fileURL]; @@ -619,7 +627,7 @@ - (BOOL)_locked_setFileModificationDate:(NSDate *)date forURL:(NSURL *)fileURL BOOL success = [[NSFileManager defaultManager] setAttributes:@{ NSFileModificationDate: date } ofItemAtPath:[fileURL path] error:&error]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); return success; } @@ -646,14 +654,14 @@ - (BOOL)_locked_setAgeLimit:(NSTimeInterval)ageLimit forURL:(NSURL *)fileURL if (errno != ENOATTR) { NSDictionary *userInfo = @{ PINDiskCacheErrorWriteFailureCodeKey : @(errno)}; error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorWriteFailure userInfo:userInfo]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); } } } else { if (setxattr(PINDiskCacheFileSystemRepresentation(fileURL), PINDiskCacheAgeLimitAttributeName, &ageLimit, sizeof(NSTimeInterval), 0, 0) != 0) { NSDictionary *userInfo = @{ PINDiskCacheErrorWriteFailureCodeKey : @(errno)}; error = [NSError errorWithDomain:PINDiskCacheErrorDomain code:PINDiskCacheErrorWriteFailure userInfo:userInfo]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); } } @@ -1102,7 +1110,7 @@ - (id)objectForKeyedSubscript:(NSString *)key [self lock]; [[NSFileManager defaultManager] removeItemAtPath:[fileURL path] error:&error]; [self unlock]; - PINDiskCacheError(error) + PINDiskCacheLogError(error) PINDiskCacheException(exception); } [self lock]; @@ -1220,7 +1228,7 @@ - (void)setObject:(id )object forKey:(NSString *)key withAgeLimit:(NST NSError *writeError = nil; BOOL written = [data writeToURL:fileURL options:writeOptions error:&writeError]; - PINDiskCacheError(writeError); + PINDiskCacheLogError(writeError); if (written) { if (_metadata[key] == nil) { @@ -1229,7 +1237,7 @@ - (void)setObject:(id )object forKey:(NSString *)key withAgeLimit:(NST NSError *error = nil; NSDictionary *values = [fileURL resourceValuesForKeys:@[ NSURLCreationDateKey, NSURLContentModificationDateKey, NSURLTotalFileAllocatedSizeKey ] error:&error]; - PINDiskCacheError(error); + PINDiskCacheLogError(error); NSNumber *diskFileSize = [values objectForKey:NSURLTotalFileAllocatedSizeKey]; if (diskFileSize) { diff --git a/Carthage/Checkouts/PINCache/Tests/PINCacheTests.m b/Carthage/Checkouts/PINCache/Tests/PINCacheTests.m index 0a79a3ed..7a4bb370 100644 --- a/Carthage/Checkouts/PINCache/Tests/PINCacheTests.m +++ b/Carthage/Checkouts/PINCache/Tests/PINCacheTests.m @@ -1396,7 +1396,7 @@ - (void)testCustomEncoderDecoder { [testCache setObject:@(1) forKey:@"test_key"]; - XCTAssertNotNil([testCache objectForKey:@"test_key"], @"Object should not be nil"); + XCTAssertEqualObjects([testCache objectForKey:@"test_key"], @1); NSString *encodedKey = [[testCache fileURLForKey:@"test_key"] lastPathComponent]; XCTAssertEqualObjects(@"test_key", encodedKey, @"Encoded key should be equal to decoded one"); @@ -1407,6 +1407,9 @@ - (void)testTTLCacheIsSet { PINCache *cache = [[PINCache alloc] initWithName:@"test" rootPath:PINDiskCachePrefix serializer:nil deserializer:nil keyEncoder:nil keyDecoder:nil ttlCache:YES]; XCTAssert(cache.diskCache.isTTLCache); XCTAssert(cache.memoryCache.isTTLCache); + + [cache setObject:@(1) forKey:@"test_key"]; + XCTAssertNotNil([cache objectForKey:@"test_key"], @"Object should not be nil"); } @end diff --git a/Source/Classes/PINRemoteImageManager.m b/Source/Classes/PINRemoteImageManager.m index 57adce8b..11515596 100644 --- a/Source/Classes/PINRemoteImageManager.m +++ b/Source/Classes/PINRemoteImageManager.m @@ -133,7 +133,6 @@ @interface PINRemoteImageManager () PINAlternateRepresentationProvider *_defaultAlternateRepresentationProvider; __weak PINAlternateRepresentationProvider *_alternateRepProvider; NSURLSessionConfiguration *_sessionConfiguration; - } @property (nonatomic, strong) id cache; @@ -158,6 +157,7 @@ @interface PINRemoteImageManager () @property (nonatomic, strong) NSMutableDictionary *httpHeaderFields; @property (nonatomic, readonly) BOOL diskCacheTTLIsEnabled; @property (nonatomic, readonly) BOOL memoryCacheTTLIsEnabled; + #if DEBUG @property (nonatomic, assign) NSUInteger totalDownloads; #endif @@ -328,12 +328,24 @@ - (void)dealloc PINCache *pinCache = [[PINCache alloc] initWithName:kPINRemoteImageDiskCacheName rootPath:cacheURLRoot serializer:^NSData * _Nonnull(id _Nonnull object, NSString * _Nonnull key) { id obj = (id )object; if ([key hasPrefix:PINRemoteImageCacheKeyResumePrefix]) { - return [NSKeyedArchiver archivedDataWithRootObject:obj]; + if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) { + return [NSKeyedArchiver archivedDataWithRootObject:obj requiringSecureCoding:NO error:nil]; + } else { + return [NSKeyedArchiver archivedDataWithRootObject:object]; + } } return (NSData *)object; } deserializer:^id _Nonnull(NSData * _Nonnull data, NSString * _Nonnull key) { if ([key hasPrefix:PINRemoteImageCacheKeyResumePrefix]) { - return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) { + NSError *error = nil; + NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:&error]; + NSAssert(!error, @"unarchiver init failed with error"); + unarchiver.requiresSecureCoding = NO; + return [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; + } else { + return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + } } return data; } keyEncoder:nil keyDecoder:nil ttlCache:enableTtl]; diff --git a/Tests/PINRemoteImageTests.m b/Tests/PINRemoteImageTests.m index 3a772630..13063de9 100644 --- a/Tests/PINRemoteImageTests.m +++ b/Tests/PINRemoteImageTests.m @@ -795,9 +795,19 @@ - (void)testInvalidObject NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject]; PINDiskCache *tempDiskCache = [[PINDiskCache alloc] initWithName:kPINRemoteImageDiskCacheName rootPath:cachePath serializer:^NSData * _Nonnull(id _Nonnull object, NSString * _Nonnull key) { - return [NSKeyedArchiver archivedDataWithRootObject:object]; + if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) { + return [NSKeyedArchiver archivedDataWithRootObject:object requiringSecureCoding:NO error:nil]; + } else { + return [NSKeyedArchiver archivedDataWithRootObject:object]; + } } deserializer:^id _Nonnull(NSData * _Nonnull data, NSString * _Nonnull key) { - return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + if (@available(iOS 11.0, macOS 10.13, tvOS 11.0, watchOS 4.0, *)) { + NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingFromData:data error:nil]; + unarchiver.requiresSecureCoding = NO; + return [unarchiver decodeObjectForKey:NSKeyedArchiveRootObjectKey]; + } else { + return [NSKeyedUnarchiver unarchiveObjectWithData:data]; + } }]; [tempDiskCache setObject:@"invalid" forKey:[self.imageManager cacheKeyForURL:[self JPEGURL] processorKey:nil]];