// (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
// (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
// (c) 2011 spinor (@tplantd) <http://d.hatena.ne.jp/spinor/>
+// (c) 2014 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
// All rights reserved.
-//
+//
// This file is part of OpenTween.
-//
+//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 3 of the License, or (at your option)
// any later version.
-//
+//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
-// for more details.
-//
+// for more details.
+//
// You should have received a copy of the GNU General Public License along
// with this program. If not, see <http://www.gnu.org/licenses/>, or write to
// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
// Boston, MA 02110-1301, USA.
-using IMultimediaShareService = OpenTween.IMultimediaShareService;
-using Array = System.Array;
-using Convert = System.Convert;
-using Exception = System.Exception;
-using UploadFileType = OpenTween.MyCommon.UploadFileType;
-using MyCommon = OpenTween.MyCommon;
-using FileInfo = System.IO.FileInfo;
-using NotSupportedException = System.NotSupportedException;
+using System;
using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Threading.Tasks;
+using OpenTween.Api.DataModel;
-namespace OpenTween
+namespace OpenTween.Connection
{
- public class TwitterPhoto : IMultimediaShareService
- {
- private string[] pictureExt = new string[] { ".jpg", ".jpeg", ".gif", ".png" };
-
- private const long MaxfilesizeDefault = 3145728;
-
- // help/configurationにより取得されコンストラクタへ渡される
- private long _MaxFileSize = 3145728;
-
- private Twitter tw;
-
- public bool CheckValidExtension( string ext )
- {
- if ( Array.IndexOf( this.pictureExt, ext.ToLower() ) > -1 )
- return true;
-
- return false;
- }
-
- public bool CheckValidFilesize( string ext, long fileSize )
- {
- if ( this.CheckValidExtension( ext ) )
- return fileSize <= this._MaxFileSize;
-
- return false;
- }
-
- public bool Configuration( string key, object value )
- {
- if ( key == "MaxUploadFilesize" )
- {
- long val;
- try
- {
- val = Convert.ToInt64( value );
- if ( val > 0 )
- this._MaxFileSize = val;
- else
- this._MaxFileSize = TwitterPhoto.MaxfilesizeDefault;
- }
- catch ( Exception )
- {
- this._MaxFileSize = TwitterPhoto.MaxfilesizeDefault;
- return false; // error
- }
- return true; // 正常に設定終了
- }
- return true; // 設定項目がない場合はとりあえずエラー扱いにしない
- }
-
- public string GetFileOpenDialogFilter()
- {
- return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
- }
-
- public UploadFileType GetFileType( string ext )
- {
- if ( this.CheckValidExtension( ext ) )
- return UploadFileType.Picture;
-
- return UploadFileType.Invalid;
- }
-
- public bool IsSupportedFileType( UploadFileType type )
- {
- return type.Equals( UploadFileType.Picture );
- }
-
- public string Upload( ref string filePath, ref string message, long? reply_to )
- {
- if ( string.IsNullOrEmpty( filePath ) )
- return "Err:File isn't specified.";
-
- if ( string.IsNullOrEmpty( message ) )
- message = "";
-
- FileInfo mediaFile;
- try
- {
- mediaFile = new FileInfo( filePath );
- }
- catch ( NotSupportedException ex )
- {
- return "Err:" + ex.Message;
- }
-
- if ( !mediaFile.Exists )
- return "Err:File isn't exists.";
-
- if ( MyCommon.IsAnimatedGif( filePath ) )
- return "Err:Don't support animatedGIF.";
-
- return tw.PostStatusWithMedia( message, reply_to, mediaFile );
- }
-
- public string Upload(ref string[] filePaths, ref string message, long? reply_to)
- {
- if (filePaths == null || filePaths.Length == 0 || string.IsNullOrEmpty(filePaths[0]))
- return "Err:File isn't specified.";
+ public class TwitterPhoto : IMediaUploadService
+ {
+ private readonly string[] pictureExt = new[] { ".jpg", ".jpeg", ".gif", ".png" };
- if (string.IsNullOrEmpty(message))
- message = "";
+ private readonly Twitter tw;
+ private TwitterConfiguration twitterConfig;
- var mediaFiles = new List<FileInfo>();
+ public TwitterPhoto(Twitter twitter, TwitterConfiguration twitterConfig)
+ {
+ this.tw = twitter;
+ this.twitterConfig = twitterConfig;
+ }
- foreach (var filePath in filePaths)
+ public int MaxMediaCount
+ {
+ get { return 4; }
+ }
+
+ public string SupportedFormatsStrForDialog
+ {
+ get
{
- if (string.IsNullOrEmpty(filePath)) continue;
+ return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
+ }
+ }
+
+ public bool CheckFileExtension(string fileExtension)
+ {
+ return this.pictureExt.Contains(fileExtension.ToLowerInvariant());
+ }
+
+ public bool CheckFileSize(string fileExtension, long fileSize)
+ {
+ var maxFileSize = this.GetMaxFileSize(fileExtension);
+ return maxFileSize == null || fileSize <= maxFileSize.Value;
+ }
- FileInfo mediaFile;
- try
- {
- mediaFile = new FileInfo(filePath);
- }
- catch (NotSupportedException ex)
- {
- return "Err:" + ex.Message;
- }
+ public long? GetMaxFileSize(string fileExtension)
+ {
+ return this.twitterConfig.PhotoSizeLimit;
+ }
- if (!mediaFile.Exists)
- return "Err:File isn't exists.";
+ public async Task PostStatusAsync(string text, long? inReplyToStatusId, IMediaItem[] mediaItems)
+ {
+ if (mediaItems == null)
+ throw new ArgumentNullException(nameof(mediaItems));
+
+ if (mediaItems.Length == 0)
+ throw new ArgumentException("Err:Media not specified.");
- if (MyCommon.IsAnimatedGif(filePath))
- return "Err:Don't support animatedGIF.";
+ foreach (var item in mediaItems)
+ {
+ if (item == null)
+ throw new ArgumentException("Err:Media not specified.");
- mediaFiles.Add(mediaFile);
+ if (!item.Exists)
+ throw new ArgumentException("Err:Media not found.");
}
- return tw.PostStatusWithMultipleMedia(message, reply_to, mediaFiles);
+ await this.tw.PostStatusWithMultipleMedia(text, inReplyToStatusId, mediaItems)
+ .ConfigureAwait(false);
+ }
+
+ public int GetReservedTextLength(int mediaCount)
+ {
+ // 枚数に関わらず文字数は一定
+ return this.twitterConfig.ShortUrlLength;
+ }
+
+ public void UpdateTwitterConfiguration(TwitterConfiguration config)
+ {
+ this.twitterConfig = config;
}
-
- public TwitterPhoto(Twitter twitter)
- {
- this.tw = twitter;
- }
- }
+ }
}