using System;\r
using System.Net;\r
+using System.Net.Mime;\r
using System.IO;\r
-using System.Collections;\r
using System.Threading;\r
-using NaGet.SubCommands;\r
+using NaGet.Tasks;\r
\r
namespace NaGet.Net\r
{\r
/// <summary>\r
/// ダウンロードイベントオブジェクト\r
/// </summary>\r
-public class DownloadEventArgs : NaGetEventArgs\r
+public class DownloadEventArgs : TaskEventArgs\r
{\r
/// <summary>\r
/// イベントの種類\r
/// </summary>\r
- public DownloadEventType Type;\r
+ public DownloadEventType DownloadTaskType;\r
\r
/// <summary>\r
/// ダウンロード済みのバイト数\r
/// <param name="max">ダウンロードする総バイト数</param>\r
public DownloadEventArgs(DownloadEventType type, string msg, long pos, long max)\r
{\r
- Type = type;\r
+ DownloadTaskType = type;\r
DownloadSize = pos;\r
MaxSize = max;\r
\r
TaskMessage = msg;\r
- TaskProgressPercent = (max > 0)? (100.0f * pos / max) : -1;\r
+ ProgressPercent = (max > 0)? (100.0f * pos / max) : -1;\r
}\r
}\r
\r
/// <summary>\r
/// ダウンロード処理を行うクラス\r
/// </summary>\r
-public class Downloader : NaGetTask\r
+public class Downloader : Task\r
{\r
public IWebProxy proxy;\r
\r
/// <summary>\r
/// アクセスURL\r
/// </summary>\r
- protected string url;\r
+ protected Uri url;\r
\r
/// <summary>\r
/// 保存先\r
/// <param name="filepath">保存先ファイルパス</param>\r
public void Download(string url, string filepath)\r
{\r
- this.url = url;\r
+ this.url = new Uri(url);\r
this.filepath = filepath;\r
\r
try {\r
request.Proxy = this.Proxy;\r
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(CacheLevel);\r
\r
+ HttpWebRequest httpRequest = request as HttpWebRequest;\r
+ if (httpRequest != null) {\r
+ httpRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; \r
+ httpRequest.UserAgent = "AppliStation/1.3";\r
+ }\r
+ \r
if (cancelCalled) {\r
- throw new NaGetTaskCanceledException(string.Empty);\r
+ throw new TaskCanceledException(string.Empty);\r
}\r
\r
try {\r
response = request.GetResponse();\r
} catch (WebException e) {\r
if (cancelCalled) { // キャンセル時\r
- throw new NaGetTaskCanceledException(string.Empty, e);\r
+ throw new TaskCanceledException(string.Empty, e);\r
} else {\r
throw new WebException(e.Message, e);\r
}\r
}\r
\r
if (cancelCalled) {\r
- throw new NaGetTaskCanceledException(string.Empty);\r
+ throw new TaskCanceledException(string.Empty);\r
}\r
\r
try {\r
fs.Write(data, 0, size);\r
\r
if (cancelCalled) {\r
- throw new NaGetTaskCanceledException(string.Empty);\r
+ throw new TaskCanceledException(string.Empty);\r
}\r
}\r
} finally {\r
RaiseDownloadEvent(DownloadEventType.COMPLETED, fs.Position, contentLength);\r
} catch (IOException ex) {\r
if (cancelCalled) {\r
- throw new NaGetTaskCanceledException(string.Empty);\r
+ throw new TaskCanceledException(string.Empty);\r
} else {\r
RaiseDownloadEvent(DownloadEventType.ERROR, 0, 0);\r
throw new IOException(ex.Message, ex);\r
\r
// 更新日を補完\r
if (File.Exists(filepath)) {\r
- if (response is HttpWebResponse) {\r
- File.SetLastWriteTime(filepath, ((HttpWebResponse) response).LastModified);\r
- } else if (response is FtpWebResponse) {\r
- File.SetLastWriteTime(filepath, ((FtpWebResponse) response).LastModified);\r
+ HttpWebResponse httpResponse = response as HttpWebResponse;\r
+ FtpWebResponse ftpResponse = response as FtpWebResponse;\r
+ \r
+ if (httpResponse != null) {\r
+ File.SetLastWriteTime(filepath, httpResponse.LastModified);\r
+ } else if (ftpResponse != null) {\r
+ File.SetLastWriteTime(filepath, ftpResponse.LastModified);\r
}\r
}\r
} finally {\r
if (DownloadEventRaised != null) {\r
DownloadEventArgs e = new DownloadEventArgs(type, string.Empty, pos, max);\r
\r
- switch (e.Type) {\r
+ switch (e.DownloadTaskType) {\r
case DownloadEventType.CONNECTED:\r
e.TaskMessage = "接続しました";\r
break;\r
case DownloadEventType.STARTED:\r
case DownloadEventType.DOWNLOADING:\r
case DownloadEventType.COMPLETED:\r
- if (e.TaskProgressPercent >= 0) {\r
- e.TaskMessage = string.Format("{0} bytes ({1} %)", e.DownloadSize, (int) e.TaskProgressPercent);\r
+ if (e.ProgressPercent >= 0) {\r
+ e.TaskMessage = string.Format("{0} bytes ({1} %)", e.DownloadSize, (int) e.ProgressPercent);\r
} else {\r
e.TaskMessage = string.Format("{0} bytes", e.DownloadSize);\r
}\r
\r
if (stopwatch != null && stopwatch.IsRunning && stopwatch.ElapsedMilliseconds > 3000) {\r
long bpers = e.DownloadSize * 1000 / stopwatch.ElapsedMilliseconds;\r
- if ((e.TaskProgressPercent >= 0) && (bpers > 0)) {\r
+ if ((e.ProgressPercent >= 0) && (bpers > 0)) {\r
TimeSpan rest = TimeSpan.FromSeconds((max - e.DownloadSize) / bpers);\r
e.TaskMessage += string.Format(" 推定残り時間:{0} ({1}/s)", rest, NaGet.Utils.FormatSize(bpers));\r
} else {\r
/// <summary>\r
/// Webレスポンスからダウンロードしたファイルの名前を取得\r
/// </summary>\r
- /// <param name="response"></param>\r
- /// <returns></returns>\r
- private string getFileNameFromWebResponse(WebResponse response)\r
+ /// <remarks>Content-Dispositionヘッダから取得あるいはURLの末尾から推定します</remarks>\r
+ /// <param name="response">レスポンスオブジェクト</param>\r
+ /// <returns>取得したファイル名</returns>\r
+ private static string getFileNameFromWebResponse(WebResponse response)\r
{\r
- if (response is HttpWebResponse) {\r
- string contentDisposition = ((HttpWebResponse) response).Headers["Content-Disposition"];\r
+ HttpWebResponse httpresp = response as HttpWebResponse;\r
+ if (httpresp != null) {\r
+ string contentDisposition = httpresp.Headers["Content-Disposition"];\r
\r
- // TODO check license for http://www.atmarkit.co.jp/fdotnet/dotnettips/618downnoname/downnoname.html\r
if (! string.IsNullOrEmpty(contentDisposition)) {\r
- System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex(\r
- @"filename\s*=\s*(?:""(?<filename>[^""]*)""|(?<filename>[^;]*))",\r
- System.Text.RegularExpressions.RegexOptions.IgnoreCase);\r
- \r
- System.Text.RegularExpressions.Match m = re.Match(contentDisposition);\r
- if (m.Success) {\r
- return m.Groups["filename"].Value;\r
+ try {\r
+ ContentDisposition parser = new ContentDisposition(contentDisposition);\r
+ if (! string.IsNullOrEmpty(parser.FileName)) {\r
+ return parser.FileName;\r
+ }\r
+ } catch (FormatException) {\r
}\r
}\r
}\r
\r
- return NaGet.Utils.Url2filename(response.ResponseUri.ToString());\r
+ return NaGet.Utils.Url2filename(response.ResponseUri);\r
}\r
}\r
\r