Aria2Helper.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using System;
  2. using System.Diagnostics;
  3. using System.IO;
  4. using System.Text;
  5. namespace BanTur.Core.Utility
  6. {
  7. public static class Aria2Helper
  8. {
  9. private static string GetNoticeFilePath(int id)
  10. {
  11. return Path.Combine(DbAccess.Config.WorkingDirectory, id + ".bat");
  12. }
  13. public static string GetLogFilePath(int id)
  14. {
  15. return Path.Combine(DbAccess.Config.WorkingDirectory, id + ".log");
  16. }
  17. public static string CreateNoticeFile(int id)
  18. {
  19. var self = Process.GetCurrentProcess().MainModule.FileName;
  20. var selfDir = Path.GetDirectoryName(self);
  21. var path = GetNoticeFilePath(id);
  22. var content = $"cd /d {selfDir}{Environment.NewLine}{self} {BanTurProgram.CmdComplete} {id} %3";
  23. if (Directory.Exists(DbAccess.Config.WorkingDirectory) == false) Directory.CreateDirectory(DbAccess.Config.WorkingDirectory);
  24. File.WriteAllText(path, content, Encoding.Default);
  25. return path;
  26. }
  27. public static void DeleteNoticeFile(int id)
  28. {
  29. var path = GetNoticeFilePath(id);
  30. File.Delete(path);
  31. }
  32. public static void StartBitTorrentDownload(string magnet, string downloadDirectory, string onBtDownloadComplete, string logFilePath)
  33. {
  34. if (Directory.Exists(downloadDirectory) == false) Directory.CreateDirectory(downloadDirectory);
  35. var p = new Process
  36. {
  37. StartInfo =
  38. {
  39. FileName = "aria2c",
  40. WorkingDirectory = downloadDirectory,
  41. ArgumentList =
  42. {
  43. "--seed-time=30",
  44. "--on-bt-download-complete=" + onBtDownloadComplete,
  45. magnet
  46. },
  47. UseShellExecute = true,
  48. WindowStyle = ProcessWindowStyle.Minimized,
  49. }
  50. };
  51. p.Start();
  52. }
  53. }
  54. }