123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- //
- // cachesFileManager.m
- // 隐私保护
- //
- // Created by xd h on 2023/11/21.
- //
- #import "cachesFileManager.h"
- @implementation cachesFileManager
- #pragma mark - 获取文件夹(没有的话创建)
- + (NSString *)getCreateFilePath:(NSString *)path {
-
- if (![[NSFileManager defaultManager] fileExistsAtPath:path]) {
-
- [[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
- }
- return path;
- }
- #pragma mark - 获取文件夹
- + (BOOL)checkFileIsSaveState:(NSString *)fileName withType:(uploadFileType)type
- {
- NSString * path = [cachesFileManager getFilePathWithName:fileName type:type];
- return [[NSFileManager defaultManager] fileExistsAtPath:path];
- }
- //获取资源名字并且保存资源
- + (NSString *)getFileNameWithContent:(id)content fileName:(NSString*)fileName type:(uploadFileType)type
- {
- // 解决服务器文件名有 /
- NSArray *strArr = [fileName componentsSeparatedByString:@"/"];
-
- if (strArr.count >= 2) {
- fileName = strArr.lastObject;
- }
-
- if (!content) {
- return @"";
- }
-
- NSString *filePath;
- NSData *data;
-
- switch (type) {
- case uploadFileTypeImage://image
- {
- filePath = [NSString stringWithFormat:@"%@/%@",kSHPath_image,fileName];
-
- if ([content isKindOfClass:[NSData class]]) {
- data = content;
- }
- }
- break;
-
- case uploadFileTypeVideo://video
- {
- if ([content isKindOfClass:[NSData class]]) {
- data = content;
- }
- // else if ([content isKindOfClass:[NSString class]])
- // {
- // data = [NSData dataWithContentsOfFile:content];
- // }
- filePath = [NSString stringWithFormat:@"%@/%@",kSHPath_video,fileName];
- }
- break;
-
- case DownLoadThumbnail://image
- {
- filePath = [NSString stringWithFormat:@"%@/%@",kSHPath_DownLoadThumbnail,fileName];
-
- if ([content isKindOfClass:[NSData class]]) {
- data = content;
- }
- }
- break;
-
- case DownLoadFileType://
- {
- filePath = [NSString stringWithFormat:@"%@/%@",kSHPath_DownLoadFlie,fileName];
-
- if ([content isKindOfClass:[NSData class]]) {
- data = content;
- }
- }
- break;
-
- default:
- break;
- }
-
-
-
- //创建文件路径
- [cachesFileManager getCreateFilePath:[filePath stringByDeletingLastPathComponent]];
-
- if (data) {
- BOOL ret = [data writeToFile:filePath atomically:YES];
- if (!ret) {
- HLog(@"\n\n上传文件名写入失败\n\n");
- }
- }
-
- return filePath.length?filePath.lastPathComponent:@"";
- }
- //获取资源路径
- + (NSString *)getFilePathWithName:(NSString *)name type:(uploadFileType)type
- {
-
- // 解决服务器文件名有 /
- NSArray *strArr = [name componentsSeparatedByString:@"/"];
-
- if (strArr.count >= 2) {
- name = strArr.lastObject;
- }
-
- switch (type) {
- case uploadFileTypeImage://image
- {
- name = [NSString stringWithFormat:@"%@/%@",kSHPath_image,name];
- }
- break;
-
- case uploadFileTypeVideo://video
- {
- name = [NSString stringWithFormat:@"%@/%@",kSHPath_video,name];
- }
- break;
- case DownLoadThumbnail://image
- {
- name = [NSString stringWithFormat:@"%@/%@",kSHPath_DownLoadThumbnail,name];
- }
- break;
- case DownLoadFileType://
- {
- name = [NSString stringWithFormat:@"%@/%@",kSHPath_DownLoadFlie,name];
- }
- break;
- default:
- break;
- }
-
- return name;
- }
- + (BOOL)removeItemAtPath:(NSString *)fileName type:(uploadFileType)type error:(NSError *__autoreleasing *)error {
- NSString *curPath = [cachesFileManager getFilePathWithName:fileName type:type];
-
- BOOL success = [[NSFileManager defaultManager] removeItemAtPath:curPath error:error];
- if (success) {
- // 文件删除成功
- } else {
- // 文件删除失败
- NSLog(@"删除文件失败:%@-----%@",fileName,error);
- }
-
- return success;
- }
- + (BOOL)copyVideoItemAtPath:(NSString *)Path fileName:(NSString *)fileName error:(NSError *__autoreleasing *)error {
- NSString *curPath = [cachesFileManager getFilePathWithName:fileName type:uploadFileTypeVideo];
-
- BOOL success = [[NSFileManager defaultManager] copyItemAtPath:Path toPath:curPath error:error];
- if (success) {
- // 文件删除成功
- } else {
- // 文件删除失败
- NSLog(@"删除文件失败:%@-----%@",fileName,error);
- }
-
- return success;
- }
- @end
|