Aria2Helper.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. //TODO: set --follow-torrent=mem for http torrent url
  36. var p = new Process
  37. {
  38. StartInfo =
  39. {
  40. FileName = "aria2c",
  41. WorkingDirectory = downloadDirectory,
  42. ArgumentList =
  43. {
  44. "--seed-time=30",
  45. "--on-bt-download-complete=" + onBtDownloadComplete,
  46. magnet
  47. },
  48. UseShellExecute = true,
  49. WindowStyle = ProcessWindowStyle.Minimized,
  50. }
  51. };
  52. p.Start();
  53. }
  54. }
  55. }