cachesFileManager.m 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // cachesFileManager.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2023/11/21.
  6. //
  7. #import "cachesFileManager.h"
  8. @implementation cachesFileManager
  9. #pragma mark - 获取文件夹(没有的话创建)
  10. + (NSString *)getCreateFilePath:(NSString *)path {
  11. if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
  12. [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
  13. }
  14. return path;
  15. }
  16. #pragma mark - 获取文件夹(没有的话创建)
  17. + (BOOL)checkFileIsSaveState:(NSString *)fileName withType:(uploadFileType)type
  18. {
  19. NSString * path = [cachesFileManager getFilePathWithName:fileName type:type];
  20. return [[NSFileManager defaultManager] fileExistsAtPath:path];
  21. }
  22. //获取资源名字并且保存资源
  23. + (NSString *)getFileNameWithContent:(id)content fileName:(NSString*)fileName type:(uploadFileType)type
  24. {
  25. // 解决服务器文件名有 /
  26. NSArray *strArr = [fileName componentsSeparatedByString:@"/"];
  27. if (strArr.count >= 2) {
  28. fileName = strArr.lastObject;
  29. }
  30. if (!content) {
  31. return @"";
  32. }
  33. NSString *filePath;
  34. NSData *data;
  35. switch (type) {
  36. case uploadFileTypeImage://image
  37. {
  38. filePath = [NSString stringWithFormat:@"%@/%@",kSHPath_image,fileName];
  39. if ([content isKindOfClass:[NSData class]]) {
  40. data = content;
  41. }
  42. }
  43. break;
  44. case uploadFileTypeVideo://video
  45. {
  46. if ([content isKindOfClass:[NSData class]]) {
  47. data = content;
  48. }
  49. // else if ([content isKindOfClass:[NSString class]])
  50. // {
  51. // data = [NSData dataWithContentsOfFile:content];
  52. // }
  53. filePath = [NSString stringWithFormat:@"%@/%@",kSHPath_video,fileName];
  54. }
  55. break;
  56. case DownLoadThumbnail://image
  57. {
  58. filePath = [NSString stringWithFormat:@"%@/%@",kSHPath_DownLoadThumbnail,fileName];
  59. if ([content isKindOfClass:[NSData class]]) {
  60. data = content;
  61. }
  62. }
  63. break;
  64. default:
  65. break;
  66. }
  67. //创建文件路径
  68. [cachesFileManager getCreateFilePath:[filePath stringByDeletingLastPathComponent]];
  69. if (data) {
  70. BOOL ret = [data writeToFile:filePath atomically:YES];
  71. if (!ret) {
  72. HLog(@"\n\n上传文件名写入失败\n\n");
  73. }
  74. }
  75. return filePath.length?filePath.lastPathComponent:@"";
  76. }
  77. //获取资源路径
  78. + (NSString *)getFilePathWithName:(NSString *)name type:(uploadFileType)type
  79. {
  80. // 解决服务器文件名有 /
  81. NSArray *strArr = [name componentsSeparatedByString:@"/"];
  82. if (strArr.count >= 2) {
  83. name = strArr.lastObject;
  84. }
  85. switch (type) {
  86. case uploadFileTypeImage://image
  87. {
  88. name = [NSString stringWithFormat:@"%@/%@",kSHPath_image,name];
  89. }
  90. break;
  91. case uploadFileTypeVideo://video
  92. {
  93. name = [NSString stringWithFormat:@"%@/%@",kSHPath_video,name];
  94. }
  95. break;
  96. case DownLoadThumbnail://image
  97. {
  98. name = [NSString stringWithFormat:@"%@/%@",kSHPath_DownLoadThumbnail,name];
  99. }
  100. break;
  101. default:
  102. break;
  103. }
  104. return name;
  105. }
  106. + (BOOL)removeItemAtPath:(NSString *)fileName type:(uploadFileType)type error:(NSError *__autoreleasing *)error {
  107. NSString *curPath = [cachesFileManager getFilePathWithName:fileName type:type];
  108. BOOL success = [[NSFileManager defaultManager] removeItemAtPath:curPath error:error];
  109. if (success) {
  110. // 文件删除成功
  111. } else {
  112. // 文件删除失败
  113. NSLog(@"删除文件失败:%@-----%@",fileName,error);
  114. }
  115. return success;
  116. }
  117. + (BOOL)copyVideoItemAtPath:(NSString *)Path fileName:(NSString *)fileName error:(NSError *__autoreleasing *)error {
  118. NSString *curPath = [cachesFileManager getFilePathWithName:fileName type:uploadFileTypeVideo];
  119. BOOL success = [[NSFileManager defaultManager] copyItemAtPath:Path toPath:curPath error:error];
  120. if (success) {
  121. // 文件删除成功
  122. } else {
  123. // 文件删除失败
  124. NSLog(@"删除文件失败:%@-----%@",fileName,error);
  125. }
  126. return success;
  127. }
  128. @end