cachesFileManager.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. //获取资源名字并且保存资源
  17. + (NSString *)getFileNameWithContent:(id)content fileName:(NSString*)fileName type:(uploadFileType)type
  18. {
  19. // 解决服务器文件名有 /
  20. NSArray *strArr = [fileName componentsSeparatedByString:@"/"];
  21. if (strArr.count >= 2) {
  22. fileName = strArr.lastObject;
  23. }
  24. if (!content) {
  25. return @"";
  26. }
  27. NSString *filePath;
  28. NSData *data;
  29. switch (type) {
  30. case uploadFileTypeImage://image
  31. {
  32. filePath = [NSString stringWithFormat:@"%@/%@",kSHPath_image,fileName];
  33. if ([content isKindOfClass:[NSData class]]) {
  34. data = content;
  35. }
  36. }
  37. break;
  38. case uploadFileTypeVideo://video
  39. {
  40. if ([content isKindOfClass:[NSData class]]) {
  41. data = content;
  42. }
  43. // else if ([content isKindOfClass:[NSString class]])
  44. // {
  45. // data = [NSData dataWithContentsOfFile:content];
  46. // }
  47. filePath = [NSString stringWithFormat:@"%@/%@",kSHPath_video,fileName];
  48. }
  49. break;
  50. default:
  51. break;
  52. }
  53. //创建文件路径
  54. [cachesFileManager getCreateFilePath:[filePath stringByDeletingLastPathComponent]];
  55. if (data) {
  56. BOOL ret = [data writeToFile:filePath atomically:YES];
  57. if (!ret) {
  58. HLog(@"\n\n上传文件名写入失败\n\n");
  59. }
  60. }
  61. return filePath.length?filePath.lastPathComponent:@"";
  62. }
  63. //获取资源路径
  64. + (NSString *)getFilePathWithName:(NSString *)name type:(uploadFileType)type
  65. {
  66. // 解决服务器文件名有 /
  67. NSArray *strArr = [name componentsSeparatedByString:@"/"];
  68. if (strArr.count >= 2) {
  69. name = strArr.lastObject;
  70. }
  71. switch (type) {
  72. case uploadFileTypeImage://image
  73. {
  74. name = [NSString stringWithFormat:@"%@/%@",kSHPath_image,name];
  75. }
  76. break;
  77. case uploadFileTypeVideo://video
  78. {
  79. name = [NSString stringWithFormat:@"%@/%@",kSHPath_video,name];
  80. }
  81. break;
  82. default:
  83. break;
  84. }
  85. return name;
  86. }
  87. + (BOOL)removeItemAtPath:(NSString *)fileName type:(uploadFileType)type error:(NSError *__autoreleasing *)error {
  88. NSString *curPath = [cachesFileManager getFilePathWithName:fileName type:type];
  89. BOOL success = [[NSFileManager defaultManager] removeItemAtPath:curPath error:error];
  90. if (success) {
  91. // 文件删除成功
  92. } else {
  93. // 文件删除失败
  94. NSLog(@"删除文件失败:%@", error);
  95. }
  96. return success;
  97. }
  98. @end