/// <summary> /// load sync settings, this method is called before the page loaded method /// </summary> /// <param name="syncSetting"></param> public void LoadSyncSetting(SyncSetting syncSetting) { if (syncSetting == null) { isLoadedFromRecord = false; this.syncSetting = new SyncSetting(); } else { isLoadedFromRecord = true; this.syncSetting = syncSetting; } // 初始化bucketManager var mac = new Qiniu.Util.Mac(SystemConfig.ACCESS_KEY, SystemConfig.SECRET_KEY); this.bucketManager = new BucketManager(mac); }
private void createDirCache(string localSyncDir) { DirectoryInfo di = new DirectoryInfo(localSyncDir); FileInfo[] fileInfos = di.GetFiles("*.*", SearchOption.AllDirectories); int originalCount = fileInfos.Length; List <string> files = new List <string>(); string[] keys = new string[originalCount]; bool[] skip = new bool[originalCount]; overwriteDict = new Dictionary <string, bool>(); for (int i = 0; i < originalCount; ++i) { files.Add(fileInfos[i].FullName); if (syncSetting.UseShortFilename) { keys[i] = syncSetting.SyncPrefix + fileInfos[i].Name; } else { keys[i] = syncSetting.SyncPrefix + fileInfos[i].FullName; } skip[i] = false; overwriteDict.Add(fileInfos[i].FullName, false); } // 按照前缀/后缀跳过 string[] skippedPrefixes = this.syncSetting.SkipPrefixes.Split(','); string[] skippedSuffixes = this.syncSetting.SkipSuffixes.Split(','); for (int i = 0; i < originalCount; ++i) { string shortFileName = fileInfos[i].Name; if (skip[i]) { continue; } if (shouldSkipByPrefix(shortFileName, skippedPrefixes)) { skip[i] = true; addFileSkippedLog(string.Format("{0}\t{1}", this.syncSetting.TargetBucket, files[i])); updateUploadLog("按照前缀规则跳过文件 " + files[i]); } if (skip[i]) { continue; } if (shouldSkipBySuffix(shortFileName, skippedSuffixes)) { skip[i] = true; addFileSkippedLog(string.Format("{0}\t{1}", this.syncSetting.TargetBucket, files[i])); updateUploadLog("按照后缀规则跳过文件 " + files[i]); } } // 检查本地增量文件 if (syncSetting.CheckNewFiles) { List <string> cachedKeys = CachedHash.GetAllKeys(localHashDB); Dictionary <string, string> itemDict = CachedHash.GetAllItems(localHashDB); for (int i = 0; i < originalCount; ++i) { if (skip[i]) { continue; } string f = files[i]; if (itemDict.ContainsKey(f)) { string oldHash = itemDict[f]; string newHash = Qiniu.Util.QETag.hash(f); if (string.Equals(oldHash, newHash)) { addFileExistsLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket, files[i], keys[i])); updateUploadLog("本地检查已同步,跳过文件 " + files[i]); skip[i] = true; } } } } // 检查云端同名文件 Qiniu.Util.Mac mac = new Qiniu.Util.Mac(SystemConfig.ACCESS_KEY, SystemConfig.SECRET_KEY); string[] remoteHash = BucketFileHash.BatchStat(mac, syncSetting.TargetBucket, keys); // 跳过 if (!syncSetting.OverwriteDuplicate) { for (int i = 0; i < originalCount; ++i) { if (skip[i]) { continue; } if (!string.IsNullOrEmpty(remoteHash[i])) { string localHash = Qiniu.Util.QETag.hash(files[i]); if (string.Equals(localHash, remoteHash[i])) { addFileNotOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket, files[i], keys[i])); updateUploadLog("空间已存在相同文件,跳过文件 " + files[i]); skip[i] = true; } } } } else // 覆盖 { for (int i = 0; i < originalCount; ++i) { if (skip[i]) { continue; } if (!string.IsNullOrEmpty(remoteHash[i])) { string localHash = Qiniu.Util.QETag.hash(files[i]); if (string.Equals(localHash, remoteHash[i])) { addFileNotOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket, files[i], keys[i])); updateUploadLog("空间已存在相同文件,跳过文件 " + files[i]); overwriteDict[files[i]] = true; } } } } using (StreamWriter sw = new StreamWriter(cacheFilePathDone)) { for (int i = 0; i < files.Count; ++i) { if (!skip[i]) { sw.WriteLine(files[i]); } } } }
private void createDirCache(string localSyncDir) { DirectoryInfo di = new DirectoryInfo(localSyncDir); FileInfo[] fileInfos = di.GetFiles("*.*", SearchOption.AllDirectories); int originalCount = fileInfos.Length; List<string> files = new List<string>(); string[] keys = new string[originalCount]; bool[] skip = new bool[originalCount]; overwriteDict = new Dictionary<string, bool>(); for(int i=0;i<originalCount;++i) { files.Add(fileInfos[i].FullName); if(syncSetting.UseShortFilename) { keys[i] = syncSetting.SyncPrefix + fileInfos[i].Name; } else { keys[i] = syncSetting.SyncPrefix + fileInfos[i].FullName; } skip[i] = false; overwriteDict.Add(fileInfos[i].FullName, false); } // 按照前缀/后缀跳过 string[] skippedPrefixes = this.syncSetting.SkipPrefixes.Split(','); string[] skippedSuffixes = this.syncSetting.SkipSuffixes.Split(','); for (int i = 0; i < originalCount; ++i) { string shortFileName = fileInfos[i].Name; if (skip[i]) continue; if (shouldSkipByPrefix(shortFileName, skippedPrefixes)) { skip[i] = true; addFileSkippedLog(string.Format("{0}\t{1}", this.syncSetting.TargetBucket,files[i])); updateUploadLog("按照前缀规则跳过文件 " + files[i]); } if (skip[i]) continue; if (shouldSkipBySuffix(shortFileName, skippedSuffixes)) { skip[i] = true; addFileSkippedLog(string.Format("{0}\t{1}", this.syncSetting.TargetBucket, files[i])); updateUploadLog("按照后缀规则跳过文件 " + files[i]); } } // 检查本地增量文件 if (syncSetting.CheckNewFiles) { List<string> cachedKeys = CachedHash.GetAllKeys(localHashDB); Dictionary<string, string> itemDict = CachedHash.GetAllItems(localHashDB); for (int i = 0; i < originalCount; ++i) { if (skip[i]) continue; string f = files[i]; if (itemDict.ContainsKey(f)) { string oldHash = itemDict[f]; string newHash = Qiniu.Util.QETag.hash(f); if (string.Equals(oldHash, newHash)) { addFileExistsLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket, files[i], keys[i])); updateUploadLog("本地检查已同步,跳过文件 " + files[i]); skip[i] = true; } } } } // 检查云端同名文件 Qiniu.Util.Mac mac = new Qiniu.Util.Mac(SystemConfig.ACCESS_KEY, SystemConfig.SECRET_KEY); string[] remoteHash = BucketFileHash.BatchStat(mac, syncSetting.TargetBucket, keys); // 跳过 if (!syncSetting.OverwriteDuplicate) { for (int i = 0; i < originalCount; ++i) { if (skip[i]) continue; if (!string.IsNullOrEmpty(remoteHash[i])) { string localHash = Qiniu.Util.QETag.hash(files[i]); if (string.Equals(localHash, remoteHash[i])) { addFileNotOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket, files[i], keys[i])); updateUploadLog("空间已存在相同文件,跳过文件 " + files[i]); skip[i] = true; } } } } else // 覆盖 { for (int i = 0; i < originalCount; ++i) { if (skip[i]) continue; if (!string.IsNullOrEmpty(remoteHash[i])) { string localHash = Qiniu.Util.QETag.hash(files[i]); if (string.Equals(localHash, remoteHash[i])) { addFileNotOverwriteLog(string.Format("{0}\t{1}\t{2}", this.syncSetting.TargetBucket, files[i], keys[i])); updateUploadLog("空间已存在相同文件,跳过文件 " + files[i]); overwriteDict[files[i]] = true; } } } } using (StreamWriter sw = new StreamWriter(cacheFilePathDone)) { for (int i = 0; i < files.Count; ++i) { if (!skip[i]) { sw.WriteLine(files[i]); } } } }