DownloadTask.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace HttpDownload
  8. {
  9. public class DownloadTask
  10. {
  11. public int BelongingId { get; set; }
  12. public int DownloadTaskId { get; set; }
  13. public string FileUrl { get; }
  14. public string FileDir { get; }
  15. public long Offset { get; } // 开始下载的位置
  16. public string ExpectedMD5 { get; }
  17. private volatile DownloadStatus status;
  18. public DownloadStatus Status { get { return status; } }
  19. private string exceptionInfo;
  20. public string ExceptionInfo { get { return exceptionInfo; } } // 保存发生异常时的Message
  21. private double percentage = 0d;
  22. private HttpDownloader httpDownloader = null;
  23. /// <summary>
  24. ///
  25. /// </summary>
  26. /// <param name="fileUrl"></param>
  27. /// <param name="fileDir"></param>
  28. /// <param name="expectedMD5"></param>
  29. /// <param name="offset">如果 offset == -1, 则把已写入文件的字节数当作offset</param>
  30. public DownloadTask(string fileUrl, string fileDir, string expectedMD5, long offset = 0L)
  31. {
  32. this.FileUrl = fileUrl;
  33. this.FileDir = fileDir;
  34. this.Offset = offset;
  35. this.ExpectedMD5 = expectedMD5;
  36. status = DownloadStatus.Created;
  37. exceptionInfo = string.Empty;
  38. httpDownloader = new HttpDownloader(fileUrl, fileDir, offset);
  39. }
  40. public DownloadTask(int belongingId, int downTaskId, string fileUrl, string fileDir, string expectedMD5, long offset = 0L)
  41. {
  42. this.BelongingId = belongingId;
  43. this.DownloadTaskId = downTaskId;
  44. this.FileUrl = fileUrl;
  45. this.FileDir = fileDir;
  46. this.Offset = offset;
  47. this.ExpectedMD5 = expectedMD5;
  48. status = DownloadStatus.Created;
  49. exceptionInfo = string.Empty;
  50. httpDownloader = new HttpDownloader(fileUrl, fileDir, offset);
  51. }
  52. internal static void download(DownloadTask dTask)
  53. {
  54. try
  55. {
  56. if (dTask.status.Equals(DownloadStatus.Paused))
  57. return;
  58. dTask.status = DownloadStatus.Processing;
  59. dTask.httpDownloader.DownloadFile();
  60. if (dTask.httpDownloader.running)
  61. dTask.status = DownloadStatus.Completed;
  62. }
  63. catch (Exception ex)
  64. {
  65. dTask.status = DownloadStatus.ExceptionStopped;
  66. dTask.exceptionInfo = ex.Message;
  67. }
  68. }
  69. public string calculateMD5()
  70. {
  71. if (Status.Equals(DownloadStatus.Completed))
  72. {
  73. string md5Digest = "";
  74. return md5Digest;
  75. }
  76. return null;
  77. }
  78. public void pauseTask()
  79. {
  80. if (status == DownloadStatus.Completed)
  81. return;
  82. httpDownloader.running = false;
  83. status = DownloadStatus.Paused;
  84. }
  85. public void resumeTask()
  86. {
  87. if (!httpDownloader.running)
  88. {
  89. httpDownloader.running = true;
  90. status = DownloadStatus.WaitToRun;
  91. }
  92. }
  93. public bool isFinished()
  94. {
  95. return httpDownloader.Finished;
  96. }
  97. public string getFileName()
  98. {
  99. return Path.GetFileName(FileDir);
  100. }
  101. public long totalFileSize
  102. {
  103. get
  104. {
  105. if (httpDownloader != null)
  106. return httpDownloader.TotalRemoteFileSize;
  107. return 0;
  108. }
  109. }
  110. public long BytesWritten
  111. {
  112. get
  113. {
  114. if (httpDownloader != null)
  115. return httpDownloader.BytesWritten;
  116. return 0;
  117. }
  118. }
  119. public string getSpeed(bool isFormatted)
  120. {
  121. if (httpDownloader != null && DownloadStatus.Processing.Equals(status))
  122. return httpDownloader.getSpeed(isFormatted);
  123. return "0 B/s";
  124. }
  125. public double getPercentage(int decimals)
  126. {
  127. if (httpDownloader != null)
  128. return httpDownloader.getPercentage(decimals);
  129. return 0d;
  130. }
  131. }
  132. public enum DownloadStatus
  133. {
  134. Created, // 初始状态
  135. WaitToRun, // 进入队列等待被执行
  136. Paused, // 已暂停
  137. Processing,// 正在执行
  138. Completed, // 完成
  139. ExceptionStopped // 出现异常已暂停
  140. }
  141. }