OSDN Git Service

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