1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
26 using System.Net.Http;
27 using System.Net.Http.Headers;
29 using System.Threading.Tasks;
30 using System.Xml.Linq;
31 using OpenTween.Api.DataModel;
33 namespace OpenTween.Connection
35 public class Imgur : IMediaUploadService
37 private readonly static long MaxFileSize = 10L * 1024 * 1024;
38 private readonly static Uri UploadEndpoint = new Uri("https://api.imgur.com/3/image.xml");
40 private readonly static IEnumerable<string> SupportedExtensions = new[]
53 private readonly Twitter twitter;
54 private TwitterConfiguration twitterConfig;
56 public Imgur(Twitter tw, TwitterConfiguration twitterConfig)
59 this.twitterConfig = twitterConfig;
62 public int MaxMediaCount
67 public string SupportedFormatsStrForDialog
71 var formats = new StringBuilder();
73 foreach (var extension in SupportedExtensions)
74 formats.AppendFormat("*{0};", extension);
76 return "Image Files(" + formats + ")|" + formats;
80 public bool CheckFileExtension(string fileExtension)
82 return SupportedExtensions.Contains(fileExtension, StringComparer.OrdinalIgnoreCase);
85 public bool CheckFileSize(string fileExtension, long fileSize)
87 var maxFileSize = this.GetMaxFileSize(fileExtension);
88 return maxFileSize == null || fileSize <= maxFileSize.Value;
91 public long? GetMaxFileSize(string fileExtension)
96 public async Task PostStatusAsync(string text, long? inReplyToStatusId, IMediaItem[] mediaItems)
98 if (mediaItems == null)
99 throw new ArgumentNullException(nameof(mediaItems));
101 if (mediaItems.Length != 1)
102 throw new ArgumentOutOfRangeException(nameof(mediaItems));
104 var item = mediaItems[0];
107 throw new ArgumentException("Err:Media not specified.");
110 throw new ArgumentException("Err:Media not found.");
115 xml = await this.UploadFileAsync(item, text)
116 .ConfigureAwait(false);
118 catch (HttpRequestException ex)
120 throw new WebApiException("Err:" + ex.Message, ex);
122 catch (OperationCanceledException ex)
124 throw new WebApiException("Err:Timeout", ex);
127 var imageElm = xml.Element("data");
129 if (imageElm.Attribute("success").Value != "1")
130 throw new WebApiException("Err:" + imageElm.Attribute("status").Value);
132 var imageUrl = imageElm.Element("link").Value;
134 var textWithImageUrl = text + " " + imageUrl.Trim();
136 await this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId)
137 .ConfigureAwait(false);
140 public int GetReservedTextLength(int mediaCount)
142 return this.twitterConfig.ShortUrlLength;
145 public void UpdateTwitterConfiguration(TwitterConfiguration config)
147 this.twitterConfig = config;
150 public async Task<XDocument> UploadFileAsync(IMediaItem item, string title)
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))
157 content.Add(mediaContent, "image", item.Name);
158 content.Add(titleContent, "title");
160 using (var request = new HttpRequestMessage(HttpMethod.Post, UploadEndpoint))
162 request.Headers.Authorization =
163 new AuthenticationHeaderValue("Client-ID", ApplicationSettings.ImgurClientID);
164 request.Content = content;
166 using (var response = await Networking.Http.SendAsync(request).ConfigureAwait(false))
168 response.EnsureSuccessStatusCode();
170 using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
172 return XDocument.Load(stream);