123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261 |
- //
- // 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:@"%@/%@.png",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 uploadFileTypeFileAPP://文件APP
- {
- name = [NSString stringWithFormat:@"%@/%@",kSHPath_FileAPP,name];
- }
- break;
- case uploadFileTypeRecord://文件APP
- {
- name = [NSString stringWithFormat:@"%@/%@",kSHPath_Record,name];
- }
- break;
- case DownLoadThumbnail://image
- {
- name = [NSString stringWithFormat:@"%@/%@.png",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;
- }
- //写日志
- + (void)writeLogsWithMsg:(NSString *)msg
- {
- //return;
-
- if (![[NSFileManager defaultManager] fileExistsAtPath:kSHPath_logs]) {
-
- [[NSFileManager defaultManager] createDirectoryAtPath:kSHPath_logs withIntermediateDirectories:YES attributes:nil error:nil];
- }
-
- NSString *logFilePath = [kSHPath_logs stringByAppendingPathComponent:@"app.log"];
-
- // 创建或打开文件
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager fileExistsAtPath:logFilePath]) {
- // 如果文件不存在,则创建它
- NSError *error;
- if (![[NSFileManager defaultManager] createFileAtPath:logFilePath contents:nil attributes:nil]) {
- NSLog(@"Unable to create file: %@", logFilePath);
- }
- }
-
- // 准备写入文件
- long long endOfFile = 0;
-
- NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:logFilePath]; // 创建文件句柄
- if (fileHandle) {
- endOfFile = [fileHandle seekToEndOfFile];
- if (endOfFile >= 0) {
- NSString *allMsg = [[NSString alloc] initWithFormat:@"%@ %@\n",[iTools getNowTimeString3],msg];
- NSData *fileData =[allMsg dataUsingEncoding:NSUTF8StringEncoding];
- [fileHandle writeData:fileData];
- }
-
- endOfFile = [fileHandle seekToEndOfFile];
- // 关闭文件句柄
- [fileHandle closeFile];
- }
-
- }
- //写日志
- + (void)writeCrashLogsWithMsg:(NSString *)msg
- {
- if (![[NSFileManager defaultManager] fileExistsAtPath:kSHPath_Crashlogs]) {
-
- [[NSFileManager defaultManager] createDirectoryAtPath:kSHPath_Crashlogs withIntermediateDirectories:YES attributes:nil error:nil];
- }
-
- NSString *logName = [[NSString alloc] initWithFormat:@"%@.txt",[iTools getNowTimeStampString]];
- NSString *logFilePath = [kSHPath_Crashlogs stringByAppendingPathComponent:logName];
-
- // 创建或打开文件
- NSFileManager *fileManager = [NSFileManager defaultManager];
- if (![fileManager fileExistsAtPath:logFilePath]) {
- // 如果文件不存在,则创建它
- //NSError *error;
- if (![[NSFileManager defaultManager] createFileAtPath:logFilePath contents:nil attributes:nil]) {
- NSLog(@"Unable to create file: %@", logFilePath);
- }
- }
-
- // 准备写入文件
- BOOL ret = [msg writeToFile:logFilePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
-
- if (!ret) {
- HLog(@"\n\n闪退日志写入失败\n\n");
- }
-
- }
- @end
|