cachesFileManager.m 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. case DownLoadFileType://
  65. {
  66. filePath = [NSString stringWithFormat:@"%@/%@",kSHPath_DownLoadFlie,fileName];
  67. if ([content isKindOfClass:[NSData class]]) {
  68. data = content;
  69. }
  70. }
  71. break;
  72. default:
  73. break;
  74. }
  75. //创建文件路径
  76. [cachesFileManager getCreateFilePath:[filePath stringByDeletingLastPathComponent]];
  77. if (data) {
  78. BOOL ret = [data writeToFile:filePath atomically:YES];
  79. if (!ret) {
  80. HLog(@"\n\n上传文件名写入失败\n\n");
  81. }
  82. }
  83. return filePath.length?filePath.lastPathComponent:@"";
  84. }
  85. //获取资源路径
  86. + (NSString *)getFilePathWithName:(NSString *)name type:(uploadFileType)type
  87. {
  88. // 解决服务器文件名有 /
  89. NSArray *strArr = [name componentsSeparatedByString:@"/"];
  90. if (strArr.count >= 2) {
  91. name = strArr.lastObject;
  92. }
  93. switch (type) {
  94. case uploadFileTypeImage://image
  95. {
  96. name = [NSString stringWithFormat:@"%@/%@",kSHPath_image,name];
  97. }
  98. break;
  99. case uploadFileTypeVideo://video
  100. {
  101. name = [NSString stringWithFormat:@"%@/%@",kSHPath_video,name];
  102. }
  103. break;
  104. case DownLoadThumbnail://image
  105. {
  106. name = [NSString stringWithFormat:@"%@/%@",kSHPath_DownLoadThumbnail,name];
  107. }
  108. break;
  109. case DownLoadFileType://
  110. {
  111. name = [NSString stringWithFormat:@"%@/%@",kSHPath_DownLoadFlie,name];
  112. }
  113. break;
  114. default:
  115. break;
  116. }
  117. return name;
  118. }
  119. + (BOOL)removeItemAtPath:(NSString *)fileName type:(uploadFileType)type error:(NSError *__autoreleasing *)error {
  120. NSString *curPath = [cachesFileManager getFilePathWithName:fileName type:type];
  121. BOOL success = [[NSFileManager defaultManager] removeItemAtPath:curPath error:error];
  122. if (success) {
  123. // 文件删除成功
  124. } else {
  125. // 文件删除失败
  126. NSLog(@"删除文件失败:%@-----%@",fileName,error);
  127. }
  128. return success;
  129. }
  130. + (BOOL)copyVideoItemAtPath:(NSString *)Path fileName:(NSString *)fileName error:(NSError *__autoreleasing *)error {
  131. NSString *curPath = [cachesFileManager getFilePathWithName:fileName type:uploadFileTypeVideo];
  132. BOOL success = [[NSFileManager defaultManager] copyItemAtPath:Path toPath:curPath error:error];
  133. if (success) {
  134. // 文件删除成功
  135. } else {
  136. // 文件删除失败
  137. NSLog(@"删除文件失败:%@-----%@",fileName,error);
  138. }
  139. return success;
  140. }
  141. @end