//一.对文件和文件夹处理
//功能:获取文件夹大小,判断缓存是否过期,超出预设容量,
//备注:文件夹的名字是url通过MD5,Hash得到的字符串。
#pragma mark - Cache Helper
- (NSString *)filePathFromRequest:(NSURLRequest *)request isInfo:(BOOL)info{
NSString *url = request.URL.absoluteString;
NSString *fileName = [self cacheRequestFileName:url];
NSString *otherInfoFileName = [self cacheRequestOtherInfoFileName:url];
NSString *filePath = [self cacheFilePath:fileName];
NSString *fileInfoPath = [self cacheFilePath:otherInfoFileName];
if (info) {
return fileInfoPath;
}
return filePath;
}
- (NSString *)cacheRequestFileName:(NSString *)requestUrl {
return [self md5Hash:[NSString stringWithFormat:@"%@",requestUrl]];
}
- (NSString *)cacheRequestOtherInfoFileName:(NSString *)requestUrl {
return [self md5Hash:[NSString stringWithFormat:@"%@-otherInfo",requestUrl]];
}
- (NSString *)cacheFilePath:(NSString *)file {
NSString *path = [NSString stringWithFormat:@"%@/%@",self.diskPath,self.cacheFolder];
NSFileManager *fm = [NSFileManager defaultManager];
BOOL isDir;
if ([fm fileExistsAtPath:path isDirectory:&isDir] && isDir) {
//
} else {
[fm createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *subDirPath = [NSString stringWithFormat:@"%@/%@/%@",self.diskPath,self.cacheFolder,self.subDirectory];
if ([fm fileExistsAtPath:subDirPath isDirectory:&isDir] && isDir) {
//
} else {
[fm createDirectoryAtPath:subDirPath withIntermediateDirectories:YES attributes:nil error:nil];
}
NSString *cFilePath = [NSString stringWithFormat:@"%@/%@",subDirPath,file];
// NSLog(@"%@",cFilePath);
return cFilePath;
}
//清除自建的缓存目录
- (void)checkCapacity {
if ([self folderSize] > self.diskCapacity) {
[self deleteCacheFolder];
}
}
- (void)deleteCacheFolder {
[[NSFileManager defaultManager] removeItemAtPath:[self cacheFolderPath] error:nil];
}
- (NSUInteger)folderSize {
NSArray *filesArray = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:[self cacheFolderPath] error:nil];
NSEnumerator *filesEnumerator = [filesArray objectEnumerator];
NSString *fileName;
unsigned long long int fileSize = 0;
while (fileName = [filesEnumerator nextObject]) {
NSDictionary *fileDic = [[NSFileManager defaultManager] attributesOfItemAtPath:[[self cacheFolderPath] stringByAppendingPathComponent:fileName] error:nil];
fileSize += [fileDic fileSize];
}
return (NSUInteger)fileSize;
}
#pragma mark - Function Helper
- (NSString *)md5Hash:(NSString *)str {
const char *cStr = [str UTF8String];
unsigned char result[16];
CC_MD5( cStr, (CC_LONG)strlen(cStr), result );
NSString *md5Result = [NSString stringWithFormat:
@"%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
return md5Result;
}
- (NSString *)cacheFolderPath {
return [NSString stringWithFormat:@"%@/%@/%@",self.diskPath,self.cacheFolder,self.subDirectory];
}
#pragma mark - Getter
- (NSString *)diskPath {
if (!_diskPath) {
_diskPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
}
return _diskPath;
}
- (NSMutableDictionary *)responseDic {
if (!_responseDic) {
_responseDic = [NSMutableDictionary dictionaryWithCapacity:0];
}
return _responseDic;
}
- (NSString *)cacheFolder {
if (!_cacheFolder) {
_cacheFolder = @"Url";
}
return _cacheFolder;
}
- (NSString *)subDirectory {
if (!_subDirectory) {
_subDirectory = @"UrlCacheDownload";
}
return _subDirectory;
}
- (NSUInteger)memoryCapacity {
if (!_memoryCapacity) {
_memoryCapacity = 20 * 1024 * 1024;
}
return _memoryCapacity;
}
- (NSUInteger)diskCapacity {
if (!_diskCapacity) {
_diskCapacity = 200 * 1024 * 1024;
}
return _diskCapacity;
}
- (NSUInteger)cacheTime {
if (!_cacheTime) {
_cacheTime = 0;
}
return _cacheTime;
}