// // FKDownloadStore.m // FirstLink // // Created by ascii on 16/1/8. // Copyright © 2016年 FirstLink. All rights reserved. // #import "FKDownloadStore.h" #import "NSString+MD5.h" #include NSString *const REACTNATIVE_DIRECTORY_NAME = @"reactnative"; @implementation FKDownloadStore + (NSString *)directoryPath:(NSString *)dirName { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; return [path stringByAppendingPathComponent:dirName]; } + (NSString*)firstFilePathInDirectoryPath:(NSString *)dirPath { NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:dirPath]) { NSString *fileName = [FKDownloadStore filesAtDirectory:dirPath].firstObject; if (fileName) { return [[FKDownloadStore directoryPath:REACTNATIVE_DIRECTORY_NAME] stringByAppendingPathComponent:fileName]; } } return nil; } + (BOOL)removeFilesInDirectoryPath:(NSString *)dirPath { NSFileManager *fileManager = [NSFileManager defaultManager]; NSArray *filePaths = [FKDownloadStore filesAtDirectory:dirPath]; for (NSString *filePath in filePaths) { [fileManager removeItemAtPath:[dirPath stringByAppendingPathComponent:filePath] error:nil]; } return YES; } + (BOOL)removeFileAtPath:(NSString *)filePath { NSFileManager *fileManager = [NSFileManager defaultManager]; [fileManager removeItemAtPath:filePath error:nil]; return YES; } + (BOOL)renameFileAtPath:(NSString *)oldPath toPath:(NSString *)toPath { NSError *error; NSFileManager *fileManager = [NSFileManager defaultManager]; return [fileManager moveItemAtPath:oldPath toPath:toPath error:&error]; } + (BOOL)makeDirectory:(NSString *)dirName { NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *dirPath = [FKDownloadStore directoryPath:dirName]; if (![fileManager fileExistsAtPath:dirPath]) { return [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:NO attributes:nil error:nil]; } return YES; } + (NSString *)md5OfFileAtPath:(NSString *)filePath { if (filePath) { NSError *error; NSString *localContent = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error]; if (!error) { return localContent.MD5; } } return nil; } #pragma mark - Private + (NSArray *)filesAtDirectory:(NSString *)dirPath { NSFileManager *fileManager = [NSFileManager defaultManager]; if ([fileManager fileExistsAtPath:dirPath]) { NSError *error; NSArray *files = [fileManager contentsOfDirectoryAtPath:dirPath error:&error]; if (!error) { return files; } } return nil; } @end