NIMKitFileLocationHelper.m 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. //
  2. // NIMKitFileLocationHelper.m
  3. // NIMKit
  4. //
  5. // Created by chris on 2016/11/12.
  6. // Copyright © 2016年 NetEase. All rights reserved.
  7. //
  8. #import "NIMKitFileLocationHelper.h"
  9. #import <NIMSDK/NIMSDK.h>
  10. #import <sys/stat.h>
  11. @interface NIMKitFileLocationHelper ()
  12. + (NSString *)filepathForDir: (NSString *)dirname filename: (NSString *)filename;
  13. @end
  14. @implementation NIMKitFileLocationHelper
  15. + (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
  16. {
  17. assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
  18. NSError *error = nil;
  19. BOOL success = [URL setResourceValue:@(YES)
  20. forKey:NSURLIsExcludedFromBackupKey
  21. error:&error];
  22. if(!success)
  23. {
  24. NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
  25. }
  26. return success;
  27. }
  28. + (NSString *)getAppDocumentPath
  29. {
  30. static NSString *appDocumentPath = nil;
  31. static dispatch_once_t onceToken;
  32. dispatch_once(&onceToken, ^{
  33. NSString *appKey = [NIMSDK sharedSDK].appKey;
  34. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  35. appDocumentPath= [[NSString alloc]initWithFormat:@"%@/%@/",[paths objectAtIndex:0],appKey];
  36. if (![[NSFileManager defaultManager] fileExistsAtPath:appDocumentPath])
  37. {
  38. [[NSFileManager defaultManager] createDirectoryAtPath:appDocumentPath
  39. withIntermediateDirectories:NO
  40. attributes:nil
  41. error:nil];
  42. }
  43. [NIMKitFileLocationHelper addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:appDocumentPath]];
  44. });
  45. return appDocumentPath;
  46. }
  47. + (NSString *)getAppTempPath
  48. {
  49. return NSTemporaryDirectory();
  50. }
  51. + (NSString *)userDirectory
  52. {
  53. NSString *documentPath = [NIMKitFileLocationHelper getAppDocumentPath];
  54. NSString *userID = [NIMSDK sharedSDK].loginManager.currentAccount;
  55. if ([userID length] == 0)
  56. {
  57. NSLog(@"Error: Get User Directory While UserID Is Empty");
  58. }
  59. NSString* userDirectory= [NSString stringWithFormat:@"%@%@/",documentPath,userID];
  60. if (![[NSFileManager defaultManager] fileExistsAtPath:userDirectory])
  61. {
  62. [[NSFileManager defaultManager] createDirectoryAtPath:userDirectory
  63. withIntermediateDirectories:NO
  64. attributes:nil
  65. error:nil];
  66. }
  67. return userDirectory;
  68. }
  69. + (NSString *)resourceDir: (NSString *)resouceName
  70. {
  71. NSString *dir = [[NIMKitFileLocationHelper userDirectory] stringByAppendingPathComponent:resouceName];
  72. if (![[NSFileManager defaultManager] fileExistsAtPath:dir])
  73. {
  74. [[NSFileManager defaultManager] createDirectoryAtPath:dir
  75. withIntermediateDirectories:NO
  76. attributes:nil
  77. error:nil];
  78. }
  79. return dir;
  80. }
  81. + (NSString *)filepathForVideo:(NSString *)filename
  82. {
  83. return [NIMKitFileLocationHelper filepathForDir:@"video"
  84. filename:filename];
  85. }
  86. + (NSString *)filepathForImage:(NSString *)filename
  87. {
  88. return [NIMKitFileLocationHelper filepathForDir:@"image"
  89. filename:filename];
  90. }
  91. + (NSString *)genFilenameWithExt:(NSString *)ext
  92. {
  93. CFUUIDRef uuid = CFUUIDCreate(nil);
  94. NSString *uuidString = (__bridge_transfer NSString*)CFUUIDCreateString(nil, uuid);
  95. CFRelease(uuid);
  96. NSString *uuidStr = [[uuidString stringByReplacingOccurrencesOfString:@"-" withString:@""] lowercaseString];
  97. NSString *name = [NSString stringWithFormat:@"%@",uuidStr];
  98. return [ext length] ? [NSString stringWithFormat:@"%@.%@",name,ext]:name;
  99. }
  100. #pragma mark - 辅助方法
  101. + (NSString *)filepathForDir:(NSString *)dirname
  102. filename:(NSString *)filename
  103. {
  104. return [[NIMKitFileLocationHelper resourceDir:dirname] stringByAppendingPathComponent:filename];
  105. }
  106. @end