142 lines
4.8 KiB
Objective-C
142 lines
4.8 KiB
Objective-C
//
|
|
// QXFileManager.m
|
|
// QXLive
|
|
//
|
|
// Created by 启星 on 2025/5/8.
|
|
//
|
|
|
|
#import "QXFileManager.h"
|
|
static NSString *separatorStr = @"$&";
|
|
///导航历史文件
|
|
static NSString *searhHistory = @"searhHistoty.txt";
|
|
|
|
/// path
|
|
static NSString *QXVideoPlayerDownloadAtPath = @"GiftSource";
|
|
@implementation QXFileManager
|
|
+(NSString *)getDocumentPath{
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
|
|
NSString *documentsDirectory = [paths objectAtIndex:0];
|
|
return documentsDirectory;
|
|
}
|
|
|
|
+(NSString *)getLibraryPreferencesPath{
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
|
|
NSString *PreferenceDirectory = [paths objectAtIndex:0];
|
|
return PreferenceDirectory;
|
|
}
|
|
|
|
+(NSString *)getLibraryCachesPath{
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
|
NSString *cacheDirectory = [paths objectAtIndex:0];
|
|
return cacheDirectory;
|
|
}
|
|
|
|
+(NSString *)getTmpPath{
|
|
NSString*tmpPath = NSTemporaryDirectory();
|
|
return tmpPath;
|
|
}
|
|
+ (NSString*)getSearchHistoryPath{
|
|
NSString *dir = [NSString stringWithFormat:@"%@/Documents/SearchHistory", NSHomeDirectory()];
|
|
BOOL isDir = NO;
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
BOOL existed = [fileManager fileExistsAtPath:dir isDirectory:&isDir];
|
|
///文件夹已存在
|
|
if (existed) {
|
|
return dir;
|
|
}
|
|
BOOL result = NO;
|
|
if (!(isDir == YES && existed == YES)){
|
|
result =[fileManager createDirectoryAtPath:dir withIntermediateDirectories:YES attributes:nil error:nil];
|
|
}
|
|
if (result) {
|
|
return dir;
|
|
}else{
|
|
return @"";
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+(BOOL)writeSearchHistoryWithContent:(NSString*)content{
|
|
NSString *path = [QXFileManager getSearchHistoryPath];
|
|
if (path.length == 0) {
|
|
NSLog(@"文件夹创建失败");
|
|
return NO;
|
|
}
|
|
NSString *filePath;
|
|
NSString *str = @"";
|
|
//获取历史搜索记录
|
|
NSArray *historyArr = [self getSearchHistory];
|
|
if ([historyArr containsObject:content]) {
|
|
QXLOG(@"搜素已存在");
|
|
return NO;
|
|
}
|
|
if (historyArr.count > 0) {
|
|
//历史搜索记录为空
|
|
str = [historyArr componentsJoinedByString:separatorStr];
|
|
}
|
|
//添加最新的搜索历史
|
|
if (str.length == 0) {
|
|
str = content;
|
|
}else{
|
|
str = [NSString stringWithFormat:@"%@%@%@",content,separatorStr,str];
|
|
}
|
|
|
|
filePath = [path stringByAppendingPathComponent:searhHistory];
|
|
|
|
if (filePath.length == 0) {
|
|
NSLog(@"文件路径为空");
|
|
return NO;
|
|
}
|
|
|
|
BOOL result = [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
|
|
if (result) {
|
|
NSLog(@"写入文件成功");
|
|
}else{
|
|
NSLog(@"写入文件失败");
|
|
}
|
|
return result;
|
|
}
|
|
|
|
+(NSArray<NSString *> *)getSearchHistory{
|
|
NSArray *arr;
|
|
NSString *filePath = [NSString stringWithFormat:@"%@/Documents/SearchHistory", NSHomeDirectory()];
|
|
filePath = [filePath stringByAppendingPathComponent:searhHistory];
|
|
NSString *str = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
|
|
if (!str || str.length == 0) {
|
|
return nil;
|
|
}
|
|
arr = [str componentsSeparatedByString:separatorStr];
|
|
return arr;
|
|
}
|
|
|
|
+(BOOL)removeSearchHistory{
|
|
NSString *filePath = [NSString stringWithFormat:@"%@/Documents/SearchHistory", NSHomeDirectory()];
|
|
filePath = [filePath stringByAppendingPathComponent:searhHistory];
|
|
return [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
|
|
}
|
|
+ (NSString*)createLocalSourceVideoDirectory{
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
|
NSString *cacheDirectory = [paths objectAtIndex:0];
|
|
NSString*path = [cacheDirectory stringByAppendingPathComponent:QXVideoPlayerDownloadAtPath];
|
|
if (![fileManager fileExistsAtPath:QXVideoPlayerDownloadAtPath]) {
|
|
NSError *error = nil;
|
|
BOOL ret = [fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:&error];
|
|
NSLog(@"文件夹创建是否成功=%u",ret);
|
|
}
|
|
return path;
|
|
}
|
|
+ (NSString*)getGiftVideoPath:(NSString*)videoName{
|
|
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
|
|
NSString *cacheDirectory = [paths objectAtIndex:0];
|
|
NSString*path = [cacheDirectory stringByAppendingPathComponent:QXVideoPlayerDownloadAtPath];
|
|
NSString *fileNamePath = [path stringByAppendingPathComponent:videoName];
|
|
return fileNamePath;
|
|
}
|
|
+ (BOOL)isExistsFileWithFilePath:(NSString *)filePath {
|
|
NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
return [fileManager fileExistsAtPath:filePath];
|
|
}
|
|
@end
|