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;
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.Length != 1)
99 throw new ArgumentOutOfRangeException("mediaItems");
101 var item = mediaItems[0] as FileMediaItem;
104 throw new NotImplementedException();
107 throw new ArgumentException("File isn't exists.", "mediaItems[0]");
112 xml = await this.UploadFileAsync(item.FileInfo, text)
113 .ConfigureAwait(false);
115 catch (HttpRequestException ex)
117 throw new WebApiException("Err:" + ex.Message, ex);
120 var imageElm = xml.Element("data");
122 if (imageElm.Attribute("success").Value != "1")
123 throw new WebApiException("Err:" + imageElm.Attribute("status").Value);
125 var imageUrl = imageElm.Element("link").Value;
127 var textWithImageUrl = text + " " + imageUrl.Trim();
129 await Task.Run(() => this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId))
130 .ConfigureAwait(false);
133 public int GetReservedTextLength(int mediaCount)
135 return this.twitterConfig.ShortUrlLength;
138 public void UpdateTwitterConfiguration(TwitterConfiguration config)
140 this.twitterConfig = config;
143 public async Task<XDocument> UploadFileAsync(FileInfo file, string title)
145 using (var content = new MultipartFormDataContent())
146 using (var fileStream = file.OpenRead())
147 using (var fileContent = new StreamContent(fileStream))
148 using (var titleContent = new StringContent(title))
150 content.Add(fileContent, "image", file.Name);
151 content.Add(titleContent, "title");
153 using (var request = new HttpRequestMessage(HttpMethod.Post, UploadEndpoint))
155 request.Headers.Authorization =
156 new AuthenticationHeaderValue("Client-ID", ApplicationSettings.ImgurClientID);
157 request.Content = content;
159 using (var response = await Networking.Http.SendAsync(request).ConfigureAwait(false))
161 response.EnsureSuccessStatusCode();
163 using (var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
165 return XDocument.Load(stream);