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 // All rights reserved.
10 // This file is part of OpenTween.
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
27 using IMultimediaShareService = OpenTween.IMultimediaShareService;
28 using Array = System.Array;
29 using Convert = System.Convert;
30 using Exception = System.Exception;
31 using UploadFileType = OpenTween.MyCommon.UploadFileType;
32 using MyCommon = OpenTween.MyCommon;
33 using FileInfo = System.IO.FileInfo;
34 using NotSupportedException = System.NotSupportedException;
35 using System.Collections.Generic;
39 public class TwitterPhoto : IMultimediaShareService
41 private string[] pictureExt = new string[] { ".jpg", ".jpeg", ".gif", ".png" };
43 private const long MaxfilesizeDefault = 3145728;
45 // help/configurationにより取得されコンストラクタへ渡される
46 private long _MaxFileSize = 3145728;
50 public bool CheckValidExtension( string ext )
52 if ( Array.IndexOf( this.pictureExt, ext.ToLower() ) > -1 )
58 public bool CheckValidFilesize( string ext, long fileSize )
60 if ( this.CheckValidExtension( ext ) )
61 return fileSize <= this._MaxFileSize;
66 public bool Configuration( string key, object value )
68 if ( key == "MaxUploadFilesize" )
73 val = Convert.ToInt64( value );
75 this._MaxFileSize = val;
77 this._MaxFileSize = TwitterPhoto.MaxfilesizeDefault;
81 this._MaxFileSize = TwitterPhoto.MaxfilesizeDefault;
82 return false; // error
84 return true; // 正常に設定終了
86 return true; // 設定項目がない場合はとりあえずエラー扱いにしない
89 public string GetFileOpenDialogFilter()
91 return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
94 public UploadFileType GetFileType( string ext )
96 if ( this.CheckValidExtension( ext ) )
97 return UploadFileType.Picture;
99 return UploadFileType.Invalid;
102 public bool IsSupportedFileType( UploadFileType type )
104 return type.Equals( UploadFileType.Picture );
107 public string Upload( ref string filePath, ref string message, long? reply_to )
109 if ( string.IsNullOrEmpty( filePath ) )
110 return "Err:File isn't specified.";
112 if ( string.IsNullOrEmpty( message ) )
118 mediaFile = new FileInfo( filePath );
120 catch ( NotSupportedException ex )
122 return "Err:" + ex.Message;
125 if ( !mediaFile.Exists )
126 return "Err:File isn't exists.";
128 if ( MyCommon.IsAnimatedGif( filePath ) )
129 return "Err:Don't support animatedGIF.";
131 return tw.PostStatusWithMedia( message, reply_to, mediaFile );
134 public string Upload(ref string[] filePaths, ref string message, long? reply_to)
136 if (filePaths == null || filePaths.Length == 0 || string.IsNullOrEmpty(filePaths[0]))
137 return "Err:File isn't specified.";
139 if (string.IsNullOrEmpty(message))
142 var mediaFiles = new List<FileInfo>();
144 foreach (var filePath in filePaths)
146 if (string.IsNullOrEmpty(filePath)) continue;
151 mediaFile = new FileInfo(filePath);
153 catch (NotSupportedException ex)
155 return "Err:" + ex.Message;
158 if (!mediaFile.Exists)
159 return "Err:File isn't exists.";
161 if (MyCommon.IsAnimatedGif(filePath))
162 return "Err:Don't support animatedGIF.";
164 mediaFiles.Add(mediaFile);
167 return tw.PostStatusWithMultipleMedia(message, reply_to, mediaFiles);
170 public TwitterPhoto(Twitter twitter)