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;
190 private readonly MemoryImage _image;
192 public bool IsDisposed { get; private set; } = false;
194 public MemoryImageMediaItem(MemoryImage image)
196 this._image = image ?? throw new ArgumentNullException(nameof(image));
198 var num = Interlocked.Increment(ref _fileNumber);
199 this.Path = PathPrefix + num + this._image.ImageFormatExt;
202 public string Path { get; }
203 public string AltText { get; set; }
207 get { return this.Path.Substring(PathPrefix.Length); }
210 public string Extension
212 get { return this._image.ImageFormatExt; }
217 get { return this._image != null; }
222 get { return this._image.Stream.Length; }
230 public MemoryImage CreateImage()
232 return this._image.Clone();
235 public Stream OpenRead()
237 MemoryStream memstream = null;
241 memstream = new MemoryStream();
243 this._image.Stream.WriteTo(memstream);
244 memstream.Seek(0, SeekOrigin.Begin);
250 memstream?.Dispose();
255 public void CopyTo(Stream stream)
257 this._image.Stream.WriteTo(stream);
260 protected virtual void Dispose(bool disposing)
262 if (this.IsDisposed) return;
266 this._image.Dispose();
269 this.IsDisposed = true;
272 public void Dispose()
276 // 明示的にDisposeが呼ばれた場合はファイナライザを使用しない
277 GC.SuppressFinalize(this);
280 ~MemoryImageMediaItem()
281 => this.Dispose(false);