OSDN Git Service

Twitterへの複数画像の投稿に対応した
[opentween/open-tween.git] / OpenTween / Connection / TwitterPhoto.cs
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.
9 // 
10 // This file is part of OpenTween.
11 // 
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)
15 // any later version.
16 // 
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
20 // for more details. 
21 // 
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.
26
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;
36
37 namespace OpenTween
38 {
39         public class TwitterPhoto : IMultimediaShareService
40         {
41                 private string[] pictureExt = new string[] { ".jpg", ".jpeg", ".gif", ".png" };
42
43                 private const long MaxfilesizeDefault = 3145728;
44
45                 // help/configurationにより取得されコンストラクタへ渡される
46                 private long _MaxFileSize = 3145728;
47
48                 private Twitter tw;
49
50                 public bool CheckValidExtension( string ext )
51                 {
52                         if ( Array.IndexOf( this.pictureExt, ext.ToLower() ) > -1 )
53                                 return true;
54
55                         return false;
56                 }
57
58                 public bool CheckValidFilesize( string ext, long fileSize )
59                 {
60                         if ( this.CheckValidExtension( ext ) )
61                                 return fileSize <= this._MaxFileSize;
62
63                         return false;
64                 }
65
66                 public bool Configuration( string key, object value )
67                 {
68                         if ( key == "MaxUploadFilesize" )
69                         {
70                                 long val;
71                                 try
72                                 {
73                                         val = Convert.ToInt64( value );
74                                         if ( val > 0 )
75                                                 this._MaxFileSize = val;
76                                         else
77                                         this._MaxFileSize = TwitterPhoto.MaxfilesizeDefault;
78                                 }
79                                 catch ( Exception )
80                                 {
81                                         this._MaxFileSize = TwitterPhoto.MaxfilesizeDefault;
82                                         return false; // error
83                                 }
84                                 return true; // 正常に設定終了
85                         }
86                         return true; // 設定項目がない場合はとりあえずエラー扱いにしない
87                 }
88
89                 public string GetFileOpenDialogFilter()
90                 {
91                         return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
92                 }
93
94                 public UploadFileType GetFileType( string ext )
95                 {
96                         if ( this.CheckValidExtension( ext ) )
97                                 return UploadFileType.Picture;
98
99                         return UploadFileType.Invalid;
100                 }
101
102                 public bool IsSupportedFileType( UploadFileType type )
103                 {
104                         return type.Equals( UploadFileType.Picture );
105                 }
106
107                 public string Upload( ref string filePath, ref string message, long? reply_to )
108                 {
109                         if ( string.IsNullOrEmpty( filePath ) )
110                                 return "Err:File isn't specified.";
111
112                         if ( string.IsNullOrEmpty( message ) )
113                                 message =  "";
114
115                         FileInfo mediaFile;
116                         try
117                         {
118                                 mediaFile = new FileInfo( filePath );
119                         }
120                         catch ( NotSupportedException ex )
121                         {
122                                 return "Err:" + ex.Message;
123                         }
124
125                         if ( !mediaFile.Exists )
126                                 return "Err:File isn't exists.";
127
128                         if ( MyCommon.IsAnimatedGif( filePath ) )
129                                 return "Err:Don't support animatedGIF.";
130
131                         return tw.PostStatusWithMedia( message, reply_to, mediaFile );
132                 }
133
134         public string Upload(ref string[] filePaths, ref string message, long? reply_to)
135         {
136             if (filePaths == null || filePaths.Length == 0 || string.IsNullOrEmpty(filePaths[0]))
137                 return "Err:File isn't specified.";
138
139             if (string.IsNullOrEmpty(message))
140                 message = "";
141
142             var mediaFiles = new List<FileInfo>();
143
144             foreach (var filePath in filePaths)
145             {
146                 if (string.IsNullOrEmpty(filePath)) continue;
147
148                 FileInfo mediaFile;
149                 try
150                 {
151                     mediaFile = new FileInfo(filePath);
152                 }
153                 catch (NotSupportedException ex)
154                 {
155                     return "Err:" + ex.Message;
156                 }
157
158                 if (!mediaFile.Exists)
159                     return "Err:File isn't exists.";
160
161                 if (MyCommon.IsAnimatedGif(filePath))
162                     return "Err:Don't support animatedGIF.";
163
164                 mediaFiles.Add(mediaFile);
165             }
166
167             return tw.PostStatusWithMultipleMedia(message, reply_to, mediaFiles);
168         }
169         
170         public TwitterPhoto(Twitter twitter)
171                 {
172                         this.tw = twitter;
173                 }
174         }
175 }