FileBackupPartial.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.IO;
  11. using Y.Utils.IOUtils.FileUtils;
  12. using Y.Utils.DataUtils.Collections;
  13. using Y.Utils.DataUtils.UnitConvertUtils;
  14. using Y.Utils.IOUtils.PathUtils;
  15. using Oreo.FileMan.Models;
  16. using Oreo.FileMan.DatabaseEngine;
  17. using Y.Utils.IOUtils.FileManUtils;
  18. using System.Windows.Threading;
  19. using System.Threading;
  20. using Y.Utils.DataUtils.DateTimeUtils;
  21. using Oreo.FileMan.Commons;
  22. namespace Oreo.FileMan.Partials
  23. {
  24. public partial class FileBackupPartial : UserControl
  25. {
  26. public FileBackupPartial()
  27. {
  28. InitializeComponent();
  29. }
  30. private void FileBackupPartial_Load(object sender, EventArgs e)
  31. {
  32. UIEnableButton(false);
  33. TmReadPaths.Enabled = true;
  34. TmStatus.Enabled = true;
  35. }
  36. #region Event
  37. private void BtAddPath_Click(object sender, EventArgs e)
  38. {
  39. FolderBrowserDialog dialog = new FolderBrowserDialog();
  40. dialog.Description = "请选择要备份的文件夹";
  41. if (dialog.ShowDialog() == DialogResult.OK)
  42. {
  43. string selPath = dialog.SelectedPath;//获取选中的目录
  44. string path = DirTool.Combine(selPath, "\\");//格式化选中的目录
  45. string name = DirTool.GetPathName(selPath);//获取目录名称
  46. List<BackupPaths> clashPath = R.Services.FBS.Paths.Where(x => x.Path.Contains(path) || path.Contains(x.Path)).ToList();//查询冲突项
  47. if (ListTool.HasElements(clashPath))
  48. {
  49. string cp = "";
  50. clashPath.ForEach(x => cp += (x.Path + ";"));
  51. //存在重合目录
  52. MessageBox.Show(string.Format("您当前选择路径:{0},与之前选择的目录:{1},存在嵌套包含关系,请先从备份目录中移除,然后重新添加。", path, cp));
  53. }
  54. else
  55. {
  56. UIEnableButton(false);
  57. Task.Factory.StartNew(() =>
  58. {
  59. using (var db = new Muse())
  60. {
  61. if (!db.Do<BackupPaths>().Any(x => x.Path == path))
  62. {
  63. BackupPaths bp = new BackupPaths() { Path = path, Alias = Guid.NewGuid().ToString() };
  64. if (db.Add(bp) > 0)
  65. {
  66. R.Services.FBS.Paths.Add(bp);//添加到列表
  67. R.Services.FBS.AddToWatcherPath(bp.Path);//添加到监听
  68. UIDgvPathAdd(name, null);//添加到列表UI
  69. }
  70. }
  71. }
  72. UIEnableButton(true);
  73. });
  74. }
  75. }
  76. }
  77. private void BtDelPath_Click(object sender, EventArgs e)
  78. {
  79. if (DgvPath.CurrentRow != null)
  80. {
  81. int row = DgvPath.CurrentRow.Index;
  82. string path = R.Services.FBS.Paths[row].Path;
  83. if (row >= 0)
  84. {
  85. using (var db = new Muse())
  86. {
  87. BackupPaths bp = db.Get<BackupPaths>(x => x.Path == path, null);
  88. if (bp != null) db.Del(bp, true);
  89. R.Services.FBS.Paths.RemoveAt(row);
  90. }
  91. UIDgvPathDel(row);
  92. }
  93. }
  94. }
  95. private void DgvPath_CellClick(object sender, DataGridViewCellEventArgs e)
  96. {
  97. ShowFileDetails(e.RowIndex);
  98. }
  99. /// <summary>
  100. /// 读取备份文件目录
  101. /// </summary>
  102. /// <param name="sender"></param>
  103. /// <param name="e"></param>
  104. private void TmReadPaths_Tick(object sender, EventArgs e)
  105. {
  106. if (R.Services.FBS.StatusOfReadBackupPaths)
  107. {
  108. TmReadPaths.Enabled = false;
  109. Task.Factory.StartNew(() =>
  110. {
  111. if (ListTool.HasElements(R.Services.FBS.Paths))
  112. {
  113. foreach (var p in R.Services.FBS.Paths)
  114. {
  115. using (var db = new Muse())
  116. {
  117. long size = db.Do<BackupFiles>().Where(x => x.FullPath.Contains(p.Path)).Sum(x => x.Size);
  118. string name = DirTool.GetPathName(p.Path);//获取目录名称
  119. UIDgvPathAdd(name, ByteConvertTool.Fmt(size));//添加到列表UI
  120. }
  121. }
  122. }
  123. UIEnableButton(true);
  124. });
  125. }
  126. }
  127. private void TmStatus_Tick(object sender, EventArgs e)
  128. {
  129. LbStatus.Text = R.Services.FBS.IsStart ?
  130. string.Format("文件备份已开启:已备份 {0} 个文件", R.Services.FBS.FileCount) : "文件监控已关闭";
  131. LbStatus.ForeColor = R.Services.FBS.IsStart ? Color.Green : Color.Red;
  132. }
  133. #endregion
  134. #region Function
  135. void ShowFileDetails(int row)
  136. {
  137. if (row >= 0)
  138. {
  139. string path = R.Services.FBS.Paths[row].Path;
  140. UIEnableButton(false);
  141. DgvFile.Rows.Clear();
  142. Task.Factory.StartNew(() =>
  143. {
  144. using (var db = new Muse())
  145. {
  146. db.Context.Database.Log = (sql) =>
  147. {
  148. R.Log.i(sql);
  149. };
  150. try
  151. {
  152. var result = db.Do<BackupFiles>().
  153. Where(x => x.FullPath.Contains(path)).
  154. GroupBy(x => new { x.FullPath }).
  155. Select(x => new
  156. {
  157. Path = x.Max(o => o.FullPath),
  158. BackPath = x.Max(o => o.BackupFullPath),
  159. Count = x.Count(),
  160. Time = x.Max(o => o.LastWriteTime),
  161. }).ToList();
  162. if (ListTool.HasElements(result))
  163. {
  164. foreach (var item in result)
  165. {
  166. //BackupFiles bkfile = bkfiles.FirstOrDefault(x => x.FullPath == file);
  167. //int versioncount = bkfiles.Count(x => x.FullPath == file);
  168. //string lastwritetime = bkfile != null ? bkfile.LastWriteTime : "-";
  169. string versiondesc = "第 " + item.Count + " 版";
  170. UIDgvFileAdd(Path.GetFileName(item.Path), item.Path, FileTool.SizeFormat(item.BackPath), versiondesc, item.Time);
  171. }
  172. }
  173. }
  174. catch (Exception e) { }
  175. //List<BackupFiles> bkfiles = db.Gets<BackupFiles>(x => x.FullPath.Contains(path), null).ToList();
  176. //List<string> files = FileTool.GetAllFile(path);
  177. //if (ListTool.HasElements(files))
  178. //{
  179. // foreach (var file in files)
  180. // {
  181. // BackupFiles bkfile = bkfiles.FirstOrDefault(x => x.FullPath == file);
  182. // int versioncount = bkfiles.Count(x => x.FullPath == file);
  183. // string versiondesc = "第 " + versioncount + " 版";
  184. // string lastwritetime = bkfile != null ? bkfile.LastWriteTime : "-";
  185. // UIDgvFileAdd(Path.GetFileName(file), file, FileTool.SizeFormat(file), versiondesc, lastwritetime);
  186. // }
  187. //}
  188. }
  189. UIEnableButton(true);
  190. });
  191. }
  192. }
  193. #endregion
  194. #region UI
  195. /// <summary>
  196. /// 停用或启用所有按钮
  197. /// </summary>
  198. /// <param name="enable"></param>
  199. void UIEnableButton(bool enable)
  200. {
  201. BeginInvoke(new Action(() =>
  202. {
  203. BtAddPath.Enabled = enable;
  204. BtDelPath.Enabled = enable;
  205. }));
  206. }
  207. /// <summary>
  208. /// 添加到路径列表
  209. /// </summary>
  210. /// <param name="path"></param>
  211. void UIDgvPathAdd(string name, string size)
  212. {
  213. BeginInvoke(new Action(() =>
  214. {
  215. DgvPath.Rows.Add(new object[] { name, size ?? "-" });
  216. }));
  217. }
  218. /// <summary>
  219. /// 从路径列表删除
  220. /// </summary>
  221. void UIDgvPathDel(int row)
  222. {
  223. BeginInvoke(new Action(() =>
  224. {
  225. DgvPath.Rows.RemoveAt(row);
  226. }));
  227. }
  228. /// <summary>
  229. /// 更新到路径列表
  230. /// </summary>
  231. /// <param name="row"></param>
  232. /// <param name="path"></param>
  233. /// <param name="size"></param>
  234. void UIDgvPathUpdate(int row, string path, string size)
  235. {
  236. BeginInvoke(new Action(() =>
  237. {
  238. DgvPath.Rows[row].SetValues(new object[] { Path.GetFileName(path), size });
  239. }));
  240. }
  241. /// <summary>
  242. /// 添加文件到列表
  243. /// </summary>
  244. /// <param name="file"></param>
  245. /// <param name="path"></param>
  246. /// <param name="size"></param>
  247. void UIDgvFileAdd(string file, string path, string size, string version, string lasttime)
  248. {
  249. BeginInvoke(new Action(() =>
  250. {
  251. DgvFile.Rows.Add(new object[] { file, size, version, lasttime, path });
  252. DgvFile.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
  253. }));
  254. }
  255. #endregion
  256. }
  257. }