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;
32 using System.Threading.Tasks;
33 using OpenTween.Api.DataModel;
35 namespace OpenTween.Connection
37 public class TwitterPhoto : IMediaUploadService
39 private readonly string[] pictureExt = { ".jpg", ".jpeg", ".gif", ".png" };
41 private readonly Twitter tw;
42 private TwitterConfiguration twitterConfig;
44 public TwitterPhoto(Twitter twitter, TwitterConfiguration twitterConfig)
47 this.twitterConfig = twitterConfig;
50 public int MaxMediaCount
55 public string SupportedFormatsStrForDialog
59 return "Image Files(*.gif;*.jpg;*.jpeg;*.png)|*.gif;*.jpg;*.jpeg;*.png";
63 public bool CanUseAltText => true;
65 public bool CheckFileExtension(string fileExtension)
67 return this.pictureExt.Contains(fileExtension.ToLowerInvariant());
70 public bool CheckFileSize(string fileExtension, long fileSize)
72 var maxFileSize = this.GetMaxFileSize(fileExtension);
73 return maxFileSize == null || fileSize <= maxFileSize.Value;
76 public long? GetMaxFileSize(string fileExtension)
78 return this.twitterConfig.PhotoSizeLimit;
81 public async Task PostStatusAsync(string text, long? inReplyToStatusId, IMediaItem[] mediaItems,
82 long[] excludeReplyUserIds, string attachmentUrl)
84 if (mediaItems == null)
85 throw new ArgumentNullException(nameof(mediaItems));
87 if (mediaItems.Length == 0)
88 throw new ArgumentException("Err:Media not specified.");
90 foreach (var item in mediaItems)
93 throw new ArgumentException("Err:Media not specified.");
96 throw new ArgumentException("Err:Media not found.");
99 var uploadTasks = from m in mediaItems
100 select this.UploadMediaItem(m);
102 var mediaIds = await Task.WhenAll(uploadTasks)
103 .ConfigureAwait(false);
105 await this.tw.PostStatus(text, inReplyToStatusId, mediaIds, excludeReplyUserIds, attachmentUrl)
106 .ConfigureAwait(false);
109 // pic.twitter.com の URL は文字数にカウントされない
110 public int GetReservedTextLength(int mediaCount)
113 public void UpdateTwitterConfiguration(TwitterConfiguration config)
115 this.twitterConfig = config;
118 private async Task<long> UploadMediaItem(IMediaItem mediaItem)
120 var mediaId = await this.tw.UploadMedia(mediaItem)
121 .ConfigureAwait(false);
123 if (!string.IsNullOrEmpty(mediaItem.AltText))
125 await this.tw.Api.MediaMetadataCreate(mediaId, mediaItem.AltText)
126 .ConfigureAwait(false);