AutoSaveManager.m 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // AutoSaveManager.m
  3. // PDF Reader Pro Edition
  4. //
  5. // Created by Niehaoyu on 2023/08/10.
  6. //
  7. #import "AutoSaveManager.h"
  8. @interface AutoSaveManager ()
  9. @property (nonatomic, strong) NSMutableDictionary *infoDict;
  10. @property (nonatomic, strong, readwrite) NSMutableArray *originalPaths;//保存的原文件信息
  11. @property (nonatomic, strong, readwrite) NSMutableArray *autoSavePaths;
  12. @property (nonatomic, copy, readwrite) NSString *autoSaveFolder;
  13. @end
  14. @implementation AutoSaveManager
  15. + (AutoSaveManager *)manager {
  16. static AutoSaveManager *__manager = nil;
  17. if (!__manager) {
  18. __manager = [[super allocWithZone:NULL] init];
  19. }
  20. return __manager;
  21. }
  22. + (instancetype)allocWithZone:(struct _NSZone *)zone {
  23. return AutoSaveManager.manager;
  24. }
  25. - (id)init {
  26. self = [super init];
  27. if (self) {
  28. self.autoSaveEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"ComAutoSaveKey"];
  29. if ([[NSUserDefaults standardUserDefaults] floatForKey:@"AutoSaveTimeIntervalKey"]) {
  30. self.timeInterval = [[NSUserDefaults standardUserDefaults] floatForKey:@"AutoSaveTimeIntervalKey"];
  31. } else {
  32. self.timeInterval = 15;
  33. }
  34. [self loadData];
  35. self.autoSaveDidEndAction = YES;
  36. }
  37. return self;
  38. }
  39. - (void)loadData {
  40. NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  41. if ([[NSFileManager defaultManager] fileExistsAtPath:[cachesPath stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier]]) {
  42. cachesPath = [cachesPath stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier];
  43. }
  44. cachesPath = [cachesPath stringByAppendingPathComponent:@"autoSaveFolder"];
  45. if (![[NSFileManager defaultManager] fileExistsAtPath:cachesPath]) {
  46. [[NSFileManager defaultManager] createDirectoryAtPath:cachesPath withIntermediateDirectories:YES attributes:nil error:nil];
  47. }
  48. self.autoSaveFolder = cachesPath;
  49. #if DEBUG
  50. NSLog(@"autoSaveFolder===%@",cachesPath);
  51. #endif
  52. NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",self.autoSaveFolder];
  53. NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
  54. if (dict.allKeys.count == 0) {
  55. return;
  56. }
  57. self.infoDict = dict;
  58. if (!self.autoSavePaths) {
  59. self.autoSavePaths = [[NSMutableArray alloc] init];
  60. }
  61. if (!self.originalPaths) {
  62. self.originalPaths = [[NSMutableArray alloc] init];
  63. }
  64. [self.originalPaths addObjectsFromArray:dict.allKeys];
  65. self.opendPaths = [[NSMutableArray alloc] init];
  66. NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.autoSaveFolder error:nil];
  67. for (NSString *fileName in arr) {
  68. NSString *savedKey = [self.autoSaveFolder stringByAppendingPathComponent:fileName];
  69. if ([dict.allValues containsObject:savedKey]) {
  70. [self.autoSavePaths addObject:savedKey];
  71. }else {
  72. if ([fileName isEqualToString:@"autoSaveInfo.plist"]) {
  73. } else {
  74. [[NSFileManager defaultManager] removeItemAtPath:savedKey error:nil];
  75. }
  76. }
  77. }
  78. }
  79. #pragma mark - Setter
  80. - (void)setAutoSaveEnabled:(BOOL)autoSaveEnabled {
  81. _autoSaveEnabled = autoSaveEnabled;
  82. [[NSUserDefaults standardUserDefaults] setBool:_autoSaveEnabled forKey:@"ComAutoSaveKey"];
  83. [[NSUserDefaults standardUserDefaults] synchronize];
  84. }
  85. - (void)setTimeInterval:(CGFloat)timeInterval {
  86. _timeInterval = timeInterval;
  87. [[NSUserDefaults standardUserDefaults] setFloat:_timeInterval forKey:@"AutoSaveTimeIntervalKey"];
  88. [[NSUserDefaults standardUserDefaults] synchronize];
  89. }
  90. - (NSString *)URLEncodedString:(NSString *)string {
  91. CFStringRef stringRef = CFBridgingRetain(string);
  92. #pragma clang diagnostic push
  93. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  94. CFStringRef encoded = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
  95. stringRef,
  96. NULL,
  97. CFSTR("!*'\"();:@&=+$,/?%#[]% "),
  98. kCFStringEncodingUTF8);
  99. #pragma clang diagnostic pop
  100. CFRelease(stringRef);
  101. return CFBridgingRelease(encoded);
  102. }
  103. - (NSString *)getValidFilePath:(NSString *)oldPath {
  104. NSFileManager *fileManager = [NSFileManager defaultManager];
  105. NSDictionary *fileDic = [fileManager attributesOfItemAtPath:oldPath error:nil];
  106. NSString *fileType = [fileDic objectForKey:NSFileType];
  107. int i =1;
  108. NSString *newPath = oldPath;
  109. while ([fileManager fileExistsAtPath:newPath]) {
  110. if ([fileType isEqualToString:NSFileTypeDirectory]) {
  111. newPath = [oldPath stringByAppendingFormat:@"(%d)",i];
  112. } else {
  113. NSString *fileExtension = [oldPath pathExtension];
  114. newPath = [[oldPath stringByDeletingPathExtension] stringByAppendingFormat:@"(%d).%@",i,fileExtension];
  115. }
  116. i++;
  117. }
  118. return newPath;
  119. }
  120. #pragma mark - Public Method
  121. - (void)clearCache {
  122. if ([[NSFileManager defaultManager] fileExistsAtPath:self.autoSaveFolder]) {
  123. [[NSFileManager defaultManager] removeItemAtPath:self.autoSaveFolder error:nil];
  124. }
  125. if (![[NSFileManager defaultManager] fileExistsAtPath:self.autoSaveFolder]) {
  126. [[NSFileManager defaultManager] createDirectoryAtPath:self.autoSaveFolder withIntermediateDirectories:YES attributes:nil error:nil];
  127. }
  128. }
  129. - (NSString *)autoSaveWithPath:(NSString *)filePath {
  130. NSString *autoSaveFolder = self.autoSaveFolder;
  131. NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",autoSaveFolder];
  132. NSString *savePath = [NSString stringWithFormat:@"%@/%@_%@.%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",filePath.pathExtension];
  133. if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
  134. NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
  135. if ([dict.allKeys containsObject:filePath]) {
  136. savePath = [dict valueForKey:filePath];
  137. }
  138. if (savePath.length > 0 && filePath.length > 0) {
  139. if ([dict.allValues containsObject:savePath]) {
  140. //不同路径下同名文件的保存覆盖的情况处理
  141. NSArray *values = [[NSArray alloc] initWithArray:dict.allValues];
  142. for (NSString *key in dict.allKeys) {
  143. NSString *value = [dict objectForKey:key];
  144. if ([savePath isEqualToString:value] &&
  145. ![key isEqualToString:filePath]) {
  146. int count = 1;
  147. savePath = [NSString stringWithFormat:@"%@/%@_%@(%d).%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",count,filePath.pathExtension];
  148. while ([values containsObject:savePath]) {
  149. count ++;
  150. savePath = [NSString stringWithFormat:@"%@/%@_%@(%d).%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",count,filePath.pathExtension];
  151. }
  152. }
  153. }
  154. }
  155. [dict setValue:savePath forKey:filePath];
  156. }
  157. [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
  158. } else {
  159. NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
  160. if (savePath.length > 0 && filePath.length > 0) {
  161. [dict setValue:savePath forKey:filePath];
  162. }
  163. [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
  164. }
  165. return savePath;
  166. }
  167. - (void)removeAutoSavePath:(NSString *)filePath {
  168. NSString *autoSaveFolder = self.autoSaveFolder;
  169. NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",autoSaveFolder];
  170. if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
  171. NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
  172. if ([dict valueForKey:filePath]) {
  173. NSString *savePath = [dict valueForKey:filePath];
  174. if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) {
  175. [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
  176. }
  177. [dict removeObjectForKey:filePath];
  178. }
  179. if (dict.allKeys.count > 0) {
  180. [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
  181. } else {
  182. [[NSFileManager defaultManager] removeItemAtPath:plistPath error:nil];
  183. }
  184. }
  185. }
  186. @end