// // NIMKitFileLocationHelper.m // NIMKit // // Created by chris on 2016/11/12. // Copyright © 2016年 NetEase. All rights reserved. // #import "NIMKitFileLocationHelper.h" #import #import @interface NIMKitFileLocationHelper () + (NSString *)filepathForDir: (NSString *)dirname filename: (NSString *)filename; @end @implementation NIMKitFileLocationHelper + (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL { assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]); NSError *error = nil; BOOL success = [URL setResourceValue:@(YES) forKey:NSURLIsExcludedFromBackupKey error:&error]; if(!success) { NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error); } return success; } + (NSString *)getAppDocumentPath { static NSString *appDocumentPath = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ NSString *appKey = [NIMSDK sharedSDK].appKey; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); appDocumentPath= [[NSString alloc]initWithFormat:@"%@/%@/",[paths objectAtIndex:0],appKey]; if (![[NSFileManager defaultManager] fileExistsAtPath:appDocumentPath]) { [[NSFileManager defaultManager] createDirectoryAtPath:appDocumentPath withIntermediateDirectories:NO attributes:nil error:nil]; } [NIMKitFileLocationHelper addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:appDocumentPath]]; }); return appDocumentPath; } + (NSString *)getAppTempPath { return NSTemporaryDirectory(); } + (NSString *)userDirectory { NSString *documentPath = [NIMKitFileLocationHelper getAppDocumentPath]; NSString *userID = [NIMSDK sharedSDK].loginManager.currentAccount; if ([userID length] == 0) { NSLog(@"Error: Get User Directory While UserID Is Empty"); } NSString* userDirectory= [NSString stringWithFormat:@"%@%@/",documentPath,userID]; if (![[NSFileManager defaultManager] fileExistsAtPath:userDirectory]) { [[NSFileManager defaultManager] createDirectoryAtPath:userDirectory withIntermediateDirectories:NO attributes:nil error:nil]; } return userDirectory; } + (NSString *)resourceDir: (NSString *)resouceName { NSString *dir = [[NIMKitFileLocationHelper userDirectory] stringByAppendingPathComponent:resouceName]; if (![[NSFileManager defaultManager] fileExistsAtPath:dir]) { [[NSFileManager defaultManager] createDirectoryAtPath:dir withIntermediateDirectories:NO attributes:nil error:nil]; } return dir; } + (NSString *)filepathForVideo:(NSString *)filename { return [NIMKitFileLocationHelper filepathForDir:@"video" filename:filename]; } + (NSString *)filepathForImage:(NSString *)filename { return [NIMKitFileLocationHelper filepathForDir:@"image" filename:filename]; } + (NSString *)genFilenameWithExt:(NSString *)ext { CFUUIDRef uuid = CFUUIDCreate(nil); NSString *uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(nil, uuid); CFRelease(uuid); NSString *uuidStr = [[uuidString stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString]; NSString *name = [NSString stringWithFormat:@"%@",uuidStr]; return [ext length] ? [NSString stringWithFormat:@"%@.%@",name,ext]:name; } #pragma mark - 辅助方法 + (NSString *)filepathForDir:(NSString *)dirname filename:(NSString *)filename { return [[NIMKitFileLocationHelper resourceDir:dirname] stringByAppendingPathComponent:filename]; } @end