OSDN Git Service

d985549589e8f6e1b246f0ea4205522bb0a0279c
[opentween/open-tween.git] / OpenTween / Thumbnail / ThumbnailInfo.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2012 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Linq;
25 using System.Net.Http;
26 using System.Text;
27 using System.Threading;
28 using System.Threading.Tasks;
29 using OpenTween.Connection;
30
31 namespace OpenTween.Thumbnail
32 {
33     public class ThumbnailInfo : IEquatable<ThumbnailInfo>
34     {
35         /// <summary>サムネイルとして表示するメディアが掲載されている URL</summary>
36         /// <remarks>
37         /// 例えば Youtube のサムネイルの場合、動画そのものの URL ではなく
38         /// https://www.youtube.com/watch?v=****** 形式の URL が含まれる
39         /// </remarks>
40         public string MediaPageUrl { get; set; }
41
42         /// <summary>サムネイルとして表示する画像の URL</summary>
43         /// <remarks>
44         /// ここに含まれる URL は直接画像として表示可能である必要がある
45         /// </remarks>
46         public string ThumbnailImageUrl { get; set; }
47
48         /// <summary>最も高解像度な画像の URL</summary>
49         /// <remarks>
50         /// サムネイルとしては不適だが、より高解像度な画像を表示する場面に使用できる
51         /// URL があればここに含まれる
52         /// </remarks>
53         public string FullSizeImageUrl { get; set; }
54
55         /// <summary>ツールチップとして表示するテキスト</summary>
56         /// <remarks>
57         /// サムネイル画像にマウスオーバーした際に表示されるテキスト
58         /// </remarks>
59         public string TooltipText { get; set; }
60
61         /// <summary>
62         /// 対象となるメディアが動画や音声など再生可能なものであるか否か
63         /// </summary>
64         public bool IsPlayable { get; set; }
65
66         public Task<MemoryImage> LoadThumbnailImageAsync()
67         {
68             return this.LoadThumbnailImageAsync(CancellationToken.None);
69         }
70
71         public Task<MemoryImage> LoadThumbnailImageAsync(CancellationToken cancellationToken)
72         {
73             return this.LoadThumbnailImageAsync(Networking.Http, cancellationToken);
74         }
75
76         public async virtual Task<MemoryImage> LoadThumbnailImageAsync(HttpClient http, CancellationToken cancellationToken)
77         {
78             MemoryImage image = null;
79             try
80             {
81                 using (var response = await http.GetAsync(this.ThumbnailImageUrl, cancellationToken).ConfigureAwait(false))
82                 {
83                     response.EnsureSuccessStatusCode();
84
85                     using (var imageStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false))
86                     {
87                         image = await MemoryImage.CopyFromStreamAsync(imageStream)
88                             .ConfigureAwait(false);
89
90                         cancellationToken.ThrowIfCancellationRequested();
91
92                         return image;
93                     }
94                 }
95             }
96             catch (OperationCanceledException)
97             {
98                 image?.Dispose();
99                 throw;
100             }
101         }
102
103         public override bool Equals(object obj)
104         {
105             return this.Equals(obj as ThumbnailInfo);
106         }
107
108         public bool Equals(ThumbnailInfo other)
109         {
110             return other != null &&
111                 other.MediaPageUrl == this.MediaPageUrl &&
112                 other.ThumbnailImageUrl == this.ThumbnailImageUrl &&
113                 other.TooltipText == this.TooltipText &&
114                 other.FullSizeImageUrl == this.FullSizeImageUrl &&
115                 other.IsPlayable == this.IsPlayable;
116         }
117
118         public override int GetHashCode()
119         {
120             return this.MediaPageUrl.GetHashCode() ^ this.ThumbnailImageUrl.GetHashCode();
121         }
122     }
123 }