WXWorkspace.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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) {
  17. string checkResult = Init(path);
  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)
  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. if (!Directory.Exists(UserBakConfig.UserResPath))
  74. {
  75. return "用户资源文件夹不存在,如需使用离线数据,请从工作区读取";
  76. }
  77. if (!Directory.Exists(UserBakConfig.UserWorkspacePath))
  78. {
  79. Directory.CreateDirectory(UserBakConfig.UserWorkspacePath);
  80. }
  81. string db = Path.Combine(UserBakConfig.UserWorkspacePath, "OriginalDB");
  82. string decDb = Path.Combine(UserBakConfig.UserWorkspacePath, "DecDB");
  83. if (!Directory.Exists(db))
  84. {
  85. Directory.CreateDirectory (db);
  86. }
  87. if (!Directory.Exists(decDb))
  88. {
  89. Directory.CreateDirectory(decDb);
  90. }
  91. SaveConfig(UserBakConfig);
  92. return "";
  93. }
  94. private static string GetMd5Hash(string input)
  95. {
  96. using (MD5 md5Hash = MD5.Create())
  97. {
  98. // Convert the input string to a byte array and compute the hash.
  99. byte[] data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(input));
  100. // Create a new Stringbuilder to collect the bytes
  101. // and create a string.
  102. StringBuilder sBuilder = new StringBuilder();
  103. // Loop through each byte of the hashed data
  104. // and format each one as a hexadecimal string.
  105. for (int i = 0; i < data.Length; i++)
  106. {
  107. sBuilder.Append(data[i].ToString("x2"));
  108. }
  109. // Return the hexadecimal string.
  110. return sBuilder.ToString();
  111. }
  112. }
  113. }
  114. }