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.
11 // This file is part of OpenTween.
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)
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
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.
31 using System.Collections.Generic;
33 using System.Drawing.Imaging;
36 using System.Threading.Tasks;
37 using OpenTween.Api.DataModel;
38 using OpenTween.Setting;
40 namespace OpenTween.Connection
42 public class TwitterPhoto : IMediaUploadService
44 private readonly string[] pictureExt = { ".jpg", ".jpeg", ".gif", ".png" };
46 private readonly Twitter tw;
47 private TwitterConfiguration twitterConfig;
49 public TwitterPhoto(Twitter twitter, TwitterConfiguration twitterConfig)
52 this.twitterConfig = twitterConfig;
55 public int MaxMediaCount => 4;
57 public string SupportedFormatsStrForDialog => "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
59 public bool CanUseAltText => true;
61 public bool CheckFileExtension(string fileExtension)
62 => this.pictureExt.Contains(fileExtension, StringComparer.InvariantCultureIgnoreCase);
64 public bool CheckFileSize(string fileExtension, long fileSize)
66 var maxFileSize = this.GetMaxFileSize(fileExtension);
67 return maxFileSize == null || fileSize <= maxFileSize.Value;
70 public long? GetMaxFileSize(string fileExtension)
71 => this.twitterConfig.PhotoSizeLimit;
73 public async Task<PostStatusParams> UploadAsync(IMediaItem[] mediaItems, PostStatusParams postParams)
75 if (mediaItems == null)
76 throw new ArgumentNullException(nameof(mediaItems));
78 if (mediaItems.Length == 0)
79 throw new ArgumentException("Err:Media not specified.");
81 foreach (var item in mediaItems)
84 throw new ArgumentException("Err:Media not specified.");
87 throw new ArgumentException("Err:Media not found.");
92 if (Twitter.DMSendTextRegex.IsMatch(postParams.Text))
93 mediaIds = new[] { await this.UploadMediaForDM(mediaItems).ConfigureAwait(false) };
95 mediaIds = await this.UploadMediaForTweet(mediaItems).ConfigureAwait(false);
97 postParams.MediaIds = mediaIds;
102 // pic.twitter.com の URL は文字数にカウントされない
103 public int GetReservedTextLength(int mediaCount)
106 public void UpdateTwitterConfiguration(TwitterConfiguration config)
107 => this.twitterConfig = config;
109 private async Task<long[]> UploadMediaForTweet(IMediaItem[] mediaItems)
111 var uploadTasks = from m in mediaItems
112 select this.UploadMediaItem(m, mediaCategory: null);
114 var mediaIds = await Task.WhenAll(uploadTasks)
115 .ConfigureAwait(false);
120 private async Task<long> UploadMediaForDM(IMediaItem[] mediaItems)
122 if (mediaItems.Length > 1)
123 throw new InvalidOperationException("Err:Can't attach multiple media to DM.");
125 var mediaItem = mediaItems[0];
126 var mediaCategory = mediaItem.Extension switch
132 var mediaId = await this.UploadMediaItem(mediaItems[0], mediaCategory)
133 .ConfigureAwait(false);
138 private async Task<long> UploadMediaItem(IMediaItem mediaItem, string? mediaCategory)
140 async Task<long> UploadInternal(IMediaItem media, string? category)
142 var mediaId = await this.tw.UploadMedia(media, category)
143 .ConfigureAwait(false);
145 if (!string.IsNullOrEmpty(media.AltText))
147 await this.tw.Api.MediaMetadataCreate(mediaId, media.AltText)
148 .ConfigureAwait(false);
154 using var origImage = mediaItem.CreateImage();
156 if (SettingManager.Common.AlphaPNGWorkaround && this.AddAlphaChannelIfNeeded(origImage.Image, out var newImage))
158 using var newMediaItem = new MemoryImageMediaItem(newImage!);
159 newMediaItem.AltText = mediaItem.AltText;
161 return await UploadInternal(newMediaItem, mediaCategory);
165 return await UploadInternal(mediaItem, mediaCategory);
170 /// pic.twitter.com アップロード時に JPEG への変換を回避するための加工を行う
173 /// pic.twitter.com へのアップロード時に、アルファチャンネルを持たない PNG 画像が
174 /// JPEG 形式に変換され画質が低下する問題を回避します。
175 /// PNG 以外の画像や、すでにアルファチャンネルを持つ PNG 画像に対しては何もしません。
177 /// <returns>加工が行われた場合は true、そうでない場合は false</returns>
178 private bool AddAlphaChannelIfNeeded(Image origImage, out MemoryImage? newImage)
182 // PNG 画像以外に対しては何もしない
183 if (origImage.RawFormat.Guid != ImageFormat.Png.Guid)
186 using var bitmap = new Bitmap(origImage);
188 // アルファ値が 255 以外のピクセルが含まれていた場合は何もしない
189 foreach (var x in Enumerable.Range(0, bitmap.Width))
191 foreach (var y in Enumerable.Range(0, bitmap.Height))
193 if (bitmap.GetPixel(x, y).A != 255)
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);
203 // MemoryImage 作成時に画像はコピーされるため、この後 bitmap は破棄しても問題ない
204 newImage = MemoryImage.CopyFromImage(bitmap);