OSDN Git Service

using var を使用する
[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.Drawing;
31 using System.Drawing.Imaging;
32 using System.IO;
33 using System.Linq;
34 using System.Threading.Tasks;
35 using OpenTween.Api.DataModel;
36 using OpenTween.Setting;
37
38 namespace OpenTween.Connection
39 {
40     public class TwitterPhoto : IMediaUploadService
41     {
42         private readonly string[] pictureExt = { ".jpg", ".jpeg", ".gif", ".png" };
43
44         private readonly Twitter tw;
45         private TwitterConfiguration twitterConfig;
46
47         public TwitterPhoto(Twitter twitter, TwitterConfiguration twitterConfig)
48         {
49             this.tw = twitter;
50             this.twitterConfig = twitterConfig;
51         }
52
53         public int MaxMediaCount => 4;
54
55         public string SupportedFormatsStrForDialog => "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
56
57         public bool CanUseAltText => true;
58
59         public bool CheckFileExtension(string fileExtension)
60             => this.pictureExt.Contains(fileExtension, StringComparer.InvariantCultureIgnoreCase);
61
62         public bool CheckFileSize(string fileExtension, long fileSize)
63         {
64             var maxFileSize = this.GetMaxFileSize(fileExtension);
65             return maxFileSize == null || fileSize <= maxFileSize.Value;
66         }
67
68         public long? GetMaxFileSize(string fileExtension)
69             => this.twitterConfig.PhotoSizeLimit;
70
71         public async Task<PostStatusParams> UploadAsync(IMediaItem[] mediaItems, PostStatusParams postParams)
72         {
73             if (mediaItems == null)
74                 throw new ArgumentNullException(nameof(mediaItems));
75
76             if (mediaItems.Length == 0)
77                 throw new ArgumentException("Err:Media not specified.");
78
79             foreach (var item in mediaItems)
80             {
81                 if (item == null)
82                     throw new ArgumentException("Err:Media not specified.");
83
84                 if (!item.Exists)
85                     throw new ArgumentException("Err:Media not found.");
86             }
87
88             long[] mediaIds;
89
90             if (Twitter.DMSendTextRegex.IsMatch(postParams.Text))
91                 mediaIds = new[] { await this.UploadMediaForDM(mediaItems).ConfigureAwait(false) };
92             else
93                 mediaIds = await this.UploadMediaForTweet(mediaItems).ConfigureAwait(false);
94
95             postParams.MediaIds = mediaIds;
96
97             return postParams;
98         }
99
100         // pic.twitter.com の URL は文字数にカウントされない
101         public int GetReservedTextLength(int mediaCount)
102             => 0;
103
104         public void UpdateTwitterConfiguration(TwitterConfiguration config)
105             => this.twitterConfig = config;
106
107         private async Task<long[]> UploadMediaForTweet(IMediaItem[] mediaItems)
108         {
109             var uploadTasks = from m in mediaItems
110                               select this.UploadMediaItem(m, mediaCategory: null);
111
112             var mediaIds = await Task.WhenAll(uploadTasks)
113                 .ConfigureAwait(false);
114
115             return mediaIds;
116         }
117
118         private async Task<long> UploadMediaForDM(IMediaItem[] mediaItems)
119         {
120             if (mediaItems.Length > 1)
121                 throw new InvalidOperationException("Err:Can't attach multiple media to DM.");
122
123             var mediaItem = mediaItems[0];
124
125             string mediaCategory;
126             switch (mediaItem.Extension)
127             {
128                 case ".gif":
129                     mediaCategory = "dm_gif";
130                     break;
131                 default:
132                     mediaCategory = "dm_image";
133                     break;
134             }
135
136             var mediaId = await this.UploadMediaItem(mediaItems[0], mediaCategory)
137                 .ConfigureAwait(false);
138
139             return mediaId;
140         }
141
142         private async Task<long> UploadMediaItem(IMediaItem mediaItem, string mediaCategory)
143         {
144             async Task<long> UploadInternal(IMediaItem media, string category)
145             {
146                 var mediaId = await this.tw.UploadMedia(media, category)
147                     .ConfigureAwait(false);
148
149                 if (!string.IsNullOrEmpty(media.AltText))
150                 {
151                     await this.tw.Api.MediaMetadataCreate(mediaId, media.AltText)
152                         .ConfigureAwait(false);
153                 }
154
155                 return mediaId;
156             }
157
158             using var origImage = mediaItem.CreateImage();
159
160             if (SettingManager.Common.AlphaPNGWorkaround && this.AddAlphaChannelIfNeeded(origImage.Image, out var newImage))
161             {
162                 using var newMediaItem = new MemoryImageMediaItem(newImage);
163                 newMediaItem.AltText = mediaItem.AltText;
164
165                 return await UploadInternal(newMediaItem, mediaCategory);
166             }
167             else
168             {
169                 return await UploadInternal(mediaItem, mediaCategory);
170             }
171         }
172
173         /// <summary>
174         /// pic.twitter.com アップロード時に JPEG への変換を回避するための加工を行う
175         /// </summary>
176         /// <remarks>
177         /// pic.twitter.com へのアップロード時に、アルファチャンネルを持たない PNG 画像が
178         /// JPEG 形式に変換され画質が低下する問題を回避します。
179         /// PNG 以外の画像や、すでにアルファチャンネルを持つ PNG 画像に対しては何もしません。
180         /// </remarks>
181         /// <returns>加工が行われた場合は true、そうでない場合は false</returns>
182         private bool AddAlphaChannelIfNeeded(Image origImage, out MemoryImage newImage)
183         {
184             newImage = null;
185
186             // PNG 画像以外に対しては何もしない
187             if (origImage.RawFormat.Guid != ImageFormat.Png.Guid)
188                 return false;
189
190             using var bitmap = new Bitmap(origImage);
191
192             // アルファ値が 255 以外のピクセルが含まれていた場合は何もしない
193             foreach (var x in Enumerable.Range(0, bitmap.Width))
194             {
195                 foreach (var y in Enumerable.Range(0, bitmap.Height))
196                 {
197                     if (bitmap.GetPixel(x, y).A != 255)
198                         return false;
199                 }
200             }
201
202             // 左上の 1px だけアルファ値を 254 にする
203             var pixel = bitmap.GetPixel(0, 0);
204             var newPixel = Color.FromArgb(pixel.A - 1, pixel.R, pixel.G, pixel.B);
205             bitmap.SetPixel(0, 0, newPixel);
206
207             // MemoryImage 作成時に画像はコピーされるため、この後 bitmap は破棄しても問題ない
208             newImage = MemoryImage.CopyFromImage(bitmap);
209
210             return true;
211         }
212     }
213 }