OSDN Git Service

1790bd7ce0a7c630ab97db14775a25029656f380
[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 //           (c) 2014      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
10 //
11 // This file is part of OpenTween.
12 //
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)
16 // any later version.
17 //
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
21 // for more details.
22 //
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.
27
28 using System;
29 using System.Collections.Generic;
30 using System.IO;
31 using System.Linq;
32 using System.Threading.Tasks;
33 using OpenTween.Api;
34
35 namespace OpenTween.Connection
36 {
37     public class TwitterPhoto : IMediaUploadService
38     {
39         private readonly string[] pictureExt = new[] { ".jpg", ".jpeg", ".gif", ".png" };
40
41         private readonly Twitter tw;
42         private TwitterConfiguration twitterConfig;
43
44         public TwitterPhoto(Twitter twitter, TwitterConfiguration twitterConfig)
45         {
46             this.tw = twitter;
47             this.twitterConfig = twitterConfig;
48         }
49
50         public int MaxMediaCount
51         {
52             get { return 4; }
53         }
54
55         public string SupportedFormatsStrForDialog
56         {
57             get
58             {
59                 return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
60             }
61         }
62
63         public bool CheckFileExtension(string fileExtension)
64         {
65             return this.pictureExt.Contains(fileExtension.ToLower());
66         }
67
68         public bool CheckFileSize(string fileExtension, long fileSize)
69         {
70             var maxFileSize = this.GetMaxFileSize(fileExtension);
71             return maxFileSize == null || fileSize <= maxFileSize.Value;
72         }
73
74         public long? GetMaxFileSize(string fileExtension)
75         {
76             return this.twitterConfig.PhotoSizeLimit;
77         }
78
79         public async Task PostStatusAsync(string text, long? inReplyToStatusId, string[] filePaths)
80         {
81             if (filePaths == null || filePaths.Length == 0 || string.IsNullOrEmpty(filePaths[0]))
82                 throw new ArgumentException("Err:File isn't specified.", "filePaths");
83
84             var mediaFiles = new List<FileInfo>();
85
86             foreach (var filePath in filePaths)
87             {
88                 if (string.IsNullOrEmpty(filePath)) continue;
89
90                 var mediaFile = new FileInfo(filePath);
91
92                 if (!mediaFile.Exists)
93                     throw new ArgumentException("Err:File isn't exists.", "filePaths");
94
95                 if (MyCommon.IsAnimatedGif(filePath))
96                     throw new ArgumentException("Err:Don't support animatedGIF.", "filePaths");
97
98                 mediaFiles.Add(mediaFile);
99             }
100
101             await Task.Run(() => this.tw.PostStatusWithMultipleMedia(text, inReplyToStatusId, mediaFiles))
102                 .ConfigureAwait(false);
103         }
104
105         public int GetReservedTextLength(int mediaCount)
106         {
107             // 枚数に関わらず文字数は一定
108             return this.twitterConfig.ShortUrlLength;
109         }
110
111         public void UpdateTwitterConfiguration(TwitterConfiguration config)
112         {
113             this.twitterConfig = config;
114         }
115     }
116 }