FileBackupPartial.cs 11 KB

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