//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在RRQMCore.XREF命名空间的代码)归作者本人若汝棋茗所有 // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权 // CSDN博客:https://blog.csdn.net/qq_40374647 // 哔哩哔哩视频:https://space.bilibili.com/94253567 // Gitee源代码仓库:https://gitee.com/RRQM_Home // Github源代码仓库:https://github.com/RRQM // 交流QQ群:234762506 // 感谢您的下载和使用 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ using System.Collections.Concurrent; using System.IO; namespace RRQMSocket.FileTransfer { /// /// 传输文件Hash暂存字典 /// public static class TransferFileHashDictionary { private static ConcurrentDictionary fileHashAndInfo = new ConcurrentDictionary(); private static ConcurrentDictionary filePathAndInfo = new ConcurrentDictionary(); /// /// 字典存储文件Hash的最大数量,默认为10000 /// public static int MaxCount { get; set; } = 10000; /// /// 添加文件信息 /// /// /// /// public static UrlFileInfo AddFile(string filePath, bool breakpointResume = true) { UrlFileInfo urlFileInfo = new UrlFileInfo(); using (FileStream stream = File.OpenRead(filePath)) { urlFileInfo.FilePath = filePath; urlFileInfo.FileLength = stream.Length; urlFileInfo.FileName = Path.GetFileName(filePath); if (breakpointResume) { urlFileInfo.FileHash = FileHashGenerator.GetFileHash(stream); } } AddFile(urlFileInfo); return urlFileInfo; } /// /// 添加文件信息 /// /// public static void AddFile(UrlFileInfo urlFileInfo) { if (urlFileInfo == null) { return; } filePathAndInfo.AddOrUpdate(urlFileInfo.FilePath, urlFileInfo, (key, oldValue) => { return urlFileInfo; }); if (!string.IsNullOrEmpty(urlFileInfo.FileHash)) { fileHashAndInfo.AddOrUpdate(urlFileInfo.FileHash, urlFileInfo, (key, oldValue) => { return urlFileInfo; }); } if (filePathAndInfo.Count > MaxCount) { foreach (var item in filePathAndInfo.Keys) { if (filePathAndInfo.TryRemove(item, out _)) { break; } } } if (fileHashAndInfo.Count > MaxCount) { foreach (var item in fileHashAndInfo.Keys) { if (fileHashAndInfo.TryRemove(item, out _)) { break; } } } } /// /// 清除全部 /// public static void ClearDictionary() { if (filePathAndInfo == null) { return; } filePathAndInfo.Clear(); } /// /// 获取文件信息 /// /// /// /// /// public static bool GetFileInfo(string filePath, out UrlFileInfo urlFileInfo, bool breakpointResume) { if (filePathAndInfo == null) { urlFileInfo = null; return false; } if (filePathAndInfo.ContainsKey(filePath)) { urlFileInfo = filePathAndInfo[filePath]; if (File.Exists(filePath)) { using (FileStream stream = File.OpenRead(filePath)) { if (urlFileInfo.FileLength == stream.Length) { if (breakpointResume && urlFileInfo.FileHash == null) { urlFileInfo.FileHash = FileHashGenerator.GetFileHash(stream); AddFile(urlFileInfo); } return true; } } } } urlFileInfo = null; return false; } /// /// 通过FileHash获取文件信息 /// /// /// /// public static bool GetFileInfoFromHash(string fileHash, out UrlFileInfo urlFileInfo) { if (fileHashAndInfo == null) { urlFileInfo = null; return false; } if (string.IsNullOrEmpty(fileHash)) { urlFileInfo = null; return false; } if (fileHashAndInfo.TryGetValue(fileHash, out urlFileInfo)) { if (urlFileInfo.FileHash == fileHash) { if (File.Exists(urlFileInfo.FilePath)) { using (FileStream stream = File.OpenRead(urlFileInfo.FilePath)) { if (urlFileInfo.FileLength == stream.Length) { return true; } } } } } urlFileInfo = null; return false; } /// /// 移除 /// /// /// public static bool Remove(string filePath) { if (filePathAndInfo == null) { return false; } return filePathAndInfo.TryRemove(filePath, out _); } } }