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.Threading.Tasks;
35 using System.Xml.Linq;
36 using System.Xml.XPath;
37 using OpenTween.Api.DataModel;
39 namespace OpenTween.Connection
41 public class imgly : IMediaUploadService
43 private readonly string[] pictureExt = new[] { ".jpg", ".jpeg", ".gif", ".png" };
44 private readonly long MaxFileSize = 4L * 1024 * 1024;
46 private readonly Twitter tw;
47 private readonly ImglyApi imglyApi;
49 private TwitterConfiguration twitterConfig;
51 public imgly(Twitter twitter, TwitterConfiguration twitterConfig)
54 this.twitterConfig = twitterConfig;
56 this.imglyApi = new ImglyApi(twitter.AccessToken, twitter.AccessTokenSecret);
59 public int MaxMediaCount
64 public string SupportedFormatsStrForDialog
68 return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
72 public bool CheckFileExtension(string fileExtension)
74 return this.pictureExt.Contains(fileExtension.ToLowerInvariant());
77 public bool CheckFileSize(string fileExtension, long fileSize)
79 var maxFileSize = this.GetMaxFileSize(fileExtension);
80 return maxFileSize == null || fileSize <= maxFileSize.Value;
83 public long? GetMaxFileSize(string fileExtension)
88 public async Task PostStatusAsync(string text, long? inReplyToStatusId, IMediaItem[] mediaItems)
90 if (mediaItems == null)
91 throw new ArgumentNullException(nameof(mediaItems));
93 if (mediaItems.Length != 1)
94 throw new ArgumentOutOfRangeException(nameof(mediaItems));
96 var item = mediaItems[0];
99 throw new ArgumentException("Err:Media not specified.");
102 throw new ArgumentException("Err:Media not found.");
104 var xml = await this.imglyApi.UploadFileAsync(item, text)
105 .ConfigureAwait(false);
107 var imageUrlElm = xml.XPathSelectElement("/image/url");
108 if (imageUrlElm == null)
109 throw new WebApiException("Invalid API response", xml.ToString());
111 var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
113 await this.tw.PostStatus(textWithImageUrl, inReplyToStatusId)
114 .ConfigureAwait(false);
117 public int GetReservedTextLength(int mediaCount)
119 return this.twitterConfig.ShortUrlLength;
122 public void UpdateTwitterConfiguration(TwitterConfiguration config)
124 this.twitterConfig = config;
127 public class ImglyApi : HttpConnectionOAuthEcho
129 private static readonly Uri UploadEndpoint = new Uri("http://img.ly/api/2/upload.xml");
131 public ImglyApi(string twitterAccessToken, string twitterAccessTokenSecret)
132 : base(new Uri("http://api.twitter.com/"), new Uri("https://api.twitter.com/1.1/account/verify_credentials.json"))
134 this.Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret,
135 twitterAccessToken, twitterAccessTokenSecret, "", "");
137 this.InstanceTimeout = 60000;
143 /// <exception cref="WebApiException"/>
144 /// <exception cref="XmlException"/>
145 public async Task<XDocument> UploadFileAsync(IMediaItem item, string message)
147 // 参照: http://img.ly/api
149 var param = new Dictionary<string, string>
151 ["message"] = message,
153 var paramFiles = new List<KeyValuePair<string, IMediaItem>>
155 new KeyValuePair<string, IMediaItem>("media", item),
159 var uploadTask = Task.Run(() => this.GetContent(HttpConnection.PostMethod,
160 UploadEndpoint, param, paramFiles, ref response, null, null));
162 var ret = await uploadTask.ConfigureAwait(false);
164 if (ret != HttpStatusCode.OK)
165 throw new WebApiException("Err:" + ret, response);
167 return XDocument.Parse(response);