NIMKitFileLocationHelper.m
4.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
//
// NIMKitFileLocationHelper.m
// NIMKit
//
// Created by chris on 2016/11/12.
// Copyright © 2016年 NetEase. All rights reserved.
//
#import "NIMKitFileLocationHelper.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, ^{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
appDocumentPath= [[NSString alloc]initWithFormat:@"%@/",[paths objectAtIndex:0]];
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];
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