NTESFileLocationHelper.m 4.2 KB

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