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.
29 using System.Collections.Generic;
31 using System.Drawing.Imaging;
34 using System.Threading.Tasks;
35 using OpenTween.Api.DataModel;
36 using OpenTween.Setting;
38 namespace OpenTween.Connection
40 public class TwitterPhoto : IMediaUploadService
42 private readonly string[] pictureExt = { ".jpg", ".jpeg", ".gif", ".png" };
44 private readonly Twitter tw;
45 private TwitterConfiguration twitterConfig;
47 public TwitterPhoto(Twitter twitter, TwitterConfiguration twitterConfig)
50 this.twitterConfig = twitterConfig;
53 public int MaxMediaCount => 4;
55 public string SupportedFormatsStrForDialog => "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
57 public bool CanUseAltText => true;
59 public bool CheckFileExtension(string fileExtension)
60 => this.pictureExt.Contains(fileExtension, StringComparer.InvariantCultureIgnoreCase);
62 public bool CheckFileSize(string fileExtension, long fileSize)
64 var maxFileSize = this.GetMaxFileSize(fileExtension);
65 return maxFileSize == null || fileSize <= maxFileSize.Value;
68 public long? GetMaxFileSize(string fileExtension)
69 => this.twitterConfig.PhotoSizeLimit;
71 public async Task<PostStatusParams> UploadAsync(IMediaItem[] mediaItems, PostStatusParams postParams)
73 if (mediaItems == null)
74 throw new ArgumentNullException(nameof(mediaItems));
76 if (mediaItems.Length == 0)
77 throw new ArgumentException("Err:Media not specified.");
79 foreach (var item in mediaItems)
82 throw new ArgumentException("Err:Media not specified.");
85 throw new ArgumentException("Err:Media not found.");
88 var uploadTasks = from m in mediaItems
89 select this.UploadMediaItem(m);
91 var mediaIds = await Task.WhenAll(uploadTasks)
92 .ConfigureAwait(false);
94 postParams.MediaIds = mediaIds;
99 // pic.twitter.com の URL は文字数にカウントされない
100 public int GetReservedTextLength(int mediaCount)
103 public void UpdateTwitterConfiguration(TwitterConfiguration config)
104 => this.twitterConfig = config;
106 private async Task<long> UploadMediaItem(IMediaItem mediaItem)
108 async Task<long> UploadInternal(IMediaItem media)
110 var mediaId = await this.tw.UploadMedia(media)
111 .ConfigureAwait(false);
113 if (!string.IsNullOrEmpty(media.AltText))
115 await this.tw.Api.MediaMetadataCreate(mediaId, media.AltText)
116 .ConfigureAwait(false);
122 using (var origImage = mediaItem.CreateImage())
124 if (SettingManager.Common.AlphaPNGWorkaround && this.AddAlphaChannelIfNeeded(origImage.Image, out var newImage))
126 using (var newMediaItem = new MemoryImageMediaItem(newImage))
128 newMediaItem.AltText = mediaItem.AltText;
129 return await UploadInternal(newMediaItem);
134 return await UploadInternal(mediaItem);
140 /// pic.twitter.com アップロード時に JPEG への変換を回避するための加工を行う
143 /// pic.twitter.com へのアップロード時に、アルファチャンネルを持たない PNG 画像が
144 /// JPEG 形式に変換され画質が低下する問題を回避します。
145 /// PNG 以外の画像や、すでにアルファチャンネルを持つ PNG 画像に対しては何もしません。
147 /// <returns>加工が行われた場合は true、そうでない場合は false</returns>
148 private bool AddAlphaChannelIfNeeded(Image origImage, out MemoryImage newImage)
152 // PNG 画像以外に対しては何もしない
153 if (origImage.RawFormat.Guid != ImageFormat.Png.Guid)
156 using (var bitmap = new Bitmap(origImage))
158 // アルファ値が 255 以外のピクセルが含まれていた場合は何もしない
159 foreach (var x in Enumerable.Range(0, bitmap.Width))
161 foreach (var y in Enumerable.Range(0, bitmap.Height))
163 if (bitmap.GetPixel(x, y).A != 255)
168 // 左上の 1px だけアルファ値を 254 にする
169 var pixel = bitmap.GetPixel(0, 0);
170 var newPixel = Color.FromArgb(pixel.A - 1, pixel.R, pixel.G, pixel.B);
171 bitmap.SetPixel(0, 0, newPixel);
173 // MemoryImage 作成時に画像はコピーされるため、この後 bitmap は破棄しても問題ない
174 newImage = MemoryImage.CopyFromImage(bitmap);