1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 spx (@5px)
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
26 using System.Threading;
30 public interface IMediaItem
45 string Extension { get; }
48 /// メディアが存在するかどうかを示す真偽値
58 /// メディアが画像であるかどうかを示す真偽値
63 /// 代替テキスト (アップロード先が対応している必要がある)
65 string AltText { get; set; }
68 /// 表示用の MemoryImage を作成する
73 MemoryImage CreateImage();
76 /// メディアの内容を読み込むための Stream を開く
84 /// メディアの内容を Stream へ書き込む
86 void CopyTo(Stream stream);
90 /// ファイル用の MediaItem クラス
92 public class FileMediaItem : IMediaItem
94 public FileInfo FileInfo { get; }
95 public string AltText { get; set; }
97 public FileMediaItem(string path)
99 this.FileInfo = new FileInfo(path);
102 public FileMediaItem(FileInfo fileInfo)
103 : this(fileInfo.FullName)
109 get { return this.FileInfo.FullName; }
114 get { return this.FileInfo.Name; }
117 public string Extension
119 get { return this.FileInfo.Extension; }
124 get { return this.FileInfo.Exists; }
129 get { return this.FileInfo.Length; }
136 if (this.isImage == null)
140 // MemoryImage が生成できるかを検証する
141 using (var image = this.CreateImage()) { }
145 catch (InvalidImageException)
147 this.isImage = false;
151 return this.isImage.Value;
155 /// <summary>IsImage の検証結果をキャッシュする。未検証なら null</summary>
156 private bool? isImage = null;
158 public MemoryImage CreateImage()
160 using (var fs = this.FileInfo.OpenRead())
162 return MemoryImage.CopyFromStream(fs);
166 public Stream OpenRead()
168 return this.FileInfo.OpenRead();
171 public void CopyTo(Stream stream)
173 using (var fs = this.FileInfo.OpenRead())
181 /// MemoryImage 用の MediaItem クラス
184 /// 用途の関係上、メモリ使用量が大きくなるため、不要になればできるだけ破棄すること
186 public class MemoryImageMediaItem : IMediaItem, IDisposable
188 public const string PathPrefix = "<>MemoryImage://";
189 private static int _fileNumber = 0;
191 private bool _disposed = false;
193 private readonly MemoryImage _image;
195 public MemoryImageMediaItem(MemoryImage image)
197 this._image = image ?? throw new ArgumentNullException(nameof(image));
199 var num = Interlocked.Increment(ref _fileNumber);
200 this.Path = PathPrefix + num + this._image.ImageFormatExt;
203 public string Path { get; }
204 public string AltText { get; set; }
208 get { return this.Path.Substring(PathPrefix.Length); }
211 public string Extension
213 get { return this._image.ImageFormatExt; }
218 get { return this._image != null; }
223 get { return this._image.Stream.Length; }
231 public bool IsDisposed
233 get { return this._disposed; }
236 public MemoryImage CreateImage()
238 return this._image.Clone();
241 public Stream OpenRead()
243 MemoryStream memstream = null;
247 memstream = new MemoryStream();
249 this._image.Stream.WriteTo(memstream);
250 memstream.Seek(0, SeekOrigin.Begin);
256 memstream?.Dispose();
261 public void CopyTo(Stream stream)
263 this._image.Stream.WriteTo(stream);
266 protected virtual void Dispose(bool disposing)
268 if (this._disposed) return;
272 this._image.Dispose();
275 this._disposed = true;
278 public void Dispose()
282 // 明示的にDisposeが呼ばれた場合はファイナライザを使用しない
283 GC.SuppressFinalize(this);
286 ~MemoryImageMediaItem()
287 => this.Dispose(false);