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;
39 namespace OpenTween.Connection
41 public class TwitPic : IMediaUploadService
43 private readonly string[] pictureExt = new[] { ".jpg", ".jpeg", ".gif", ".png" };
45 private readonly string[] multimediaExt = new[] { ".avi", ".wmv", ".flv", ".m4v", ".mov", ".mp4", ".rm", ".mpeg", ".mpg", ".3gp", ".3g2" };
47 private readonly long MaxFileSize = 10L * 1024 * 1024; // Image only
48 // Multimedia filesize limit unknown. But length limit is 1:30.
50 private readonly Twitter tw;
51 private readonly TwitpicApi twitpicApi;
53 private TwitterConfiguration twitterConfig;
55 public TwitPic(Twitter twitter, TwitterConfiguration twitterConfig)
58 this.twitterConfig = twitterConfig;
60 this.twitpicApi = new TwitpicApi(twitter.AccessToken, twitter.AccessTokenSecret);
63 public int MaxMediaCount
68 public string SupportedFormatsStrForDialog
72 return "Image Files(*" + string.Join(";*", this.pictureExt) + ")|*" + string.Join(";*", this.pictureExt)
73 + "|Videos(*" + string.Join(";*", this.multimediaExt) + ")|*" + string.Join(";*", this.multimediaExt);
77 public bool CheckFileExtension(string fileExtension)
79 fileExtension = fileExtension.ToLower();
81 return this.pictureExt.Contains(fileExtension) ||
82 this.multimediaExt.Contains(fileExtension);
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)
93 if (this.multimediaExt.Contains(fileExtension))
94 return null; // Multimedia : no check
99 public async Task PostStatusAsync(string text, long? inReplyToStatusId, string[] filePaths)
101 if (filePaths.Length != 1)
102 throw new ArgumentOutOfRangeException("filePaths");
104 var file = new FileInfo(filePaths[0]);
107 throw new ArgumentException("Err:File isn't exists.", "filePaths[0]");
109 var xml = await this.twitpicApi.UploadFileAsync(file, text)
110 .ConfigureAwait(false);
112 var imageUrlElm = xml.XPathSelectElement("/image/url");
113 if (imageUrlElm == null)
114 throw new WebApiException("Invalid API response", xml.ToString());
116 var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
118 await Task.Run(() => this.tw.PostStatus(textWithImageUrl, inReplyToStatusId))
119 .ConfigureAwait(false);
122 public int GetReservedTextLength(int mediaCount)
124 return this.twitterConfig.ShortUrlLength;
127 public void UpdateTwitterConfiguration(TwitterConfiguration config)
129 this.twitterConfig = config;
132 public class TwitpicApi : HttpConnectionOAuthEcho
134 private static readonly Uri UploadEndpoint = new Uri("http://api.twitpic.com/2/upload.xml");
136 public TwitpicApi(string twitterAccessToken, string twitterAccessTokenSecret)
137 : base(new Uri("http://api.twitter.com/"), new Uri("https://api.twitter.com/1.1/account/verify_credentials.json"))
139 this.Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret,
140 twitterAccessToken, twitterAccessTokenSecret, "", "");
142 this.InstanceTimeout = 120000;
148 /// <exception cref="WebApiException"/>
149 /// <exception cref="XmlException"/>
150 public async Task<XDocument> UploadFileAsync(FileInfo file, string message)
152 // 参照: http://dev.twitpic.com/docs/2/upload/
154 var param = new Dictionary<string, string>
156 {"key", ApplicationSettings.TwitpicApiKey},
157 {"message", message},
159 var paramFiles = new List<KeyValuePair<string, FileInfo>>
161 new KeyValuePair<string, FileInfo>("media", file),
165 var uploadTask = Task.Run(() => this.GetContent(HttpConnection.PostMethod,
166 UploadEndpoint, param, paramFiles, ref response, null, null));
168 var ret = await uploadTask.ConfigureAwait(false);
170 if (ret != HttpStatusCode.OK)
171 throw new WebApiException("Err:" + ret, response);
173 return XDocument.Parse(response);