DownloadTask.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 string BelongingId { get; set; } // 任务id
  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(string taskId, string fileUrl, string fileDir, string expectedMD5, long offset = 0L)
  41. {
  42. this.BelongingId = taskId;
  43. this.FileUrl = fileUrl;
  44. this.FileDir = fileDir;
  45. this.Offset = offset;
  46. this.ExpectedMD5 = expectedMD5;
  47. status = DownloadStatus.Created;
  48. exceptionInfo = string.Empty;
  49. httpDownloader = new HttpDownloader(fileUrl, fileDir, offset);
  50. }
  51. internal static void download(DownloadTask dTask)
  52. {
  53. try
  54. {
  55. if (dTask.status.Equals(DownloadStatus.Paused))
  56. return;
  57. dTask.status = DownloadStatus.Processing;
  58. dTask.httpDownloader.DownloadFile();
  59. if (dTask.httpDownloader.running)
  60. dTask.status = DownloadStatus.Completed;
  61. }
  62. catch (Exception ex)
  63. {
  64. dTask.status = DownloadStatus.ExceptionStopped;
  65. dTask.exceptionInfo = ex.Message;
  66. }
  67. }
  68. public string calculateMD5()
  69. {
  70. if (Status.Equals(DownloadStatus.Completed))
  71. {
  72. string md5Digest = "";
  73. return md5Digest;
  74. }
  75. return null;
  76. }
  77. public void pauseTask()
  78. {
  79. if (status == DownloadStatus.Completed)
  80. return;
  81. httpDownloader.running = false;
  82. status = DownloadStatus.Paused;
  83. }
  84. public void resumeTask()
  85. {
  86. if (!httpDownloader.running)
  87. {
  88. httpDownloader.running = true;
  89. status = DownloadStatus.WaitToRun;
  90. }
  91. }
  92. public bool isFinished()
  93. {
  94. return httpDownloader.Finished;
  95. }
  96. public string getFileName()
  97. {
  98. return Path.GetFileName(FileDir);
  99. }
  100. public long totalFileSize
  101. {
  102. get
  103. {
  104. if (httpDownloader != null)
  105. return httpDownloader.TotalRemoteFileSize;
  106. return 0;
  107. }
  108. }
  109. public long BytesWritten
  110. {
  111. get
  112. {
  113. if (httpDownloader != null)
  114. return httpDownloader.BytesWritten;
  115. return 0;
  116. }
  117. }
  118. public string getSpeed(bool isFormatted)
  119. {
  120. if (httpDownloader != null && DownloadStatus.Processing.Equals(status))
  121. return httpDownloader.getSpeed(isFormatted);
  122. return "0 B/s";
  123. }
  124. public double getPercentage(int decimals)
  125. {
  126. if (httpDownloader != null)
  127. return httpDownloader.getPercentage(decimals);
  128. return 0d;
  129. }
  130. }
  131. public enum DownloadStatus
  132. {
  133. Created, // 初始状态
  134. WaitToRun, // 进入队列等待被执行
  135. Paused, // 已暂停
  136. Processing,// 正在执行
  137. Completed, // 完成
  138. ExceptionStopped // 出现异常已暂停
  139. }
  140. }