//
//  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;
}

//获取资源名字并且保存资源
+ (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;
        
        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;

        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;
}
@end
