Changeset 2720 for branches/eraser6/6.0/Eraser/UpdateForm.cs
- Timestamp:
- 6/27/2012 1:47:58 AM (12 months ago)
- File:
-
- 1 edited
-
branches/eraser6/6.0/Eraser/UpdateForm.cs (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
branches/eraser6/6.0/Eraser/UpdateForm.cs
r2719 r2720 690 690 reqUri = new Uri(new Uri(SelectedMirror.Link), new Uri(update.Link)); 691 691 692 //Then grab the download. 693 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUri); 694 using (WebResponse resp = req.GetResponse()) 692 //Then grab the download. We have to manually follow redirects since headers sent 693 //in the first requset would be lost. 694 ContentDisposition contentDisposition = null; 695 for (int redirects = 0; redirects < 20; ++redirects) 695 696 { 696 //Check for a suggested filename. 697 ContentDisposition contentDisposition = null; 698 foreach (string header in resp.Headers.AllKeys) 699 if (header.ToLowerInvariant() == "content-disposition") 700 contentDisposition = new ContentDisposition(resp.Headers[header]); 701 702 string tempFilePath = Path.Combine( 703 tempDir.FullName, string.Format(CultureInfo.InvariantCulture, "{0}-{1}", 704 ++currUpdate, 705 contentDisposition == null ? 706 Path.GetFileName(reqUri.GetComponents(UriComponents.Path, 707 UriFormat.Unescaped)) : contentDisposition.FileName)); 708 709 byte[] tempBuffer = new byte[16384]; 710 using (Stream strm = resp.GetResponseStream()) 711 using (FileStream tempStrm = new FileStream(tempFilePath, FileMode.CreateNew)) 712 using (BufferedStream bufStrm = new BufferedStream(tempStrm)) 697 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(reqUri); 698 req.AllowAutoRedirect = false; 699 using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse()) 713 700 { 714 //Copy the information into the file stream 715 int readBytes = 0; 716 while ((readBytes = strm.Read(tempBuffer, 0, tempBuffer.Length)) != 0) 701 //Try to find a content-disposition header, if we have one. 702 foreach (string header in resp.Headers.AllKeys) 703 if (header.ToLowerInvariant() == "content-disposition") 704 { 705 contentDisposition = new ContentDisposition(resp.Headers[header]); 706 break; 707 } 708 709 if ((int)resp.StatusCode >= 300 && (int)resp.StatusCode <= 399) 717 710 { 718 bufStrm.Write(tempBuffer, 0, readBytes); 719 720 //Compute progress 721 float itemProgress = tempStrm.Position / (float)resp.ContentLength; 722 float overallProgress = (currUpdate - 1 + itemProgress) / downloadQueue.Count; 723 OnProgress(new ProgressEventArgs(itemProgress, overallProgress, 724 update, S._("Downloading: {0}", update.Name))); 711 //Redirect. 712 foreach (string header in resp.Headers.AllKeys) 713 if (header.ToLowerInvariant() == "location") 714 { 715 reqUri = new Uri(resp.Headers[header]); 716 break; 717 } 718 } 719 else 720 { 721 //Check for a suggested filename. 722 string tempFilePath = Path.Combine( 723 tempDir.FullName, string.Format(CultureInfo.InvariantCulture, "{0}-{1}", 724 ++currUpdate, 725 contentDisposition == null ? 726 Path.GetFileName(reqUri.GetComponents(UriComponents.Path, 727 UriFormat.Unescaped)) : contentDisposition.FileName)); 728 729 byte[] tempBuffer = new byte[16384]; 730 using (Stream strm = resp.GetResponseStream()) 731 using (FileStream tempStrm = new FileStream(tempFilePath, FileMode.CreateNew)) 732 using (BufferedStream bufStrm = new BufferedStream(tempStrm)) 733 { 734 //Copy the information into the file stream 735 int readBytes = 0; 736 while ((readBytes = strm.Read(tempBuffer, 0, tempBuffer.Length)) != 0) 737 { 738 bufStrm.Write(tempBuffer, 0, readBytes); 739 740 //Compute progress 741 float itemProgress = tempStrm.Position / (float)resp.ContentLength; 742 float overallProgress = (currUpdate - 1 + itemProgress) / downloadQueue.Count; 743 OnProgress(new ProgressEventArgs(itemProgress, overallProgress, 744 update, S._("Downloading: {0}", update.Name))); 745 } 746 } 747 748 //Store the filename-to-update mapping 749 tempFilesMap.Add(tempFilePath, update); 750 751 //Let the event handler know the download is complete. 752 OnProgress(new ProgressEventArgs(1.0f, (float)currUpdate / downloadQueue.Count, 753 update, S._("Downloaded: {0}", update.Name))); 754 break; 725 755 } 726 756 } 727 728 //Store the filename-to-update mapping729 tempFilesMap.Add(tempFilePath, update);730 731 //Let the event handler know the download is complete.732 OnProgress(new ProgressEventArgs(1.0f, (float)currUpdate / downloadQueue.Count,733 update, S._("Downloaded: {0}", update.Name)));734 757 } 735 758 }
Note: See TracChangeset
for help on using the changeset viewer.
