WXWorkspace.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. using Newtonsoft.Json;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using WechatPCMsgBakTool.Model;
  11. namespace WechatPCMsgBakTool
  12. {
  13. public class WXWorkspace
  14. {
  15. private UserBakConfig UserBakConfig = new UserBakConfig();
  16. public WXWorkspace(string path,string account = "") {
  17. string checkResult = Init(path, account);
  18. if (checkResult != "")
  19. new Exception(checkResult);
  20. }
  21. public WXWorkspace(UserBakConfig userBakConfig)
  22. {
  23. UserBakConfig = userBakConfig;
  24. }
  25. public void MoveDB()
  26. {
  27. string sourceBase = Path.Combine(UserBakConfig.UserResPath, "Msg");
  28. string sourceMulit = Path.Combine(UserBakConfig.UserResPath, "Msg/Multi");
  29. string[] files = Directory.GetFiles(sourceBase);
  30. foreach (string file in files)
  31. {
  32. FileInfo fileInfo = new FileInfo(file);
  33. if(fileInfo.Extension == ".db")
  34. {
  35. string to_path = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB", fileInfo.Name);
  36. File.Copy(file, to_path, true);
  37. }
  38. }
  39. files = Directory.GetFiles(sourceMulit);
  40. foreach (string file in files)
  41. {
  42. FileInfo fileInfo = new FileInfo(file);
  43. if (fileInfo.Extension == ".db")
  44. {
  45. string to_path = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB", fileInfo.Name);
  46. File.Copy(file, to_path, true);
  47. }
  48. }
  49. }
  50. public static void SaveConfig(UserBakConfig userBakConfig)
  51. {
  52. if(userBakConfig.UserWorkspacePath != "")
  53. {
  54. DirectoryInfo directoryInfo = new DirectoryInfo(userBakConfig.UserWorkspacePath);
  55. if(directoryInfo.Parent != null)
  56. {
  57. string json_path = Path.Combine(directoryInfo.Parent.FullName, userBakConfig.UserName + ".json");
  58. string json = JsonConvert.SerializeObject(userBakConfig);
  59. File.WriteAllText(json_path, json);
  60. }
  61. }
  62. }
  63. private string Init(string path,string account = "")
  64. {
  65. string curPath = AppDomain.CurrentDomain.BaseDirectory;
  66. string md5 = GetMd5Hash(path);
  67. string[] paths = path.Split(new string[] { "/", "\\" }, StringSplitOptions.None);
  68. string username = paths[paths.Length - 1];
  69. UserBakConfig.UserResPath = path;
  70. UserBakConfig.UserWorkspacePath = Path.Combine(curPath, "workspace", md5);
  71. UserBakConfig.Hash = md5;
  72. UserBakConfig.UserName = username;
  73. UserBakConfig.Account = account;
  74. if (!Directory.Exists(UserBakConfig.UserResPath))
  75. {
  76. return "用户资源文件夹不存在,如需使用离线数据,请从工作区读取";
  77. }
  78. if (!Directory.Exists(UserBakConfig.UserWorkspacePath))
  79. {
  80. Directory.CreateDirectory(UserBakConfig.UserWorkspacePath);
  81. }
  82. string db = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB");
  83. string decDb = Path.Combine(UserBakConfig.UserWorkspacePath, "DecDB");
  84. if (!Directory.Exists(db))
  85. {
  86. Directory.CreateDirectory (db);
  87. }
  88. if (!Directory.Exists(decDb))
  89. {
  90. Directory.CreateDirectory(decDb);
  91. }
  92. SaveConfig(UserBakConfig);
  93. return "";
  94. }
  95. private static string GetMd5Hash(string input)
  96. {
  97. using (MD5 md5Hash = MD5.Create())
  98. {
  99. // Convert the input string to a byte array and compute the hash.
  100. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  101. // Create a new Stringbuilder to collect the bytes
  102. // and create a string.
  103. StringBuilder sBuilder = new StringBuilder();
  104. // Loop through each byte of the hashed data
  105. // and format each one as a hexadecimal string.
  106. for (int i = 0; i < data.Length; i++)
  107. {
  108. sBuilder.Append(data[i].ToString("x2"));
  109. }
  110. // Return the hexadecimal string.
  111. return sBuilder.ToString();
  112. }
  113. }
  114. }
  115. }