暂无描述

FKDownloadStore.m 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // FKDownloadStore.m
  3. // FirstLink
  4. //
  5. // Created by ascii on 16/1/8.
  6. // Copyright © 2016年 FirstLink. All rights reserved.
  7. //
  8. #import "FKDownloadStore.h"
  9. #import "NSString+MD5.h"
  10. #include <CommonCrypto/CommonDigest.h>
  11. NSString *const REACTNATIVE_DIRECTORY_NAME = @"reactnative";
  12. @implementation FKDownloadStore
  13. + (NSString *)directoryPath:(NSString *)dirName {
  14. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
  15. NSString *path = [paths objectAtIndex:0];
  16. return [path stringByAppendingPathComponent:dirName];
  17. }
  18. + (NSString*)firstFilePathInDirectoryPath:(NSString *)dirPath {
  19. NSFileManager *fileManager = [NSFileManager defaultManager];
  20. if ([fileManager fileExistsAtPath:dirPath]) {
  21. NSString *fileName = [FKDownloadStore filesAtDirectory:dirPath].firstObject;
  22. if (fileName) {
  23. return [[FKDownloadStore directoryPath:REACTNATIVE_DIRECTORY_NAME] stringByAppendingPathComponent:fileName];
  24. }
  25. }
  26. return nil;
  27. }
  28. + (BOOL)removeFilesInDirectoryPath:(NSString *)dirPath {
  29. NSFileManager *fileManager = [NSFileManager defaultManager];
  30. NSArray *filePaths = [FKDownloadStore filesAtDirectory:dirPath];
  31. for (NSString *filePath in filePaths) {
  32. [fileManager removeItemAtPath:[dirPath stringByAppendingPathComponent:filePath]
  33. error:nil];
  34. }
  35. return YES;
  36. }
  37. + (BOOL)removeFileAtPath:(NSString *)filePath {
  38. NSFileManager *fileManager = [NSFileManager defaultManager];
  39. [fileManager removeItemAtPath:filePath
  40. error:nil];
  41. return YES;
  42. }
  43. + (BOOL)renameFileAtPath:(NSString *)oldPath toPath:(NSString *)toPath {
  44. NSError *error;
  45. NSFileManager *fileManager = [NSFileManager defaultManager];
  46. return [fileManager moveItemAtPath:oldPath toPath:toPath error:&error];
  47. }
  48. + (BOOL)makeDirectory:(NSString *)dirName {
  49. NSFileManager *fileManager = [NSFileManager defaultManager];
  50. NSString *dirPath = [FKDownloadStore directoryPath:dirName];
  51. if (![fileManager fileExistsAtPath:dirPath]) {
  52. return [fileManager createDirectoryAtPath:dirPath
  53. withIntermediateDirectories:NO
  54. attributes:nil
  55. error:nil];
  56. }
  57. return YES;
  58. }
  59. + (NSString *)md5OfFileAtPath:(NSString *)filePath {
  60. if (filePath) {
  61. NSError *error;
  62. NSString *localContent = [[NSString alloc] initWithContentsOfFile:filePath
  63. encoding:NSUTF8StringEncoding
  64. error:&error];
  65. if (!error) {
  66. return localContent.MD5;
  67. }
  68. }
  69. return nil;
  70. }
  71. #pragma mark - Private
  72. + (NSArray *)filesAtDirectory:(NSString *)dirPath {
  73. NSFileManager *fileManager = [NSFileManager defaultManager];
  74. if ([fileManager fileExistsAtPath:dirPath]) {
  75. NSError *error;
  76. NSArray *files = [fileManager contentsOfDirectoryAtPath:dirPath
  77. error:&error];
  78. if (!error) {
  79. return files;
  80. }
  81. }
  82. return nil;
  83. }
  84. @end