Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 30.45 KB | None | 0 0
  1. public class Download
  2. {
  3. public event EventHandler<DownloadStatusChangedEventArgs> DownloadStatusChanged;
  4. public event EventHandler<DownloadProgressChangedEventArgs> DownloadProgressChanged;
  5. public event EventHandler DownloadCompleted;
  6.  
  7. public bool stop = true; // by default stop is true
  8.  
  9. public void DownloadFile(string DownloadLink, string Path)
  10. {
  11. stop = false; // always set this bool to false, everytime this method is called
  12.  
  13. long ExistingLength = 0;
  14. FileStream saveFileStream;
  15.  
  16. if (File.Exists(Path))
  17. {
  18. FileInfo fileInfo = new FileInfo(Path);
  19. ExistingLength = fileInfo.Length;
  20. }
  21.  
  22. if (ExistingLength > 0)
  23. {
  24. saveFileStream = new FileStream(Path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  25. }
  26. else
  27. {
  28. saveFileStream = new FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
  29. }
  30.  
  31. var request = (HttpWebRequest)HttpWebRequest.Create(DownloadLink);
  32. request.Proxy = null;
  33. request.AddRange(ExistingLength);
  34.  
  35. try
  36. {
  37. using (var response = (HttpWebResponse)request.GetResponse())
  38. {
  39. long FileSize = ExistingLength + response.ContentLength; //response.ContentLength gives me the size that is remaining to be downloaded
  40. bool downloadResumable; // need it for sending empty progress
  41.  
  42. if ((int)response.StatusCode == 206)
  43. {
  44. //Console.WriteLine("Resumable");
  45. var downloadStatusArgs = new DownloadStatusChangedEventArgs();
  46. downloadResumable = true;
  47. downloadStatusArgs.ResumeSupported = downloadResumable;
  48. OnDownloadStatusChanged(downloadStatusArgs);
  49. }
  50. else // sometimes a server that supports partial content will lose its ability to send partial content(weird behavior) and thus the download will lose its resumability
  51. {
  52. //Console.WriteLine("Resume Not Supported");
  53. ExistingLength = 0;
  54. var downloadStatusArgs = new DownloadStatusChangedEventArgs();
  55. downloadResumable = false;
  56. downloadStatusArgs.ResumeSupported = downloadResumable;
  57. OnDownloadStatusChanged(downloadStatusArgs);
  58. // restart downloading the file from the beginning because it isn't resumable
  59. // if this isn't done, the method downloads the file from the beginning and starts writing it after the previously half downloaded file, thus increasing the filesize and corrupting the downloaded file
  60. saveFileStream.Dispose(); // dispose object to free it for the next operation
  61. File.WriteAllText(Path, string.Empty); // clear the contents of the half downloaded file that can't be resumed
  62. saveFileStream = new FileStream(Path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite); // reopen it for writing
  63. }
  64.  
  65. using (var stream = response.GetResponseStream())
  66. {
  67. byte[] downBuffer = new byte[4096];
  68. int byteSize = 0;
  69. long totalReceived = byteSize + ExistingLength;
  70. var sw = new Stopwatch();
  71. sw.Start();
  72. while ((byteSize = stream.Read(downBuffer, 0, downBuffer.Length)) > 0)
  73. {
  74. saveFileStream.Write(downBuffer, 0, byteSize);
  75. totalReceived += byteSize;
  76.  
  77. var args = new DownloadProgressChangedEventArgs();
  78. args.BytesReceived = totalReceived;
  79. args.TotalBytesToReceive = FileSize;
  80. float currentSpeed = totalReceived / (float)sw.Elapsed.TotalSeconds;
  81. args.CurrentSpeed = currentSpeed;
  82. if (downloadResumable == true)
  83. {
  84. args.ProgressPercentage = ((float)totalReceived / (float)FileSize) * 100;
  85. long bytesRemainingtoBeReceived = FileSize - totalReceived;
  86. args.TimeLeft = (long)(bytesRemainingtoBeReceived / currentSpeed);
  87. }
  88. else
  89. {
  90. //args.ProgressPercentage = Unknown;
  91. //args.TimeLeft = Unknown;
  92. }
  93. OnDownloadProgressChanged(args);
  94.  
  95. if (stop == true)
  96. return;
  97. }
  98. sw.Stop();
  99. }
  100. }
  101. var completedArgs = new EventArgs();
  102. OnDownloadCompleted(completedArgs);
  103. }
  104. catch (WebException e)
  105. {
  106. string filename = System.IO.Path.GetFileName(Path);
  107. Console.WriteLine(e.Message);
  108. }
  109. finally
  110. {
  111. saveFileStream.Dispose();
  112. }
  113. }
  114.  
  115. public void StopDownload()
  116. {
  117. stop = true;
  118. }
  119.  
  120. protected virtual void OnDownloadStatusChanged(DownloadStatusChangedEventArgs e)
  121. {
  122. EventHandler<DownloadStatusChangedEventArgs> handler = DownloadStatusChanged;
  123. if (handler != null)
  124. {
  125. handler(this, e);
  126. }
  127. }
  128.  
  129. protected virtual void OnDownloadProgressChanged(DownloadProgressChangedEventArgs e)
  130. {
  131. EventHandler<DownloadProgressChangedEventArgs> handler = DownloadProgressChanged;
  132. if (handler != null)
  133. {
  134. handler(this, e);
  135. }
  136. }
  137.  
  138. protected virtual void OnDownloadCompleted(EventArgs e)
  139. {
  140. EventHandler handler = DownloadCompleted;
  141. if (handler != null)
  142. {
  143. handler(this, e);
  144. }
  145. }
  146. }
  147.  
  148. public class DownloadStatusChangedEventArgs : EventArgs
  149. {
  150. public bool ResumeSupported { get; set; }
  151. }
  152.  
  153. public class DownloadProgressChangedEventArgs : EventArgs
  154. {
  155. public long BytesReceived { get; set; }
  156. public long TotalBytesToReceive { get; set; }
  157. public float ProgressPercentage { get; set; }
  158. public float CurrentSpeed { get; set; } // in bytes
  159. public long TimeLeft { get; set; } // in seconds
  160. }
  161.  
  162. public interface ILogger
  163. {
  164. void LogMessage(string message, params Object[] args);
  165. }
  166.  
  167. public class ConsoleLogger : ILogger
  168. {
  169. public void LogMessage(string message, params Object[] args)
  170. {
  171. Console.WriteLine(string.Format(message, args));
  172. }
  173. }
  174.  
  175. public class EventLogger : ILogger
  176. {
  177. // ...
  178. }
  179.  
  180. private ILogger _Logger;
  181.  
  182. public ClassName(ILogger logger)
  183. {
  184. _Logger = logger;
  185. }
  186.  
  187. logger.LogMessage(/* ... */);
  188.  
  189. if (logger != null) { logger.LogMessage(/* ... */); }
  190.  
  191. if (ExistingLength > 0)
  192. saveFileStream = new FileStream(Path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  193. else
  194. saveFileStream = new FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
  195.  
  196. File.WriteAllText(Path, string.Empty); // clear the contents of the half downloaded file that can't be resumed
  197.  
  198. saveFileStream = new FileStream(Path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  199.  
  200. if (stop == true)
  201. return;
  202.  
  203. if (stop == true) return;
  204.  
  205. if (stop) return;
  206.  
  207. else
  208. {
  209. //args.ProgressPercentage = Unknown;
  210. //args.TimeLeft = Unknown;
  211. }
  212.  
  213. public void DownloadFile(string DownloadLink, string Path)
  214. {
  215. stop = false;
  216.  
  217. long ExistingLength = 0;
  218. //using (FileStream saveFileStream = new FileStream()
  219. try
  220. {
  221. if (File.Exists(Path))
  222. {
  223. FileInfo fileInfo = new FileInfo(Path);
  224. ExistingLength = fileInfo.Length;
  225. }
  226.  
  227. if (ExistingLength > 0)
  228. {
  229. saveFileStream = new FileStream(Path, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
  230. }
  231. else
  232. {
  233. saveFileStream = new FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
  234. }
  235.  
  236. var request = (HttpWebRequest)HttpWebRequest.Create(DownloadLink);
  237. request.Proxy = null;
  238. request.AddRange(ExistingLength);
  239.  
  240. try
  241. {
  242. using (var response = (HttpWebResponse)request.GetResponse())
  243. {
  244. long FileSize = ExistingLength + response.ContentLength; //response.ContentLength gives me the size that is remaining to be downloaded
  245. bool downloadResumable; // need it for sending empty progress
  246.  
  247. if ((int)response.StatusCode == 206)
  248. {
  249. //Console.WriteLine("Resumable");
  250. var downloadStatusArgs = new DownloadStatusChangedEventArgs();
  251. downloadResumable = true;
  252. downloadStatusArgs.ResumeSupported = downloadResumable;
  253. OnDownloadStatusChanged(downloadStatusArgs);
  254. }
  255. else // sometimes a server that supports partial content will lose its ability to send partial content(weird behavior) and thus the download will lose its resumability
  256. {
  257. //Console.WriteLine("Resume Not Supported");
  258. ExistingLength = 0;
  259. var downloadStatusArgs = new DownloadStatusChangedEventArgs();
  260. downloadResumable = false;
  261. downloadStatusArgs.ResumeSupported = downloadResumable;
  262. OnDownloadStatusChanged(downloadStatusArgs);
  263. // restart downloading the file from the beginning because it isn't resumable
  264. // if this isn't done, the method downloads the file from the beginning and starts writing it after the
  265. // previously half downloaded file, thus increasing the filesize and corrupting the downloaded file
  266. saveFileStream.Flush();// clear the contents of the half downloaded file that can't be resumed
  267. }
  268.  
  269. using (var stream = response.GetResponseStream())
  270. {
  271. byte[] downBuffer = new byte[4096];
  272. int byteSize = 0;
  273. long totalReceived = byteSize + ExistingLength;
  274. var sw = new Stopwatch();
  275. sw.Start();
  276. while ((byteSize = stream.Read(downBuffer, 0, downBuffer.Length)) > 0)
  277. {
  278. saveFileStream.Write(downBuffer, 0, byteSize);
  279. totalReceived += byteSize;
  280.  
  281. var args = new DownloadProgressChangedEventArgs();
  282. args.BytesReceived = totalReceived;
  283. args.TotalBytesToReceive = FileSize;
  284. float currentSpeed = totalReceived / (float)sw.Elapsed.TotalSeconds;
  285. args.CurrentSpeed = currentSpeed;
  286. if (downloadResumable == true)
  287. {
  288. args.ProgressPercentage = ((float)totalReceived / (float)FileSize) * 100;
  289. long bytesRemainingtoBeReceived = FileSize - totalReceived;
  290. args.TimeLeft = (long)(bytesRemainingtoBeReceived / currentSpeed);
  291. }
  292.  
  293. OnDownloadProgressChanged(args);
  294.  
  295. if (stop == true) return;
  296.  
  297. }
  298. sw.Stop();
  299. }
  300. }
  301. var completedArgs = new EventArgs();
  302. OnDownloadCompleted(completedArgs);
  303. }
  304. catch (WebException e)
  305. {
  306. string filename = System.IO.Path.GetFileName(Path);
  307. Console.WriteLine(e.Message);
  308. }
  309. }
  310. finally
  311. {
  312. saveFileStream.Close()
  313. saveFileStream.Dispose()
  314. }
  315. }
  316.  
  317. public interface ILogger
  318. {
  319. void Log(string format, params object[] args);
  320. void Log(object message);
  321. }
  322.  
  323. public class Downloader
  324. {
  325. public Downloader(ILogger logger)
  326. {
  327. _logger = logger;
  328. }
  329.  
  330. public event EventHandler<DownloadStatusChangedEventArgs> ResumablityChanged;
  331. public event EventHandler<DownloadProgressChangedEventArgs> ProgressChanged;
  332. public event EventHandler Completed;
  333.  
  334. public bool stop = true; // by default stop is true
  335. private ILogger _logger;
  336.  
  337. public void DownloadFile(string downloadLink, string path)
  338. {
  339. stop = false; // always set this bool to false, everytime this method is called
  340.  
  341. var fileInfo = new FileInfo(path);
  342. long existingLength = 0;
  343. if (fileInfo.Exists)
  344. existingLength = fileInfo.Length;
  345.  
  346. var request = (HttpWebRequest)HttpWebRequest.Create(downloadLink);
  347. request.Proxy = null;
  348. request.AddRange(existingLength);
  349.  
  350. try
  351. {
  352. using (var response = (HttpWebResponse)request.GetResponse())
  353. {
  354. long fileSize = existingLength + response.ContentLength; //response.ContentLength gives me the size that is remaining to be downloaded
  355. bool downloadResumable; // need it for sending empty progress
  356.  
  357. if (response.StatusCode == HttpStatusCode.PartialContent)
  358. {
  359. downloadResumable = true;
  360. }
  361. else // sometimes a server that supports partial content will lose its ability to send partial content(weird behavior) and thus the download will lose its resumability
  362. {
  363. _logger.Log("Resume Not Supported");
  364. existingLength = 0;
  365. downloadResumable = false;
  366. }
  367. OnResumabilityChanged(new DownloadStatusChangedEventArgs(downloadResumable));
  368.  
  369. using (var saveFileStream = fileInfo.Open(downloadResumable ? FileMode.Append : FileMode.Create, FileAccess.Write))
  370. using (var stream = response.GetResponseStream())
  371. {
  372. byte[] downBuffer = new byte[4096];
  373. int byteSize = 0;
  374. long totalReceived = byteSize + existingLength;
  375. var sw = Stopwatch.StartNew();
  376. while (!stop && (byteSize = stream.Read(downBuffer, 0, downBuffer.Length)) > 0)
  377. {
  378. saveFileStream.Write(downBuffer, 0, byteSize);
  379. totalReceived += byteSize;
  380.  
  381. var currentSpeed = totalReceived / sw.Elapsed.TotalSeconds;
  382. OnProgressChanged(new DownloadProgressChangedEventArgs(totalReceived, fileSize, (long)currentSpeed));
  383. }
  384. sw.Stop();
  385. }
  386. }
  387. if (!stop)
  388. OnCompleted(EventArgs.Empty);
  389. }
  390. catch (WebException e)
  391. {
  392. _logger.Log(e);
  393. }
  394. }
  395.  
  396. public void StopDownload()
  397. {
  398. stop = true;
  399. }
  400.  
  401. protected virtual void OnResumabilityChanged(DownloadStatusChangedEventArgs e)
  402. {
  403. var handler = ResumablityChanged;
  404. if (handler != null)
  405. {
  406. handler(this, e);
  407. }
  408. }
  409.  
  410. protected virtual void OnProgressChanged(DownloadProgressChangedEventArgs e)
  411. {
  412. var handler = ProgressChanged;
  413. if (handler != null)
  414. {
  415. handler(this, e);
  416. }
  417. }
  418.  
  419. protected virtual void OnCompleted(EventArgs e)
  420. {
  421. var handler = Completed;
  422. if (handler != null)
  423. {
  424. handler(this, e);
  425. }
  426. }
  427. }
  428.  
  429. public class DownloadStatusChangedEventArgs : EventArgs
  430. {
  431. public DownloadStatusChangedEventArgs(bool canResume)
  432. {
  433. ResumeSupported = canResume;
  434. }
  435. public bool ResumeSupported { get; private set; }
  436. }
  437.  
  438. public class DownloadProgressChangedEventArgs : EventArgs
  439. {
  440. public DownloadProgressChangedEventArgs(long totalReceived, long fileSize, long currentSpeed)
  441. {
  442. BytesReceived = totalReceived;
  443. TotalBytesToReceive = fileSize;
  444. CurrentSpeed = currentSpeed;
  445. }
  446. public long BytesReceived { get; private set; }
  447. public long TotalBytesToReceive { get; private set; }
  448. public float ProgressPercentage { get { return ((float)BytesReceived / (float)TotalBytesToReceive) * 100; } }
  449. /// <summary>in Bytes</summary>
  450. public long CurrentSpeed { get; private set; }
  451. public TimeSpan TimeLeft
  452. {
  453. get
  454. {
  455. var bytesRemainingtoBeReceived = TotalBytesToReceive - BytesReceived;
  456. return TimeSpan.FromSeconds(bytesRemainingtoBeReceived / CurrentSpeed);
  457. }
  458. }
  459. }
  460.  
  461. using System;
  462. using System.Diagnostics;
  463. using System.IO;
  464. using System.Net;
  465. using System.Threading;
  466.  
  467. namespace Downloader
  468. {
  469. public interface ILogger
  470. {
  471. void LogMessage(string message, params Object[] args);
  472. }
  473.  
  474. public class ConsoleLogger : ILogger
  475. {
  476. public void LogMessage(string message, params Object[] args)
  477. {
  478. Console.WriteLine(string.Format(message, args));
  479. }
  480. }
  481.  
  482. public class Download
  483. {
  484. private ILogger _Logger;
  485.  
  486. public CallingClass(ILogger logger)
  487. {
  488. _Logger = logger;
  489. }
  490.  
  491. public event EventHandler<DownloadStatusChangedEventArgs> ResumablityChanged;
  492. public event EventHandler<DownloadProgressChangedEventArgs> ProgressChanged;
  493. public event EventHandler Completed;
  494.  
  495. public bool stop = true; // by default stop is true
  496. public bool paused = false;
  497. SemaphoreSlim pauseLock = new SemaphoreSlim(1);
  498.  
  499. string filename;
  500.  
  501. public void DownloadFile(string DownloadLink, string Path)
  502. {
  503. filename = System.IO.Path.GetFileName(Path);
  504.  
  505. stop = false; // always set this bool to false, everytime this method is called
  506.  
  507. var fileInfo = new FileInfo(Path);
  508. long existingLength = 0;
  509. if (fileInfo.Exists)
  510. existingLength = fileInfo.Length;
  511.  
  512. var request = (HttpWebRequest)HttpWebRequest.Create(DownloadLink);
  513. request.Proxy = null;
  514. request.AddRange(existingLength);
  515.  
  516. try
  517. {
  518. using (var response = (HttpWebResponse)request.GetResponse())
  519. {
  520. long fileSize = existingLength + response.ContentLength; //response.ContentLength gives me the size that is remaining to be downloaded
  521. bool downloadResumable; // need it for not sending any progress
  522.  
  523. if ((int)response.StatusCode == 206) //same as: response.StatusCode == HttpStatusCode.PartialContent
  524. {
  525. logger.LogMessage("Resumable");
  526. downloadResumable = true;
  527. }
  528. else // sometimes a server that supports partial content will lose its ability to send partial content(weird behavior) and thus the download will lose its resumability
  529. {
  530. logger.LogMessage("Not Resumable");
  531. existingLength = 0;
  532. downloadResumable = false;
  533. }
  534. OnResumabilityChanged(new DownloadStatusChangedEventArgs(downloadResumable));
  535.  
  536. using (var saveFileStream = fileInfo.Open(downloadResumable ? FileMode.Append : FileMode.Create, FileAccess.Write))
  537. using (var stream = response.GetResponseStream())
  538. {
  539. byte[] downBuffer = new byte[4096];
  540. int byteSize = 0;
  541. long totalReceived = byteSize + existingLength;
  542. var sw = Stopwatch.StartNew();
  543. while (!stop && (byteSize = stream.Read(downBuffer, 0, downBuffer.Length)) > 0)
  544. {
  545. saveFileStream.Write(downBuffer, 0, byteSize);
  546. totalReceived += byteSize;
  547.  
  548. float currentSpeed = totalReceived / (float)sw.Elapsed.TotalSeconds;
  549. OnProgressChanged(new DownloadProgressChangedEventArgs(totalReceived, fileSize, (long)currentSpeed));
  550.  
  551. pauseLock.Wait();
  552. pauseLock.Release();
  553. }
  554. sw.Stop();
  555. }
  556. }
  557. if (!stop) OnCompleted(EventArgs.Empty);
  558. }
  559. catch (WebException e)
  560. {
  561. logger.LogMessage(e);
  562. }
  563. }
  564.  
  565. public void pause()
  566. {
  567. if (!paused)
  568. {
  569. paused = true;
  570. // Note this cannot block for more than a moment
  571. // since the download thread doesn't keep the lock held
  572. pauseLock.Wait();
  573. }
  574. }
  575.  
  576. public void unpause()
  577. {
  578. if (paused)
  579. {
  580. paused = false;
  581. pauseLock.Release();
  582. }
  583. }
  584.  
  585. public void StopDownload()
  586. {
  587. stop = true;
  588. this.unpause(); // stop waiting on lock if needed
  589. }
  590.  
  591. protected virtual void OnResumabilityChanged(DownloadStatusChangedEventArgs e)
  592. {
  593. var handler = ResumablityChanged;
  594. if (handler != null)
  595. {
  596. handler(this, e);
  597. }
  598. }
  599.  
  600. protected virtual void OnProgressChanged(DownloadProgressChangedEventArgs e)
  601. {
  602. var handler = ProgressChanged;
  603. if (handler != null)
  604. {
  605. handler(this, e);
  606. }
  607. }
  608.  
  609. protected virtual void OnCompleted(EventArgs e)
  610. {
  611. var handler = Completed;
  612. if (handler != null)
  613. {
  614. handler(this, e);
  615. }
  616. }
  617. }
  618.  
  619. public class DownloadStatusChangedEventArgs : EventArgs
  620. {
  621. public DownloadStatusChangedEventArgs(bool canResume)
  622. {
  623. ResumeSupported = canResume;
  624. }
  625. public bool ResumeSupported { get; private set; }
  626. }
  627.  
  628. public class DownloadProgressChangedEventArgs : EventArgs
  629. {
  630. public DownloadProgressChangedEventArgs(long totalReceived, long fileSize, long currentSpeed)
  631. {
  632. BytesReceived = totalReceived;
  633. TotalBytesToReceive = fileSize;
  634. CurrentSpeed = currentSpeed;
  635. }
  636.  
  637. public long BytesReceived { get; private set; }
  638. public long TotalBytesToReceive { get; private set; }
  639. public float ProgressPercentage
  640. {
  641. get
  642. {
  643. return ((float)BytesReceived / (float)TotalBytesToReceive) * 100;
  644. }
  645. }
  646. public float CurrentSpeed { get; private set; } // in bytes
  647. public TimeSpan TimeLeft
  648. {
  649. get
  650. {
  651. var bytesRemainingtoBeReceived = TotalBytesToReceive - BytesReceived;
  652. return TimeSpan.FromSeconds(bytesRemainingtoBeReceived / CurrentSpeed);
  653. }
  654. }
  655. }
  656. }
  657.  
  658. using System;
  659. using System.Diagnostics;
  660. using System.IO;
  661. using System.Net;
  662. using System.Threading;
  663.  
  664. namespace Downloader
  665. {
  666. public interface ILogger
  667. {
  668. void LogMessage(string message, params Object[] args);
  669. }
  670.  
  671. public class ConsoleLogger : ILogger
  672. {
  673. public void LogMessage(string message, params Object[] args)
  674. {
  675. Console.WriteLine(string.Format(message, args));
  676. }
  677. }
  678.  
  679. public class Download
  680. {
  681. private ILogger _Logger;
  682.  
  683. public CallingClass(ILogger logger)
  684. {
  685. _Logger = logger;
  686. }
  687.  
  688. public event EventHandler<DownloadStatusChangedEventArgs> ResumablityChanged;
  689. public event EventHandler<DownloadProgressChangedEventArgs> ProgressChanged;
  690. public event EventHandler Completed;
  691.  
  692. public bool stop = true; // by default stop is true
  693. public bool paused = false;
  694. SemaphoreSlim pauseLock = new SemaphoreSlim(1);
  695.  
  696. string filename;
  697.  
  698. public void DownloadFile(string DownloadLink, string Path)
  699. {
  700. filename = System.IO.Path.GetFileName(Path);
  701.  
  702. stop = false; // always set this bool to false, everytime this method is called
  703.  
  704. var fileInfo = new FileInfo(Path);
  705. long existingLength = 0;
  706. if (fileInfo.Exists)
  707. existingLength = fileInfo.Length;
  708.  
  709. var request = (HttpWebRequest)HttpWebRequest.Create(DownloadLink);
  710. request.Proxy = null;
  711. request.AddRange(existingLength);
  712.  
  713. try
  714. {
  715. using (var response = (HttpWebResponse)request.GetResponse())
  716. {
  717. long fileSize = existingLength + response.ContentLength; //response.ContentLength gives me the size that is remaining to be downloaded
  718. bool downloadResumable; // need it for not sending any progress
  719.  
  720. if ((int)response.StatusCode == 206) //same as: response.StatusCode == HttpStatusCode.PartialContent
  721. {
  722. logger.LogMessage("Resumable");
  723. downloadResumable = true;
  724. }
  725. else // sometimes a server that supports partial content will lose its ability to send partial content(weird behavior) and thus the download will lose its resumability
  726. {
  727. logger.LogMessage("Not Resumable");
  728. if (existingLength > 0)
  729. {
  730. if (ResumeUnsupportedWarning() == false) // warn and ask for confirmation to continue if the half downloaded file is unresumable
  731. {
  732. return;
  733. }
  734. }
  735. existingLength = 0;
  736. downloadResumable = false;
  737. }
  738. OnResumabilityChanged(new DownloadStatusChangedEventArgs(downloadResumable));
  739.  
  740. using (var saveFileStream = fileInfo.Open(downloadResumable ? FileMode.Append : FileMode.Create, FileAccess.Write))
  741. using (var stream = response.GetResponseStream())
  742. {
  743. byte[] downBuffer = new byte[4096];
  744. int byteSize = 0;
  745. long totalReceived = byteSize + existingLength;
  746. var sw = Stopwatch.StartNew();
  747. while (!stop && (byteSize = stream.Read(downBuffer, 0, downBuffer.Length)) > 0)
  748. {
  749. saveFileStream.Write(downBuffer, 0, byteSize);
  750. totalReceived += byteSize;
  751.  
  752. float currentSpeed = totalReceived / (float)sw.Elapsed.TotalSeconds;
  753. OnProgressChanged(new DownloadProgressChangedEventArgs(totalReceived, fileSize, (long)currentSpeed));
  754.  
  755. pauseLock.Wait();
  756. pauseLock.Release();
  757. }
  758. sw.Stop();
  759. }
  760. }
  761. if (!stop) OnCompleted(EventArgs.Empty);
  762. }
  763. catch (WebException e)
  764. {
  765. System.Windows.MessageBox.Show(e.Message, filename);
  766. }
  767. }
  768.  
  769. public void pause()
  770. {
  771. if (!paused)
  772. {
  773. paused = true;
  774. // Note this cannot block for more than a moment
  775. // since the download thread doesn't keep the lock held
  776. pauseLock.Wait();
  777. }
  778. }
  779.  
  780. public void unpause()
  781. {
  782. if (paused)
  783. {
  784. paused = false;
  785. pauseLock.Release();
  786. }
  787. }
  788.  
  789. public void StopDownload()
  790. {
  791. stop = true;
  792. this.unpause(); // stop waiting on lock if needed
  793. }
  794.  
  795. public bool ResumeUnsupportedWarning()
  796. {
  797. var messageBoxResult = System.Windows.MessageBox.Show("When trying to resume the download , Mackerel got a response from the server that it doesn't support resuming the download. It's possible that it's a temporary error of the server, and you will be able to resume the file at a later time, but at this time Mackerel can download this file from the beginning.nnDo you want to download this file from the beginning?", filename, System.Windows.MessageBoxButton.YesNo);
  798. if (messageBoxResult == System.Windows.MessageBoxResult.Yes)
  799. {
  800. return true;
  801. }
  802. else
  803. {
  804. return false;
  805. }
  806. }
  807.  
  808. protected virtual void OnResumabilityChanged(DownloadStatusChangedEventArgs e)
  809. {
  810. var handler = ResumablityChanged;
  811. if (handler != null)
  812. {
  813. handler(this, e);
  814. }
  815. }
  816.  
  817. protected virtual void OnProgressChanged(DownloadProgressChangedEventArgs e)
  818. {
  819. var handler = ProgressChanged;
  820. if (handler != null)
  821. {
  822. handler(this, e);
  823. }
  824. }
  825.  
  826. protected virtual void OnCompleted(EventArgs e)
  827. {
  828. var handler = Completed;
  829. if (handler != null)
  830. {
  831. handler(this, e);
  832. }
  833. }
  834. }
  835.  
  836. public class DownloadStatusChangedEventArgs : EventArgs
  837. {
  838. public DownloadStatusChangedEventArgs(bool canResume)
  839. {
  840. ResumeSupported = canResume;
  841. }
  842. public bool ResumeSupported { get; private set; }
  843. }
  844.  
  845. public class DownloadProgressChangedEventArgs : EventArgs
  846. {
  847. public DownloadProgressChangedEventArgs(long totalReceived, long fileSize, long currentSpeed)
  848. {
  849. BytesReceived = totalReceived;
  850. TotalBytesToReceive = fileSize;
  851. CurrentSpeed = currentSpeed;
  852. }
  853.  
  854. public long BytesReceived { get; private set; }
  855. public long TotalBytesToReceive { get; private set; }
  856. public float ProgressPercentage
  857. {
  858. get
  859. {
  860. return ((float)BytesReceived / (float)TotalBytesToReceive) * 100;
  861. }
  862. }
  863. public float CurrentSpeed { get; private set; } // in bytes
  864. public TimeSpan TimeLeft
  865. {
  866. get
  867. {
  868. var bytesRemainingtoBeReceived = TotalBytesToReceive - BytesReceived;
  869. return TimeSpan.FromSeconds(bytesRemainingtoBeReceived / CurrentSpeed);
  870. }
  871. }
  872. }
  873. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement