OSDN Git Service

ThumbnailInfoクラス内のOTWebClientを使用している箇所をHttpClientに置き換え
authorKimura Youichi <kim.upsilon@bucyou.net>
Tue, 15 Apr 2014 17:36:20 +0000 (02:36 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Sat, 19 Apr 2014 06:15:27 +0000 (15:15 +0900)
14 files changed:
OpenTween/Thumbnail/Services/Foursquare.cs
OpenTween/Thumbnail/Services/ImgAzyobuziNet.cs
OpenTween/Thumbnail/Services/MetaThumbnailService.cs
OpenTween/Thumbnail/Services/Nicovideo.cs
OpenTween/Thumbnail/Services/PhotoShareShortlink.cs
OpenTween/Thumbnail/Services/Pixiv.cs
OpenTween/Thumbnail/Services/SimpleThumbnailService.cs
OpenTween/Thumbnail/Services/Tinami.cs
OpenTween/Thumbnail/Services/Tumblr.cs
OpenTween/Thumbnail/Services/ViaMe.cs
OpenTween/Thumbnail/Services/Vimeo.cs
OpenTween/Thumbnail/Services/Youtube.cs
OpenTween/Thumbnail/ThumbnailInfo.cs
OpenTween/TweetThumbnail.cs

index 3368e7d..a8e6c6b 100644 (file)
@@ -22,6 +22,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net.Http;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading;
@@ -31,10 +32,17 @@ namespace OpenTween.Thumbnail.Services
 {
     class Foursquare : IThumbnailService
     {
+        protected readonly HttpClient http;
         protected Regex regex;
 
         public Foursquare(string pattern)
+            : this(null, pattern)
         {
+        }
+
+        public Foursquare(HttpClient http, string pattern)
+        {
+            this.http = http ?? MyCommon.CreateHttpClient();
             this.regex = new Regex(pattern);
         }
 
@@ -54,7 +62,7 @@ namespace OpenTween.Thumbnail.Services
                 if (mapUrl == null)
                     return null;
 
-                return new ThumbnailInfo()
+                return new ThumbnailInfo(this.http)
                 {
                     ImageUrl = url,
                     ThumbnailUrl = mapUrl,
index 11f26ee..e60e40e 100644 (file)
@@ -23,6 +23,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;
+using System.Net.Http;
 using System.Runtime.Serialization.Json;
 using System.Xml;
 using System.Xml.Linq;
@@ -43,15 +44,34 @@ namespace OpenTween.Thumbnail.Services
         protected IEnumerable<Regex> UrlRegex = null;
         protected Timer UpdateTimer;
 
+        protected readonly HttpClient http;
+
         private object LockObj = new object();
 
-        public ImgAzyobuziNet(bool autoupdate = false)
+        public ImgAzyobuziNet()
+            : this(autoupdate: false)
+        {
+        }
+
+        public ImgAzyobuziNet(bool autoupdate)
+            : this(null, autoupdate)
+        {
+        }
+
+        public ImgAzyobuziNet(HttpClient http)
+            : this(http, autoupdate: false)
+        {
+        }
+
+        public ImgAzyobuziNet(HttpClient http, bool autoupdate)
         {
             this.UpdateTimer = new Timer(_ => this.LoadRegex());
             this.AutoUpdate = autoupdate;
 
             this.Enabled = true;
             this.DisabledInDM = true;
+
+            this.http = http ?? MyCommon.CreateHttpClient();
         }
 
         public bool AutoUpdate
@@ -170,7 +190,7 @@ namespace OpenTween.Thumbnail.Services
                     {
                         if (regex.IsMatch(url))
                         {
-                            return new ThumbnailInfo()
+                            return new ThumbnailInfo(this.http)
                             {
                                 ImageUrl = url,
                                 ThumbnailUrl = this.ApiBase + "redirect?size=large&uri=" + Uri.EscapeDataString(url),
index 4613d93..8fd0cf4 100644 (file)
@@ -62,7 +62,7 @@ namespace OpenTween.Thumbnail.Services
                     var thumbnailUrl = this.GetThumbnailUrl(content);
                     if (string.IsNullOrEmpty(thumbnailUrl)) return null;
 
-                    return new ThumbnailInfo()
+                    return new ThumbnailInfo(this.http)
                     {
                         ImageUrl = url,
                         ThumbnailUrl = thumbnailUrl,
index 0558599..91602f6 100644 (file)
@@ -178,7 +178,7 @@ namespace OpenTween.Thumbnail.Services
 
                     if (!string.IsNullOrEmpty(imgurl))
                     {
-                        return new ThumbnailInfo()
+                        return new ThumbnailInfo(this.http)
                         {
                             ImageUrl = url,
                             ThumbnailUrl = imgurl,
index cc502a2..cd6d8e4 100644 (file)
@@ -22,6 +22,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net.Http;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading;
@@ -31,10 +32,17 @@ namespace OpenTween.Thumbnail.Services
 {
     class PhotoShareShortlink : IThumbnailService
     {
+        protected readonly HttpClient http;
         protected Regex regex;
 
         public PhotoShareShortlink(string pattern)
+            : this(null, pattern)
         {
+        }
+
+        public PhotoShareShortlink(HttpClient http, string pattern)
+        {
+            this.http = http ?? MyCommon.CreateHttpClient();
             this.regex = new Regex(pattern);
         }
 
@@ -46,7 +54,7 @@ namespace OpenTween.Thumbnail.Services
 
                 if (!match.Success) return null;
 
-                return new ThumbnailInfo()
+                return new ThumbnailInfo(this.http)
                 {
                     ImageUrl = url,
                     ThumbnailUrl = "http://images.bcphotoshare.com/storages/" + RadixConvert.ToInt32(match.Result("${1}"), 36) + "/thumb180.jpg",
index 4035d15..ae1927d 100644 (file)
@@ -23,6 +23,7 @@ using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;
+using System.Net.Http;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading;
@@ -44,7 +45,7 @@ namespace OpenTween.Thumbnail.Services
                 var thumb = await base.GetThumbnailInfoAsync(url, post, token);
                 if (thumb == null) return null;
 
-                return new Pixiv.Thumbnail
+                return new Pixiv.Thumbnail(this.http)
                 {
                     ImageUrl = thumb.ImageUrl,
                     ThumbnailUrl = thumb.ThumbnailUrl,
@@ -56,17 +57,27 @@ namespace OpenTween.Thumbnail.Services
 
         public class Thumbnail : ThumbnailInfo
         {
+            public Thumbnail(HttpClient http)
+                : base(http)
+            {
+            }
+
             public async override Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
             {
-                using (var client = new OTWebClient())
+                var request = new HttpRequestMessage(HttpMethod.Get, this.ThumbnailUrl);
+
+                request.Headers.Add("User-Agent", MyCommon.GetUserAgentString(fakeMSIE: true));
+                request.Headers.Referrer = new Uri(this.ImageUrl);
+
+                using (var response = await this.http.SendAsync(request, token).ConfigureAwait(false))
                 {
-                    client.UserAgent = MyCommon.GetUserAgentString(fakeMSIE: true);
-                    client.Headers[HttpRequestHeader.Referer] = this.ImageUrl;
+                    response.EnsureSuccessStatusCode();
 
-                    var imageBytes = await client.DownloadDataAsync(new Uri(this.ThumbnailUrl), token)
+                    var imageStream = await response.Content.ReadAsStreamAsync()
                         .ConfigureAwait(false);
 
-                    return MemoryImage.CopyFromBytes(imageBytes);
+                    return await MemoryImage.CopyFromStreamAsync(imageStream)
+                        .ConfigureAwait(false);
                 }
             }
         }
index ed4e48e..c7fb113 100644 (file)
@@ -22,6 +22,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net.Http;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading;
@@ -38,14 +39,26 @@ namespace OpenTween.Thumbnail.Services
         protected string thumb_replacement;
         protected string fullsize_replacement;
 
+        protected readonly HttpClient http;
+
         public SimpleThumbnailService(string pattern, string replacement)
+            : this(null, pattern, replacement, null)
+        {
+        }
+
+        public SimpleThumbnailService(HttpClient http, string pattern, string replacement)
+            : this(http, pattern, replacement, null)
         {
-            this.regex = new Regex(pattern, RegexOptions.IgnoreCase);
-            this.thumb_replacement = replacement;
         }
 
         public SimpleThumbnailService(string pattern, string replacement, string file_replacement)
+            : this(null, pattern, replacement, file_replacement)
+        {
+        }
+
+        public SimpleThumbnailService(HttpClient http, string pattern, string replacement, string file_replacement)
         {
+            this.http = http ?? MyCommon.CreateHttpClient();
             this.regex = new Regex(pattern, RegexOptions.IgnoreCase);
             this.thumb_replacement = replacement;
             this.fullsize_replacement = file_replacement;
@@ -58,7 +71,7 @@ namespace OpenTween.Thumbnail.Services
                 var thumbnailUrl = this.ReplaceUrl(url);
                 if (thumbnailUrl == null) return null;
 
-                return new ThumbnailInfo()
+                return new ThumbnailInfo(this.http)
                 {
                     ImageUrl = url,
                     ThumbnailUrl = thumbnailUrl,
index e7a4fc3..9e761c6 100644 (file)
@@ -56,7 +56,7 @@ namespace OpenTween.Thumbnail.Services
                     {
                         var descElm = xdoc.XPathSelectElement("/rsp/content/description");
 
-                        return new ThumbnailInfo()
+                        return new ThumbnailInfo(this.http)
                         {
                             ImageUrl = url,
                             ThumbnailUrl = thumbUrlElm.Attribute("url").Value,
index 43b4330..20de664 100644 (file)
@@ -75,7 +75,7 @@ namespace OpenTween.Thumbnail.Services
                         return null;
                     }
 
-                    return new ThumbnailInfo()
+                    return new ThumbnailInfo(this.http)
                     {
                         ImageUrl = url,
                         ThumbnailUrl = imgurl,
index 55f792e..b3a58c7 100644 (file)
@@ -60,7 +60,7 @@ namespace OpenTween.Thumbnail.Services
 
                     var textElm = xElm.XPathSelectElement("/response/post/text");
 
-                    return new ThumbnailInfo()
+                    return new ThumbnailInfo(this.http)
                     {
                         ImageUrl = url,
                         ThumbnailUrl = thumbUrlElm.Value,
index ffa2846..98defb7 100644 (file)
@@ -64,7 +64,7 @@ namespace OpenTween.Thumbnail.Services
                         tooltipText = string.Format("{0} ({1:00}:{2:00})", titleElm.Value, minute, second);
                     }
 
-                    return new ThumbnailInfo()
+                    return new ThumbnailInfo(this.http)
                     {
                         ImageUrl = url,
                         ThumbnailUrl = thumbUrlElm.Value,
index 5bc2095..13deea8 100644 (file)
@@ -171,7 +171,7 @@ namespace OpenTween.Thumbnail.Services
 
                         }
 
-                        return new ThumbnailInfo()
+                        return new ThumbnailInfo(this.http)
                         {
                             ImageUrl = url,
                             ThumbnailUrl = imgUrl,
index 5dcf670..b07d17a 100644 (file)
@@ -22,6 +22,7 @@
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Net.Http;
 using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
@@ -35,6 +36,18 @@ namespace OpenTween.Thumbnail
         public string TooltipText { get; set; }
         public string FullSizeImageUrl { get; set; }
 
+        protected readonly HttpClient http;
+
+        public ThumbnailInfo()
+            : this(null)
+        {
+        }
+
+        public ThumbnailInfo(HttpClient http)
+        {
+            this.http = http ?? MyCommon.CreateHttpClient();
+        }
+
         public Task<MemoryImage> LoadThumbnailImageAsync()
         {
             return this.LoadThumbnailImageAsync(CancellationToken.None);
@@ -42,12 +55,15 @@ namespace OpenTween.Thumbnail
 
         public async virtual Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken token)
         {
-            using (var client = new OTWebClient())
+            using (var response = await this.http.GetAsync(this.ThumbnailUrl, token).ConfigureAwait(false))
             {
-                var imageBytes = await client.DownloadDataAsync(new Uri(this.ThumbnailUrl), token)
+                response.EnsureSuccessStatusCode();
+
+                var imageStream = await response.Content.ReadAsStreamAsync()
                     .ConfigureAwait(false);
 
-                return MemoryImage.CopyFromBytes(imageBytes);
+                return await MemoryImage.CopyFromStreamAsync(imageStream)
+                    .ConfigureAwait(false);
             }
         }
     }
index 43ca945..894df4d 100644 (file)
@@ -26,6 +26,7 @@ using System.Drawing;
 using System.Data;
 using System.Linq;
 using System.Net;
+using System.Net.Http;
 using System.Text;
 using System.Windows.Forms;
 using System.Text.RegularExpressions;
@@ -91,7 +92,9 @@ namespace OpenTween
                         .ContinueWith(t2 =>
                         {
                             if (t2.IsFaulted)
-                                t2.Exception.Flatten().Handle(x => x is WebException || x is InvalidImageException || x is TaskCanceledException);
+                                t2.Exception.Flatten().Handle(x => x is HttpRequestException ||
+                                    x is WebException || x is InvalidImageException ||
+                                    x is TaskCanceledException);
 
                             if (t2.IsFaulted || t2.IsCanceled)
                             {