protected override void OnDownloading(ImageHeaderEventArgs e) { base.OnDownloading(e); if (string.IsNullOrEmpty(e.Url)) { e.Status = ImageDownloadResultStatus.FailedReplaceExtractUrl; } }
protected override void OnDownloading(ImageHeaderEventArgs e) { base.OnDownloading(e); if (e.Cancel) { return; } //ダウンロードページ取得 string cushionPageUrl = e.Url + ".html"; InternetClient.DownloadResult cushionPage = InternetClient.DownloadData(cushionPageUrl, DateTime.MinValue, Encoding.GetEncoding("shift_jis")); if (!cushionPage.Success) { //ToDo: ダウンロード失敗時処理 e.Status = ImageDownloadResultStatus.Failed; e.Cancel = true; return; } //画像URL取得 string downloadUrl = string.Empty; if (Regex.IsMatch(cushionPage.Data, @" <form action=""http://www.dotup.org/upload.cgi"" method=""post"" name=""DL"" id=""DL"">")) { try { downloadUrl = GetSecureDownloadUrl(cushionPage.Data, new Uri(e.Url)); if (downloadUrl == string.Empty) { //パスが不一致 Secure = true; Downloadable = false; e.Status = ImageDownloadResultStatus.PasswordMissMatch; e.Cancel = true; } } catch (InvalidOperationException) { Secure = true; Downloadable = false; e.Status = ImageDownloadResultStatus.Failed; e.Cancel = true; return; } } else { //パスなし downloadUrl = e.Url; } if (!e.Cancel && !Uri.IsWellFormedUriString(downloadUrl, UriKind.Absolute)) { e.Status = ImageDownloadResultStatus.InvalidUriFormat; e.Cancel = true; Downloadable = false; } else { e.Url = downloadUrl; } }
protected override void OnDownloading(ImageHeaderEventArgs e) { base.OnDownloading(e); if (e.Cancel) { return; } //ダウンロードページ取得 InternetClient.DownloadResult cushionPage = InternetClient.DownloadData(e.Url, DateTime.MinValue, Encoding.UTF8); if (!cushionPage.Success) { //ToDo: ダウンロード失敗時処理 e.Status = ImageDownloadResultStatus.Failed; e.Cancel = true; return; } //画像Url取得 string downloadUrl = string.Empty; if (Regex.IsMatch(cushionPage.Data, @"<div class=""imgbox_\w"">")) { //画像認証あり Secure = true; Downloadable = false; e.Status = ImageDownloadResultStatus.BlockedImagePass; e.Cancel = true; return; } else { //画像認証なし if (!Regex.IsMatch(cushionPage.Data, @"name=""keyword""")) { //パスなし try { downloadUrl = GetDownloadUrl(cushionPage.Data, new Uri(e.Url)); } catch (System.Net.ProtocolViolationException) { e.Status = ImageDownloadResultStatus.Failed; e.Cancel = true; return; } } else { try { downloadUrl = GetSecureDownloadUrl(cushionPage.Data, new Uri(e.Url)); if (downloadUrl == string.Empty) { //パスが不一致 Secure = true; Downloadable = false; e.Status = ImageDownloadResultStatus.PasswordMissMatch; e.Cancel = true; return; } } catch (InvalidOperationException) { Secure = true; Downloadable = false; e.Status = ImageDownloadResultStatus.Failed; e.Cancel = true; return; } } } if (!Uri.IsWellFormedUriString(downloadUrl, UriKind.Absolute)) { e.Status = ImageDownloadResultStatus.InvalidUriFormat; e.Cancel = true; Downloadable = false; } else { e.Url = downloadUrl; } }
protected virtual void OnResponseStreamReceived(ImageHeaderEventArgs e) { if (e.ResponseHeader.ContentLength > 0) { Common.CurrentSettings.Information.CurrentDownloadByte += e.ResponseHeader.ContentLength; if (e.DownloadedData.LongLength != e.ResponseHeader.ContentLength) { e.Status = ImageDownloadResultStatus.ContentLengthMissMatch; e.Cancel = true; return; } } }
protected virtual void OnResponseHeaderReceived(ImageHeaderEventArgs e) { if (AllowContentTypeTexts.All(type => !e.ResponseHeader.ContentType.Contains(type))) { e.Status = ImageDownloadResultStatus.ContentTypeMissMatch; e.Cancel = true; return; } try { string defaultFileName = ResponseUtility.GetDefaultFileName(e.ResponseHeader); string invalidableFileName = string.IsNullOrEmpty(defaultFileName) ? Path.GetFileName(e.ResponseHeader.ResponseUri.AbsolutePath) : defaultFileName; string safeFileName = Utility.FileNameFormat.EscapeFileName(invalidableFileName); string extension = Utility.FileNameFormat.GetImageExteinsion(safeFileName); if (extension == string.Empty) { extension = Path.GetExtension(e.ResponseHeader.ResponseUri.AbsolutePath); fileName = safeFileName + extension; } else { fileName = safeFileName; } } catch (ArgumentException) { fileName = DefaultFileName; } if (ResponseHeaderReseived != null) { ResponseHeaderReseived(this, e); } }
protected virtual void OnDownloading(ImageHeaderEventArgs e) { if (triedCount > Common.Options.ImageSaveOptions.MaximumRetryCount) { e.Status = ImageDownloadResultStatus.OverTriedCount; e.Cancel = true; downloadable = false; return; } else if (triedCount > 0 && DateTime.Now - firstDownloadedTime > TimeSpan.FromDays(Common.Options.ImageSaveOptions.RetryImageLifeDate)) { e.Status = ImageDownloadResultStatus.OverLimitDate; e.Cancel = true; downloadable = false; return; } if (!Uri.IsWellFormedUriString(e.Url, UriKind.Absolute)) { e.Status = ImageDownloadResultStatus.InvalidUriFormat; e.Cancel = true; downloadable = false; return; } if (Downloading != null) { Downloading(this, e); } //if (!e.Cancel) //{ // Common.UploaderLimiter.Wait(e.Host); // e.Locked = true; //} }
protected virtual void OnDownloaded(ImageHeaderEventArgs e) { //if (e.Locked) //{ // Common.UploaderLimiter.Release(e.Host); // e.Locked = false; //} if (e.TriedDownload) { triedCount++; } if (e.Downloaded) { firstDownloadedTime = DateTime.Now; // ハッシュ値を計算 MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider(); md5Hash = BitConverter.ToString(md5.ComputeHash(e.DownloadedData)).Replace("-", "").ToLower(); downloadCompleted = true; downloadable = false; state = ImageState.DownloadComplete; } else { state = downloadable ? ImageState.DownloadPause : ImageState.DownloadFailed; } if (Downloaded != null) { Downloaded(this, e); } }
protected virtual ImageDownloadResult Download(string url, string referer = "", CookieContainer cookie = null) { if (downloadCompleted) { throw new InvalidOperationException("This file is already downloaded"); } if (!downloadable) { throw new InvalidOperationException("This file can not download"); } ImageHeaderEventArgs e = new ImageHeaderEventArgs(url); try { OnDownloading(e); if (e.Cancel) { OnDownloaded(e); return new ImageDownloadResult(e.Status); } e.TriedDownload = true; using (HttpWebResponse response = InternetClient.GetResponse(e.Url, referer, cookie)) { e.ResponseHeader = response; OnResponseHeaderReceived(e); if (e.Cancel) { OnDownloaded(e); return new ImageDownloadResult(e.Status); } using (Stream result = response.GetResponseStream()) { byte[] data = StreamUtility.ReadBytes(result); e.DownloadedData = data; OnResponseStreamReceived(e); if (e.Cancel) { OnDownloaded(e); return new ImageDownloadResult(e.Status); } e.Downloaded = true; OnDownloaded(e); return new ImageDownloadResult(data, e.Url, referer, ResponseUtility.TryLastModified(response), response.ContentType); } } } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { if (ex.Response == null) { throw; } HttpWebResponse errorResponse = (HttpWebResponse)ex.Response; if (ImpossibleDownloadStatuses.Contains(errorResponse.StatusCode)) { downloadable = false; e.Downloaded = false; } else { throw; } } else//プロトコルエラー以外の例外 { //ダウンロード可能(リトライ) downloadable = true; e.Downloaded = false; } } OnDownloaded(e); return new ImageDownloadResult(e.Status); }