JCHATFileManager.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // JMSGFileManager.m
  3. // PAChat
  4. //
  5. // Created by xiao on 9/5/13.
  6. // Copyright (c) 2013 FreeDo. All rights reserved.
  7. //
  8. #import "JCHATFileManager.h"
  9. static NSString *filePath = nil;
  10. @implementation JCHATFileManager
  11. + (BOOL)initWithFilePath{
  12. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  13. NSString *docDir = paths[0];
  14. filePath = docDir;
  15. [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/image",filePath] withIntermediateDirectories:YES attributes:nil error:nil];
  16. [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/audio",filePath] withIntermediateDirectories:YES attributes:nil error:nil];
  17. [[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/video",filePath] withIntermediateDirectories:YES attributes:nil error:nil];
  18. return YES;
  19. }
  20. //type is the messageType(etc.voice,image), fileType is type of file,(etc. jpg for image)
  21. + (NSString *)generatePathWithConversationID:(NSString *)conID withMessageType:(FILE_TYPE)type withFileType:(NSString *)fileType{
  22. NSString *pathType;
  23. NSString *suffix;
  24. switch (type){
  25. case FILE_IMAGE:
  26. pathType = @"image";
  27. if (!fileType){
  28. suffix = @".jpg";
  29. } else{
  30. suffix = [NSString stringWithFormat:@".%@",fileType];
  31. }
  32. break;
  33. case FILE_AUDIO:
  34. pathType = @"voice";
  35. if (!fileType){
  36. suffix = @".mp3";
  37. } else{
  38. suffix = [NSString stringWithFormat:@".%@",fileType];
  39. }
  40. break;
  41. default:
  42. break;
  43. }
  44. CFUUIDRef udid = CFUUIDCreate(NULL);
  45. NSString *desPath = [NSString stringWithFormat:@"%@/%@/%@/%@%@",filePath,pathType,conID,(__bridge_transfer NSString *) CFUUIDCreateString(NULL, udid),suffix];
  46. CFRelease(udid);
  47. return desPath;
  48. }
  49. + (BOOL)saveToPath:(NSString *)path withData:(NSData *)data {
  50. BOOL success = NO;
  51. if (data) {
  52. NSFileManager *file = [NSFileManager defaultManager];
  53. NSString *pathDir = [path stringByDeletingLastPathComponent];
  54. if ([file createDirectoryAtPath:pathDir withIntermediateDirectories:YES attributes:nil error:nil]) {
  55. success = [file createFileAtPath:path contents:data attributes:nil];
  56. }
  57. }
  58. return success;
  59. }
  60. + (NSString*)saveImageWithConversationID:(NSString*)conID andData:(NSData *)imgData{
  61. CFUUIDRef udid = CFUUIDCreate(NULL);
  62. NSString *desPath = [NSString stringWithFormat:@"%@/image/%@/%@.jpg",filePath,conID,(__bridge_transfer NSString *) CFUUIDCreateString(NULL, udid)];
  63. CFRelease(udid);
  64. if (imgData) {
  65. NSFileManager *file = [NSFileManager defaultManager];
  66. BOOL success = NO;
  67. if ([file createDirectoryAtPath:[NSString stringWithFormat:@"%@/image/%@",filePath,conID] withIntermediateDirectories:YES attributes:nil error:nil]) {
  68. success = [file createFileAtPath:desPath contents:imgData attributes:nil];
  69. }
  70. if (success) {
  71. return desPath;
  72. }
  73. }
  74. return nil;
  75. }
  76. + (NSString*)saveChatBackgroundImageWithConversationID:(NSString*)conID andData:(NSData *)imgData{
  77. NSString *desPath = [NSString stringWithFormat:@"%@/setting/image/%@/back.png",filePath,conID];
  78. if (imgData) {
  79. NSFileManager *file = [NSFileManager defaultManager];
  80. BOOL success = NO;
  81. if ([file createDirectoryAtPath:[NSString stringWithFormat:@"%@/setting/image/%@",filePath,conID] withIntermediateDirectories:YES attributes:nil error:nil]) {
  82. success = [file createFileAtPath:desPath contents:imgData attributes:nil];
  83. }
  84. if (success) {
  85. return desPath;
  86. }
  87. }
  88. return nil;
  89. }
  90. + (NSString *)copyFile:(NSString *)sourepath withType:(FILE_TYPE)type From:(NSString *)sourceID to:(NSString *)destinationID {
  91. NSError* error;
  92. NSString *desPath= nil;
  93. switch (type) {
  94. case FILE_IMAGE:
  95. {
  96. desPath = [NSString stringWithFormat:@"%@/image/%@/%@",filePath,destinationID,[sourepath lastPathComponent]];
  97. }
  98. break;
  99. case FILE_AUDIO:
  100. {
  101. desPath = [NSString stringWithFormat:@"%@/audio/%@/%@",filePath,destinationID,[sourepath lastPathComponent]];
  102. }
  103. break;
  104. case FILE_VIDIO:
  105. {
  106. desPath = [NSString stringWithFormat:@"%@/video/%@/%@",filePath,destinationID,[sourepath lastPathComponent]];
  107. }
  108. break;
  109. default:
  110. break;
  111. }
  112. if (!desPath) {
  113. NSLog(@"保存文件地址错误");
  114. assert(desPath);
  115. return @"";
  116. }
  117. NSFileManager *file = [NSFileManager defaultManager];
  118. if ([file createDirectoryAtPath:[desPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil]) {
  119. if ([file fileExistsAtPath:desPath]) {
  120. NSString *fileName = [desPath lastPathComponent];
  121. CFUUIDRef udid = CFUUIDCreate(NULL);
  122. fileName = [NSString stringWithFormat:@"%@_%@",(__bridge_transfer NSString *) CFUUIDCreateString(NULL, udid),fileName];
  123. desPath = [[desPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:fileName];
  124. }
  125. [file copyItemAtPath:sourepath toPath:desPath error:&error];
  126. }
  127. if (error != nil) {
  128. NSLog(@"Error message is %@", [error localizedDescription]);
  129. return @"";
  130. }
  131. return desPath;
  132. }
  133. + (BOOL)deleteFile:(NSString *)path{
  134. NSError* error;
  135. if (path.length) {
  136. NSFileManager *fileManager = [NSFileManager defaultManager];
  137. [fileManager removeItemAtPath:path error:&error];
  138. if (error != nil) {
  139. NSLog(@"Error message is %@", [error localizedDescription]);
  140. return NO;
  141. }
  142. return YES;
  143. }
  144. return NO;
  145. }
  146. + (NSString*)saveGlobalBackGround:(NSData *)imgData{
  147. NSString *desPath = [NSString stringWithFormat:@"%@/setting/image/globalback.png",filePath];
  148. if (imgData) {
  149. NSFileManager *file = [NSFileManager defaultManager];
  150. BOOL success = NO;
  151. if ([file createDirectoryAtPath:[NSString stringWithFormat:@"%@/setting/image",filePath] withIntermediateDirectories:YES attributes:nil error:nil]) {
  152. success = [file createFileAtPath:desPath contents:imgData attributes:nil];
  153. }
  154. if (success) {
  155. return desPath;
  156. }
  157. }
  158. return nil;
  159. }
  160. //清空个人用户单个会话下所有下载文件
  161. + (void)deletAllFilesByConversationID:(NSString *)conversationID{
  162. NSString *imagePath = [NSString stringWithFormat:@"%@/image/%@",filePath,conversationID];
  163. NSString *audioPath = [NSString stringWithFormat:@"%@/audio/%@",filePath,conversationID];
  164. NSString *videoPath = [NSString stringWithFormat:@"%@/video/%@",filePath,conversationID];
  165. [[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];
  166. [[NSFileManager defaultManager] removeItemAtPath:audioPath error:nil];
  167. [[NSFileManager defaultManager] removeItemAtPath:videoPath error:nil];
  168. }
  169. //清空个人用户所有下载文件和db
  170. + (void)deletAllFiles{
  171. NSString *imagePath = [NSString stringWithFormat:@"%@/image",filePath];
  172. NSString *audioPath = [NSString stringWithFormat:@"%@/audio",filePath];
  173. NSString *videoPath = [NSString stringWithFormat:@"%@/video",filePath];
  174. [[NSFileManager defaultManager] removeItemAtPath:imagePath error:nil];
  175. [[NSFileManager defaultManager] removeItemAtPath:audioPath error:nil];
  176. [[NSFileManager defaultManager] removeItemAtPath:videoPath error:nil];
  177. }
  178. //清空documnet 目录
  179. + (void)deletAllFilesAtDocument{
  180. NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  181. [[NSFileManager defaultManager] removeItemAtPath:[paths objectAtIndex:0] error:nil];
  182. }
  183. @end