Skip to content

Commit

Permalink
Minor changes to the downloader, a few missing files added
Browse files Browse the repository at this point in the history
  • Loading branch information
philc committed Oct 25, 2006
1 parent d50561e commit a5fcdb1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 34 deletions.
28 changes: 28 additions & 0 deletions Settings.cs
@@ -0,0 +1,28 @@
namespace InstallPad.Properties {


// This class allows you to handle specific events on the settings class:
// The SettingChanging event is raised before a setting's value is changed.
// The PropertyChanged event is raised after a setting's value is changed.
// The SettingsLoaded event is raised after the setting values are loaded.
// The SettingsSaving event is raised before the setting values are saved.
internal sealed partial class Settings {

public Settings() {
// // To add event handlers for saving and changing settings, uncomment the lines below:
//
// this.SettingChanging += this.SettingChangingEventHandler;
//
// this.SettingsSaving += this.SettingsSavingEventHandler;
//
}

private void SettingChangingEventHandler(object sender, System.Configuration.SettingChangingEventArgs e) {
// Add code to handle the SettingChangingEvent event here.
}

private void SettingsSavingEventHandler(object sender, System.ComponentModel.CancelEventArgs e) {
// Add code to handle the SettingsSaving event here.
}
}
}
Binary file added icon.ico
Binary file not shown.
83 changes: 49 additions & 34 deletions src/downloader/FileDownloader.cs
Expand Up @@ -12,7 +12,7 @@
namespace CodeProject.Downloader namespace CodeProject.Downloader
{ {
/// <summary> /// <summary>
/// Downloads and resumes files from HTTP, FTP, and File URLS /// Downloads and resumes files from HTTP, FTP, and File (file://) URLS
/// </summary> /// </summary>
public class FileDownloader public class FileDownloader
{ {
Expand Down Expand Up @@ -89,15 +89,11 @@ public void Download(string url, string destFolder)
// Find out the name of the file that the web server gave us. // Find out the name of the file that the web server gave us.
string destFileName = Path.GetFileName(data.Response.ResponseUri.ToString()); string destFileName = Path.GetFileName(data.Response.ResponseUri.ToString());



// The place we're downloading to (not from) must not be a URI, // The place we're downloading to (not from) must not be a URI,
// because Path and File don't handle them... // because Path and File don't handle them...
destFolder = destFolder.Replace("file:///", "").Replace("file://", ""); destFolder = destFolder.Replace("file:///", "").Replace("file://", "");
this.downloadingTo = Path.Combine(destFolder, destFileName); this.downloadingTo = Path.Combine(destFolder, destFileName);

// TODO cleanup
//if (! destFolder.StartsWith("file://"))
//this.downloadingTo=Path.GetFullPath(this.downloadingTo);


// Create the file on disk here, so even if we don't receive any data of the file // Create the file on disk here, so even if we don't receive any data of the file
// it's still on disk. This allows us to download 0-byte files. // it's still on disk. This allows us to download 0-byte files.
Expand Down Expand Up @@ -177,22 +173,18 @@ public void Download(List<string> urlList, string destFolder)
{ {
// validate input // validate input
if (urlList == null) if (urlList == null)
{
throw new ArgumentException("Url list not specified."); throw new ArgumentException("Url list not specified.");
}


if (urlList.Count == 0) if (urlList.Count == 0)
{
throw new ArgumentException("Url list empty."); throw new ArgumentException("Url list empty.");
}


// try each url in the list. // try each url in the list.
// if one succeeds, we are done. // if one succeeds, we are done.
// if any fail, move to the next. // if any fail, move to the next.
Exception ex=null; Exception ex = null;
foreach (string s in urlList) foreach (string s in urlList)
{ {
ex=null; ex = null;
try try
{ {
Download(s, destFolder); Download(s, destFolder);
Expand All @@ -210,28 +202,58 @@ public void Download(List<string> urlList, string destFolder)
} }


/// <summary> /// <summary>
/// Download a file from the url asynchronously. /// Asynchronously download a file from the url.
/// </summary> /// </summary>
public void AsyncDownload(string url) public void AsyncDownload(string url)
{ {
System.Threading.ThreadPool.QueueUserWorkItem( System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(this.WaitCallbackMethod), new string[] { url, "" }); new System.Threading.WaitCallback(this.WaitCallbackMethod), new string[] { url, "" });
} }
/// <summary> /// <summary>
/// Download a file from the url to the destination folder asynchronously. /// Asynchronously download a file from the url to the destination folder.
/// </summary> /// </summary>
public void AsyncDownload(string url, string destFolder) public void AsyncDownload(string url, string destFolder)
{ {
System.Threading.ThreadPool.QueueUserWorkItem( System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(this.WaitCallbackMethod), new string[] { url, destFolder }); new System.Threading.WaitCallback(this.WaitCallbackMethod), new string[] { url, destFolder });
} }
/// <summary> /// <summary>
/// Asynchronously download a file from a list or URLs. If downloading from one of the URLs fails,
/// another URL is tried.
/// </summary>
public void AsyncDownload(List<string> urlList, string destFolder)
{
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(this.WaitCallbackMethod), new object[] { urlList, destFolder });
}
/// <summary>
/// Asynchronously download a file from a list or URLs. If downloading from one of the URLs fails,
/// another URL is tried.
/// </summary>
public void AsyncDownload(List<string> urlList)
{
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(this.WaitCallbackMethod), new object[] { urlList, "" });
}
/// <summary>
/// A WaitCallback used by the AsyncDownload methods. /// A WaitCallback used by the AsyncDownload methods.
/// </summary> /// </summary>
private void WaitCallbackMethod(object data){ private void WaitCallbackMethod(object data)
// Expecting an array of two strings (url and dest folder) {
String[] strings = data as String[]; // Can either be a string array of two strings (url and dest folder),
this.Download(strings[0], strings[1]); // or an object array containing a list<string> and a dest folder
if (data is string[])
{
String[] strings = data as String[];
this.Download(strings[0], strings[1]);
}
else
{
Object[] list = data as Object[];
List<String> urlList = list[0] as List<String>;
String destFolder = list[1] as string;
this.Download(urlList, destFolder);
}
} }
private void SaveToFile(byte[] buffer, int count, string fileName) private void SaveToFile(byte[] buffer, int count, string fileName)
{ {
Expand Down Expand Up @@ -272,16 +294,16 @@ class DownloadData
private long size; private long size;
private long start; private long start;


private IWebProxy proxy=null; private IWebProxy proxy = null;


public static DownloadData Create(string url, string destFolder) public static DownloadData Create(string url, string destFolder)
{ {
return Create(url, destFolder, null); return Create(url, destFolder, null);
} }


public static DownloadData Create(string url, string destFolder,IWebProxy proxy) public static DownloadData Create(string url, string destFolder, IWebProxy proxy)
{ {

// This is what we will return // This is what we will return
DownloadData downloadData = new DownloadData(); DownloadData downloadData = new DownloadData();
downloadData.proxy = proxy; downloadData.proxy = proxy;
Expand Down Expand Up @@ -317,7 +339,7 @@ public static DownloadData Create(string url, string destFolder,IWebProxy proxy)
if (downloadData.IsProgressKnown && File.Exists(downloadTo)) if (downloadData.IsProgressKnown && File.Exists(downloadTo))
{ {
// We only support resuming on http requests // We only support resuming on http requests
if (! (downloadData.Response is HttpWebResponse)) if (!(downloadData.Response is HttpWebResponse))
{ {
File.Delete(downloadTo); File.Delete(downloadTo);
} }
Expand All @@ -337,7 +359,7 @@ public static DownloadData Create(string url, string destFolder,IWebProxy proxy)
req = downloadData.GetRequest(url); req = downloadData.GetRequest(url);
((HttpWebRequest)req).AddRange((int)downloadData.start); ((HttpWebRequest)req).AddRange((int)downloadData.start);
downloadData.response = req.GetResponse(); downloadData.response = req.GetResponse();

if (((HttpWebResponse)downloadData.Response).StatusCode != HttpStatusCode.PartialContent) if (((HttpWebResponse)downloadData.Response).StatusCode != HttpStatusCode.PartialContent)
{ {
// They didn't support our resume request. // They didn't support our resume request.
Expand Down Expand Up @@ -430,14 +452,7 @@ private WebRequest GetRequest(string url)
{ {
request.Proxy = this.proxy; request.Proxy = this.proxy;
} }
/*else if (request is FtpWebRequest)
{
WebProxy proxy = new WebProxy();
proxy.Address = new Uri("http://123yesman123.com");
request.Proxy = proxy;
}*/

return request; return request;
} }


Expand All @@ -462,7 +477,7 @@ public Stream DownloadStream
this.stream = this.response.GetResponseStream(); this.stream = this.response.GetResponseStream();
return this.stream; return this.stream;
} }
} }
public long FileSize public long FileSize
{ {
get get
Expand All @@ -484,10 +499,10 @@ public bool IsProgressKnown
// If the size of the remote url is -1, that means we // If the size of the remote url is -1, that means we
// couldn't determine it, and so we don't know // couldn't determine it, and so we don't know
// progress information. // progress information.
return this.size>-1; return this.size > -1;
} }
} }
#endregion #endregion
} }


/// <summary> /// <summary>
Expand Down

0 comments on commit a5fcdb1

Please sign in to comment.