1 // OpenTween - Client of Twitter
2 // Copyright (c) 2013 ANIKITI (@anikiti07) <https://twitter.com/anikiti07>
3 // (c) 2014 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
4 // All rights reserved.
6 // This file is part of OpenTween.
8 // This program is free software; you can redistribute it and/or modify it
9 // under the terms of the GNU General Public License as published by the Free
10 // Software Foundation; either version 3 of the License, or (at your option)
13 // This program is distributed in the hope that it will be useful, but
14 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 // You should have received a copy of the GNU General Public License along
19 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
20 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
21 // Boston, MA 02110-1301, USA.
24 using System.Collections.Generic;
28 using System.Threading.Tasks;
29 using System.Windows.Forms;
31 using System.Xml.Linq;
32 using System.Xml.XPath;
33 using OpenTween.Api.DataModel;
35 namespace OpenTween.Connection
37 public sealed class TwipplePhoto : IMediaUploadService
39 private static readonly long MaxFileSize = 4L * 1024 * 1024;
40 private static readonly IEnumerable<string> SupportedPictureExtensions = new[]
47 private readonly Twitter twitter;
48 private readonly TwippleApi twippleApi;
50 private TwitterConfiguration twitterConfig;
54 public TwipplePhoto(Twitter twitter, TwitterConfiguration twitterConfig)
57 throw new ArgumentNullException(nameof(twitter));
58 if (twitterConfig == null)
59 throw new ArgumentNullException(nameof(twitterConfig));
61 this.twitter = twitter;
62 this.twitterConfig = twitterConfig;
64 this.twippleApi = new TwippleApi(twitter.AccessToken, twitter.AccessTokenSecret);
69 public int MaxMediaCount
74 public string SupportedFormatsStrForDialog
78 var filterFormatExtensions = "";
79 foreach (var pictureExtension in SupportedPictureExtensions)
81 filterFormatExtensions += '*';
82 filterFormatExtensions += pictureExtension;
83 filterFormatExtensions += ';';
85 return "Image Files(" + filterFormatExtensions + ")|" + filterFormatExtensions;
89 public bool CheckFileExtension(string fileExtension)
91 return SupportedPictureExtensions.Contains(fileExtension, StringComparer.OrdinalIgnoreCase);
94 public bool CheckFileSize(string fileExtension, long fileSize)
96 var maxFileSize = this.GetMaxFileSize(fileExtension);
97 return maxFileSize == null || fileSize <= maxFileSize.Value;
100 public long? GetMaxFileSize(string fileExtension)
105 public async Task PostStatusAsync(string text, long? inReplyToStatusId, IMediaItem[] mediaItems)
107 if (mediaItems == null)
108 throw new ArgumentNullException(nameof(mediaItems));
110 if (mediaItems.Length != 1)
111 throw new ArgumentOutOfRangeException(nameof(mediaItems));
113 var item = mediaItems[0];
116 throw new ArgumentException("Err:Media not specified.");
119 throw new ArgumentException("Err:Media not found.");
121 var xml = await this.twippleApi.UploadFileAsync(item)
122 .ConfigureAwait(false);
124 var imageUrlElm = xml.XPathSelectElement("/rsp/mediaurl");
125 if (imageUrlElm == null)
126 throw new WebApiException("Invalid API response", xml.ToString());
128 var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
130 await this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId)
131 .ConfigureAwait(false);
134 public int GetReservedTextLength(int mediaCount)
136 return this.twitterConfig.ShortUrlLength;
139 public void UpdateTwitterConfiguration(TwitterConfiguration config)
141 this.twitterConfig = config;
144 public class TwippleApi : HttpConnectionOAuthEcho
146 private static readonly Uri UploadEndpoint = new Uri("http://p.twipple.jp/api/upload2");
148 public TwippleApi(string twitterAccessToken, string twitterAccessTokenSecret)
149 : base(new Uri("http://api.twitter.com/"), new Uri("https://api.twitter.com/1.1/account/verify_credentials.json"))
151 this.Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret,
152 twitterAccessToken, twitterAccessTokenSecret, "", "");
154 this.InstanceTimeout = 60000;
160 /// <exception cref="WebApiException"/>
161 /// <exception cref="XmlException"/>
162 public async Task<XDocument> UploadFileAsync(IMediaItem item)
164 // 参照: http://p.twipple.jp/wiki/API_Upload2/ja
166 var param = new Dictionary<string, string>
168 ["upload_from"] = Application.ProductName,
170 var paramFiles = new List<KeyValuePair<string, IMediaItem>>
172 new KeyValuePair<string, IMediaItem>("media", item),
176 var uploadTask = Task.Run(() => this.GetContent(HttpConnection.PostMethod,
177 UploadEndpoint, param, paramFiles, ref response, null, null));
179 var ret = await uploadTask.ConfigureAwait(false);
181 if (ret != HttpStatusCode.OK)
182 throw new WebApiException("Err:" + ret, response);
184 return XDocument.Parse(response);