X-Git-Url: http://git.sourceforge.jp/view?p=applistation%2FAppliStation.git;a=blobdiff_plain;f=na-get-lib%2FNaGet.Net%2FDownloader.cs;h=5a0a7216afd6abcf985bba068453353afbfd053d;hp=f4c3670874c385c3b939126101a766560ad0c3b8;hb=9e8830beb9bdcf182ca7e84c92a67533bbb1a76c;hpb=30c6c06dcc96976e0a7a8c7add4c6db5c9e5ce3f diff --git a/na-get-lib/NaGet.Net/Downloader.cs b/na-get-lib/NaGet.Net/Downloader.cs index f4c3670..5a0a721 100644 --- a/na-get-lib/NaGet.Net/Downloader.cs +++ b/na-get-lib/NaGet.Net/Downloader.cs @@ -1,9 +1,9 @@ using System; using System.Net; +using System.Net.Mime; using System.IO; -using System.Collections; using System.Threading; -using NaGet.SubCommands; +using NaGet.Tasks; namespace NaGet.Net { @@ -11,12 +11,12 @@ namespace NaGet.Net /// /// ダウンロードイベントオブジェクト /// -public class DownloadEventArgs : NaGetEventArgs +public class DownloadEventArgs : TaskEventArgs { /// /// イベントの種類 /// - public DownloadEventType Type; + public DownloadEventType DownloadTaskType; /// /// ダウンロード済みのバイト数 @@ -37,12 +37,12 @@ public class DownloadEventArgs : NaGetEventArgs /// ダウンロードする総バイト数 public DownloadEventArgs(DownloadEventType type, string msg, long pos, long max) { - Type = type; + DownloadTaskType = type; DownloadSize = pos; MaxSize = max; TaskMessage = msg; - TaskProgressPercent = (max > 0)? (100.0f * pos / max) : -1; + ProgressPercent = (max > 0)? (100.0f * pos / max) : -1; } } @@ -61,7 +61,7 @@ public enum DownloadEventType { /// /// ダウンロード処理を行うクラス /// -public class Downloader : NaGetTask +public class Downloader : Task { public IWebProxy proxy; @@ -73,7 +73,7 @@ public class Downloader : NaGetTask /// /// アクセスURL /// - protected string url; + protected Uri url; /// /// 保存先 @@ -129,7 +129,7 @@ public class Downloader : NaGetTask /// 保存先ファイルパス public void Download(string url, string filepath) { - this.url = url; + this.url = new Uri(url); this.filepath = filepath; try { @@ -165,22 +165,28 @@ public class Downloader : NaGetTask request.Proxy = this.Proxy; request.CachePolicy = new System.Net.Cache.RequestCachePolicy(CacheLevel); + HttpWebRequest httpRequest = request as HttpWebRequest; + if (httpRequest != null) { + httpRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip; + httpRequest.UserAgent = "AppliStation/1.3"; + } + if (cancelCalled) { - throw new NaGetTaskCanceledException(string.Empty); + throw new TaskCanceledException(string.Empty); } try { response = request.GetResponse(); } catch (WebException e) { if (cancelCalled) { // キャンセル時 - throw new NaGetTaskCanceledException(string.Empty, e); + throw new TaskCanceledException(string.Empty, e); } else { throw new WebException(e.Message, e); } } if (cancelCalled) { - throw new NaGetTaskCanceledException(string.Empty); + throw new TaskCanceledException(string.Empty); } try { @@ -222,7 +228,7 @@ public class Downloader : NaGetTask fs.Write(data, 0, size); if (cancelCalled) { - throw new NaGetTaskCanceledException(string.Empty); + throw new TaskCanceledException(string.Empty); } } } finally { @@ -234,7 +240,7 @@ public class Downloader : NaGetTask RaiseDownloadEvent(DownloadEventType.COMPLETED, fs.Position, contentLength); } catch (IOException ex) { if (cancelCalled) { - throw new NaGetTaskCanceledException(string.Empty); + throw new TaskCanceledException(string.Empty); } else { RaiseDownloadEvent(DownloadEventType.ERROR, 0, 0); throw new IOException(ex.Message, ex); @@ -249,10 +255,13 @@ public class Downloader : NaGetTask // 更新日を補完 if (File.Exists(filepath)) { - if (response is HttpWebResponse) { - File.SetLastWriteTime(filepath, ((HttpWebResponse) response).LastModified); - } else if (response is FtpWebResponse) { - File.SetLastWriteTime(filepath, ((FtpWebResponse) response).LastModified); + HttpWebResponse httpResponse = response as HttpWebResponse; + FtpWebResponse ftpResponse = response as FtpWebResponse; + + if (httpResponse != null) { + File.SetLastWriteTime(filepath, httpResponse.LastModified); + } else if (ftpResponse != null) { + File.SetLastWriteTime(filepath, ftpResponse.LastModified); } } } finally { @@ -270,15 +279,15 @@ public class Downloader : NaGetTask if (DownloadEventRaised != null) { DownloadEventArgs e = new DownloadEventArgs(type, string.Empty, pos, max); - switch (e.Type) { + switch (e.DownloadTaskType) { case DownloadEventType.CONNECTED: e.TaskMessage = "接続しました"; break; case DownloadEventType.STARTED: case DownloadEventType.DOWNLOADING: case DownloadEventType.COMPLETED: - if (e.TaskProgressPercent >= 0) { - e.TaskMessage = string.Format("{0} bytes ({1} %)", e.DownloadSize, (int) e.TaskProgressPercent); + if (e.ProgressPercent >= 0) { + e.TaskMessage = string.Format("{0} bytes ({1} %)", e.DownloadSize, (int) e.ProgressPercent); } else { e.TaskMessage = string.Format("{0} bytes", e.DownloadSize); } @@ -286,7 +295,7 @@ public class Downloader : NaGetTask if (stopwatch != null && stopwatch.IsRunning && stopwatch.ElapsedMilliseconds > 3000) { long bpers = e.DownloadSize * 1000 / stopwatch.ElapsedMilliseconds; - if ((e.TaskProgressPercent >= 0) && (bpers > 0)) { + if ((e.ProgressPercent >= 0) && (bpers > 0)) { TimeSpan rest = TimeSpan.FromSeconds((max - e.DownloadSize) / bpers); e.TaskMessage += string.Format(" 推定残り時間:{0} ({1}/s)", rest, NaGet.Utils.FormatSize(bpers)); } else { @@ -331,27 +340,27 @@ public class Downloader : NaGetTask /// /// Webレスポンスからダウンロードしたファイルの名前を取得 /// - /// - /// - private string getFileNameFromWebResponse(WebResponse response) + /// Content-Dispositionヘッダから取得あるいはURLの末尾から推定します + /// レスポンスオブジェクト + /// 取得したファイル名 + private static string getFileNameFromWebResponse(WebResponse response) { - if (response is HttpWebResponse) { - string contentDisposition = ((HttpWebResponse) response).Headers["Content-Disposition"]; + HttpWebResponse httpresp = response as HttpWebResponse; + if (httpresp != null) { + string contentDisposition = httpresp.Headers["Content-Disposition"]; - // TODO check license for http://www.atmarkit.co.jp/fdotnet/dotnettips/618downnoname/downnoname.html if (! string.IsNullOrEmpty(contentDisposition)) { - System.Text.RegularExpressions.Regex re = new System.Text.RegularExpressions.Regex( - @"filename\s*=\s*(?:""(?[^""]*)""|(?[^;]*))", - System.Text.RegularExpressions.RegexOptions.IgnoreCase); - - System.Text.RegularExpressions.Match m = re.Match(contentDisposition); - if (m.Success) { - return m.Groups["filename"].Value; + try { + ContentDisposition parser = new ContentDisposition(contentDisposition); + if (! string.IsNullOrEmpty(parser.FileName)) { + return parser.FileName; + } + } catch (FormatException) { } } } - return NaGet.Utils.Url2filename(response.ResponseUri.ToString()); + return NaGet.Utils.Url2filename(response.ResponseUri); } }