123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- //
- // NIMKitFileLocationHelper.m
- // NIMKit
- //
- // Created by chris on 2016/11/12.
- // Copyright © 2016年 NetEase. All rights reserved.
- //
- #import "NIMKitFileLocationHelper.h"
- #import <NIMSDK/NIMSDK.h>
- #import <sys/stat.h>
- @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
|