OSDN Git Service

HttpTwitter.SendDirectMessageメソッドをTwitterApiクラスに置き換え
[opentween/open-tween.git] / OpenTween / Connection / Imgur.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.IO;
25 using System.Linq;
26 using System.Net.Http;
27 using System.Net.Http.Headers;
28 using System.Text;
29 using System.Threading.Tasks;
30 using System.Xml.Linq;
31 using OpenTween.Api.DataModel;
32
33 namespace OpenTween.Connection
34 {
35     public class Imgur : IMediaUploadService
36     {
37         private readonly static long MaxFileSize = 10L * 1024 * 1024;
38         private readonly static Uri UploadEndpoint = new Uri("https://api.imgur.com/3/image.xml");
39
40         private readonly static IEnumerable<string> SupportedExtensions = new[]
41         {
42             ".jpg",
43             ".jpeg",
44             ".gif",
45             ".png",
46             ".tif",
47             ".tiff",
48             ".bmp",
49             ".pdf",
50             ".xcf",
51         };
52
53         private readonly Twitter twitter;
54         private TwitterConfiguration twitterConfig;
55
56         public Imgur(Twitter tw, TwitterConfiguration twitterConfig)
57         {
58             this.twitter = tw;
59             this.twitterConfig = twitterConfig;
60         }
61
62         public int MaxMediaCount
63         {
64             get { return 1; }
65         }
66
67         public string SupportedFormatsStrForDialog
68         {
69             get
70             {
71                 var formats = new StringBuilder();
72
73                 foreach (var extension in SupportedExtensions)
74                     formats.AppendFormat("*{0};", extension);
75
76                 return "Image Files(" + formats + ")|" + formats;
77             }
78         }
79
80         public bool CheckFileExtension(string fileExtension)
81         {
82             return SupportedExtensions.Contains(fileExtension, StringComparer.OrdinalIgnoreCase);
83         }
84
85         public bool CheckFileSize(string fileExtension, long fileSize)
86         {
87             var maxFileSize = this.GetMaxFileSize(fileExtension);
88             return maxFileSize == null || fileSize <= maxFileSize.Value;
89         }
90
91         public long? GetMaxFileSize(string fileExtension)
92         {
93             return MaxFileSize;
94         }
95
96         public async Task PostStatusAsync(string text, long? inReplyToStatusId, IMediaItem[] mediaItems)
97         {
98             if (mediaItems == null)
99                 throw new ArgumentNullException(nameof(mediaItems));
100
101             if (mediaItems.Length != 1)
102                 throw new ArgumentOutOfRangeException(nameof(mediaItems));
103
104             var item = mediaItems[0];
105
106             if (item == null)
107                 throw new ArgumentException("Err:Media not specified.");
108
109             if (!item.Exists)
110                 throw new ArgumentException("Err:Media not found.");
111
112             XDocument xml;
113             try
114             {
115                 xml = await this.UploadFileAsync(item, text)
116                     .ConfigureAwait(false);
117             }
118             catch (HttpRequestException ex)
119             {
120                 throw new WebApiException("Err:" + ex.Message, ex);
121             }
122             catch (OperationCanceledException ex)
123             {
124                 throw new WebApiException("Err:Timeout", ex);
125             }
126
127             var imageElm = xml.Element("data");
128
129             if (imageElm.Attribute("success").Value != "1")
130                 throw new WebApiException("Err:" + imageElm.Attribute("status").Value);
131
132             var imageUrl = imageElm.Element("link").Value;
133
134             var textWithImageUrl = text + " " + imageUrl.Trim();
135
136             await this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId)
137                 .ConfigureAwait(false);
138         }
139
140         public int GetReservedTextLength(int mediaCount)
141         {
142             return this.twitterConfig.ShortUrlLength;
143         }
144
145         public void UpdateTwitterConfiguration(TwitterConfiguration config)
146         {
147             this.twitterConfig = config;
148         }
149
150         public async Task<XDocument> UploadFileAsync(IMediaItem item, string title)
151         {
152             using (var content = new MultipartFormDataContent())
153             using (var mediaStream = item.OpenRead())
154             using (var mediaContent = new StreamContent(mediaStream))
155             using (var titleContent = new StringContent(title))
156             {
157                 content.Add(mediaContent, "image", item.Name);
158                 content.Add(titleContent, "title");
159
160                 using (var request = new HttpRequestMessage(HttpMethod.Post, UploadEndpoint))
161                 {
162                     request.Headers.Authorization =
163                         new AuthenticationHeaderValue("Client-ID", ApplicationSettings.ImgurClientID);
164                     request.Content = content;
165
166                     using (var response = await Networking.Http.SendAsync(request).ConfigureAwait(false))
167                     {
168                         response.EnsureSuccessStatusCode();
169
170                         using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
171                         {
172                             return XDocument.Load(stream);
173                         }
174                     }
175                 }
176             }
177         }
178     }
179 }