1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 // (c) 2008-2011 Moz (@syo68k)
4 // (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 // (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 // (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 // (c) 2011 spinor (@tplantd) <http://d.hatena.ne.jp/spinor/>
8 // (c) 2014 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
11 // This file is part of OpenTween.
13 // This program is free software; you can redistribute it and/or modify it
14 // under the terms of the GNU General Public License as published by the Free
15 // Software Foundation; either version 3 of the License, or (at your option)
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 // You should have received a copy of the GNU General Public License along
24 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
25 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 // Boston, MA 02110-1301, USA.
29 using System.Collections.Generic;
33 using System.Net.Http;
34 using System.Threading.Tasks;
36 using System.Xml.Linq;
37 using System.Xml.XPath;
39 using OpenTween.Api.DataModel;
41 namespace OpenTween.Connection
43 public class imgly : IMediaUploadService
45 private readonly string[] pictureExt = { ".jpg", ".jpeg", ".gif", ".png" };
46 private readonly long MaxFileSize = 4L * 1024 * 1024;
48 private readonly Twitter tw;
49 private readonly ImglyApi imglyApi;
51 private TwitterConfiguration twitterConfig;
53 public imgly(Twitter twitter, TwitterConfiguration twitterConfig)
56 this.twitterConfig = twitterConfig;
58 this.imglyApi = new ImglyApi(twitter.Api);
61 public int MaxMediaCount
66 public string SupportedFormatsStrForDialog
70 return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
74 public bool CanUseAltText => false;
76 public bool CheckFileExtension(string fileExtension)
78 return this.pictureExt.Contains(fileExtension.ToLowerInvariant());
81 public bool CheckFileSize(string fileExtension, long fileSize)
83 var maxFileSize = this.GetMaxFileSize(fileExtension);
84 return maxFileSize == null || fileSize <= maxFileSize.Value;
87 public long? GetMaxFileSize(string fileExtension)
92 public async Task PostStatusAsync(string text, long? inReplyToStatusId, IMediaItem[] mediaItems)
94 if (mediaItems == null)
95 throw new ArgumentNullException(nameof(mediaItems));
97 if (mediaItems.Length != 1)
98 throw new ArgumentOutOfRangeException(nameof(mediaItems));
100 var item = mediaItems[0];
103 throw new ArgumentException("Err:Media not specified.");
106 throw new ArgumentException("Err:Media not found.");
108 var xml = await this.imglyApi.UploadFileAsync(item, text)
109 .ConfigureAwait(false);
111 var imageUrlElm = xml.XPathSelectElement("/image/url");
112 if (imageUrlElm == null)
113 throw new WebApiException("Invalid API response", xml.ToString());
115 var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
117 await this.tw.PostStatus(textWithImageUrl, inReplyToStatusId)
118 .ConfigureAwait(false);
121 public int GetReservedTextLength(int mediaCount)
123 return this.twitterConfig.ShortUrlLength;
126 public void UpdateTwitterConfiguration(TwitterConfiguration config)
128 this.twitterConfig = config;
131 public class ImglyApi
133 private readonly HttpClient http;
135 private static readonly Uri UploadEndpoint = new Uri("http://img.ly/api/2/upload.xml");
137 private static readonly Uri OAuthRealm = new Uri("http://api.twitter.com/");
138 private static readonly Uri AuthServiceProvider = new Uri("https://api.twitter.com/1.1/account/verify_credentials.json");
140 public ImglyApi(TwitterApi twitterApi)
142 var handler = twitterApi.CreateOAuthEchoHandler(AuthServiceProvider, OAuthRealm);
144 this.http = Networking.CreateHttpClient(handler);
145 this.http.Timeout = TimeSpan.FromMinutes(1);
151 /// <exception cref="WebApiException"/>
152 /// <exception cref="XmlException"/>
153 public async Task<XDocument> UploadFileAsync(IMediaItem item, string message)
155 // 参照: http://img.ly/api
157 using (var request = new HttpRequestMessage(HttpMethod.Post, UploadEndpoint))
158 using (var multipart = new MultipartFormDataContent())
160 request.Content = multipart;
162 using (var messageContent = new StringContent(message))
163 using (var mediaStream = item.OpenRead())
164 using (var mediaContent = new StreamContent(mediaStream))
166 multipart.Add(messageContent, "message");
167 multipart.Add(mediaContent, "media", item.Name);
169 using (var response = await this.http.SendAsync(request).ConfigureAwait(false))
171 var responseText = await response.Content.ReadAsStringAsync()
172 .ConfigureAwait(false);
174 if (!response.IsSuccessStatusCode)
175 throw new WebApiException(response.StatusCode.ToString(), responseText);
177 return XDocument.Parse(responseText);