|
@@ -0,0 +1,232 @@
|
|
|
+//
|
|
|
+// AutoSaveManager.m
|
|
|
+// PDF Reader Pro Edition
|
|
|
+//
|
|
|
+// Created by Niehaoyu on 2023/08/10.
|
|
|
+//
|
|
|
+
|
|
|
+#import "AutoSaveManager.h"
|
|
|
+
|
|
|
+@interface AutoSaveManager ()
|
|
|
+
|
|
|
+@property (nonatomic, strong) NSMutableDictionary *infoDict;
|
|
|
+
|
|
|
+@property (nonatomic, strong, readwrite) NSMutableArray *originalPaths;//保存的原文件信息
|
|
|
+
|
|
|
+@property (nonatomic, strong, readwrite) NSMutableArray *autoSavePaths;
|
|
|
+
|
|
|
+@property (nonatomic, copy, readwrite) NSString *autoSaveFolder;
|
|
|
+
|
|
|
+@end
|
|
|
+
|
|
|
+@implementation AutoSaveManager
|
|
|
+
|
|
|
++ (AutoSaveManager *)manager {
|
|
|
+ static AutoSaveManager *__manager = nil;
|
|
|
+ if (!__manager) {
|
|
|
+ __manager = [[super allocWithZone:NULL] init];
|
|
|
+ }
|
|
|
+ return __manager;
|
|
|
+}
|
|
|
+
|
|
|
++ (instancetype)allocWithZone:(struct _NSZone *)zone {
|
|
|
+ return AutoSaveManager.manager;
|
|
|
+}
|
|
|
+
|
|
|
+- (id)init {
|
|
|
+ self = [super init];
|
|
|
+ if (self) {
|
|
|
+
|
|
|
+ self.autoSaveEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"ComAutoSaveKey"];
|
|
|
+
|
|
|
+ if ([[NSUserDefaults standardUserDefaults] floatForKey:@"AutoSaveTimeIntervalKey"]) {
|
|
|
+ self.timeInterval = [[NSUserDefaults standardUserDefaults] floatForKey:@"AutoSaveTimeIntervalKey"];
|
|
|
+ } else {
|
|
|
+ self.timeInterval = 15;
|
|
|
+ }
|
|
|
+
|
|
|
+ [self loadData];
|
|
|
+
|
|
|
+ self.autoSaveDidEndAction = YES;
|
|
|
+
|
|
|
+ }
|
|
|
+ return self;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)loadData {
|
|
|
+
|
|
|
+ NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
|
|
|
+
|
|
|
+ if ([[NSFileManager defaultManager] fileExistsAtPath:[cachesPath stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier]]) {
|
|
|
+ cachesPath = [cachesPath stringByAppendingPathComponent:[NSBundle mainBundle].bundleIdentifier];
|
|
|
+ }
|
|
|
+ cachesPath = [cachesPath stringByAppendingPathComponent:@"autoSaveFolder"];
|
|
|
+ if (![[NSFileManager defaultManager] fileExistsAtPath:cachesPath]) {
|
|
|
+ [[NSFileManager defaultManager] createDirectoryAtPath:cachesPath withIntermediateDirectories:YES attributes:nil error:nil];
|
|
|
+ }
|
|
|
+ self.autoSaveFolder = cachesPath;
|
|
|
+
|
|
|
+#if DEBUG
|
|
|
+ NSLog(@"autoSaveFolder===%@",cachesPath);
|
|
|
+#endif
|
|
|
+
|
|
|
+ NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",self.autoSaveFolder];
|
|
|
+ NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
|
|
|
+ if (dict.allKeys.count == 0) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ self.infoDict = dict;
|
|
|
+
|
|
|
+ if (!self.autoSavePaths) {
|
|
|
+ self.autoSavePaths = [[NSMutableArray alloc] init];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!self.originalPaths) {
|
|
|
+ self.originalPaths = [[NSMutableArray alloc] init];
|
|
|
+ }
|
|
|
+ [self.originalPaths addObjectsFromArray:dict.allKeys];
|
|
|
+
|
|
|
+ self.opendPaths = [[NSMutableArray alloc] init];
|
|
|
+
|
|
|
+ NSArray *arr = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:self.autoSaveFolder error:nil];
|
|
|
+ for (NSString *fileName in arr) {
|
|
|
+ NSString *savedKey = [self.autoSaveFolder stringByAppendingPathComponent:fileName];
|
|
|
+ if ([dict.allValues containsObject:savedKey]) {
|
|
|
+ [self.autoSavePaths addObject:savedKey];
|
|
|
+ }else {
|
|
|
+ if ([fileName isEqualToString:@"autoSaveInfo.plist"]) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ [[NSFileManager defaultManager] removeItemAtPath:savedKey error:nil];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Setter
|
|
|
+- (void)setAutoSaveEnabled:(BOOL)autoSaveEnabled {
|
|
|
+ _autoSaveEnabled = autoSaveEnabled;
|
|
|
+
|
|
|
+ [[NSUserDefaults standardUserDefaults] setBool:_autoSaveEnabled forKey:@"ComAutoSaveKey"];
|
|
|
+ [[NSUserDefaults standardUserDefaults] synchronize];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)setTimeInterval:(CGFloat)timeInterval {
|
|
|
+ _timeInterval = timeInterval;
|
|
|
+
|
|
|
+ [[NSUserDefaults standardUserDefaults] setFloat:_timeInterval forKey:@"AutoSaveTimeIntervalKey"];
|
|
|
+ [[NSUserDefaults standardUserDefaults] synchronize];
|
|
|
+}
|
|
|
+
|
|
|
+- (NSString *)URLEncodedString:(NSString *)string {
|
|
|
+ CFStringRef stringRef = CFBridgingRetain(string);
|
|
|
+#pragma clang diagnostic push
|
|
|
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
|
|
+ CFStringRef encoded = CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,
|
|
|
+ stringRef,
|
|
|
+ NULL,
|
|
|
+ CFSTR("!*'\"();:@&=+$,/?%#[]% "),
|
|
|
+ kCFStringEncodingUTF8);
|
|
|
+#pragma clang diagnostic pop
|
|
|
+ CFRelease(stringRef);
|
|
|
+ return CFBridgingRelease(encoded);
|
|
|
+}
|
|
|
+
|
|
|
+- (NSString *)getValidFilePath:(NSString *)oldPath {
|
|
|
+ NSFileManager *fileManager = [NSFileManager defaultManager];
|
|
|
+ NSDictionary *fileDic = [fileManager attributesOfItemAtPath:oldPath error:nil];
|
|
|
+ NSString *fileType = [fileDic objectForKey:NSFileType];
|
|
|
+ int i =1;
|
|
|
+ NSString *newPath = oldPath;
|
|
|
+ while ([fileManager fileExistsAtPath:newPath]) {
|
|
|
+ if ([fileType isEqualToString:NSFileTypeDirectory]) {
|
|
|
+ newPath = [oldPath stringByAppendingFormat:@"(%d)",i];
|
|
|
+ } else {
|
|
|
+ NSString *fileExtension = [oldPath pathExtension];
|
|
|
+ newPath = [[oldPath stringByDeletingPathExtension] stringByAppendingFormat:@"(%d).%@",i,fileExtension];
|
|
|
+ }
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ return newPath;
|
|
|
+}
|
|
|
+
|
|
|
+#pragma mark - Public Method
|
|
|
+- (void)clearCache {
|
|
|
+ if ([[NSFileManager defaultManager] fileExistsAtPath:self.autoSaveFolder]) {
|
|
|
+ [[NSFileManager defaultManager] removeItemAtPath:self.autoSaveFolder error:nil];
|
|
|
+ }
|
|
|
+ if (![[NSFileManager defaultManager] fileExistsAtPath:self.autoSaveFolder]) {
|
|
|
+ [[NSFileManager defaultManager] createDirectoryAtPath:self.autoSaveFolder withIntermediateDirectories:YES attributes:nil error:nil];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (NSString *)autoSaveWithPath:(NSString *)filePath {
|
|
|
+
|
|
|
+ NSString *autoSaveFolder = self.autoSaveFolder;
|
|
|
+
|
|
|
+ NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",autoSaveFolder];
|
|
|
+ NSString *savePath = [NSString stringWithFormat:@"%@/%@_%@.%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",filePath.pathExtension];
|
|
|
+
|
|
|
+ if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
|
|
|
+ NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
|
|
|
+ if ([dict.allKeys containsObject:filePath]) {
|
|
|
+ savePath = [dict valueForKey:filePath];
|
|
|
+ }
|
|
|
+ if (savePath.length > 0 && filePath.length > 0) {
|
|
|
+ if ([dict.allValues containsObject:savePath]) {
|
|
|
+ //不同路径下同名文件的保存覆盖的情况处理
|
|
|
+ NSArray *values = [[NSArray alloc] initWithArray:dict.allValues];
|
|
|
+
|
|
|
+ for (NSString *key in dict.allKeys) {
|
|
|
+ NSString *value = [dict objectForKey:key];
|
|
|
+ if ([savePath isEqualToString:value] &&
|
|
|
+ ![key isEqualToString:filePath]) {
|
|
|
+ int count = 1;
|
|
|
+ savePath = [NSString stringWithFormat:@"%@/%@_%@(%d).%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",count,filePath.pathExtension];
|
|
|
+ while ([values containsObject:savePath]) {
|
|
|
+ count ++;
|
|
|
+ savePath = [NSString stringWithFormat:@"%@/%@_%@(%d).%@",autoSaveFolder,filePath.lastPathComponent.stringByDeletingPathExtension,@"recovered",count,filePath.pathExtension];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ [dict setValue:savePath forKey:filePath];
|
|
|
+ }
|
|
|
+ [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
|
|
|
+ } else {
|
|
|
+ NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
|
|
|
+ if (savePath.length > 0 && filePath.length > 0) {
|
|
|
+ [dict setValue:savePath forKey:filePath];
|
|
|
+ }
|
|
|
+ [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
|
|
|
+
|
|
|
+ }
|
|
|
+ return savePath;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)removeAutoSavePath:(NSString *)filePath {
|
|
|
+
|
|
|
+ NSString *autoSaveFolder = self.autoSaveFolder;
|
|
|
+
|
|
|
+ NSString *plistPath = [NSString stringWithFormat:@"%@/autoSaveInfo.plist",autoSaveFolder];
|
|
|
+ if ([[NSFileManager defaultManager] fileExistsAtPath:plistPath]) {
|
|
|
+ NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
|
|
|
+ if ([dict valueForKey:filePath]) {
|
|
|
+ NSString *savePath = [dict valueForKey:filePath];
|
|
|
+ if ([[NSFileManager defaultManager] fileExistsAtPath:savePath]) {
|
|
|
+ [[NSFileManager defaultManager] removeItemAtPath:savePath error:nil];
|
|
|
+ }
|
|
|
+ [dict removeObjectForKey:filePath];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (dict.allKeys.count > 0) {
|
|
|
+ [dict writeToURL:[NSURL fileURLWithPath:plistPath] error:nil];
|
|
|
+ } else {
|
|
|
+ [[NSFileManager defaultManager] removeItemAtPath:plistPath error:nil];
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|
|
|
+@end
|