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 /// 表示用の MemoryImage を作成する
68 MemoryImage CreateImage();
71 /// メディアの内容を読み込むための Stream を開く
79 /// メディアの内容を Stream へ書き込む
81 void CopyTo(Stream stream);
85 /// ファイル用の MediaItem クラス
87 public class FileMediaItem : IMediaItem
89 public FileInfo FileInfo { get; }
91 public FileMediaItem(string path)
93 this.FileInfo = new FileInfo(path);
96 public FileMediaItem(FileInfo fileInfo)
97 : this(fileInfo.FullName)
103 get { return this.FileInfo.FullName; }
108 get { return this.FileInfo.Name; }
111 public string Extension
113 get { return this.FileInfo.Extension; }
118 get { return this.FileInfo.Exists; }
123 get { return this.FileInfo.Length; }
130 if (this.isImage == null)
134 // MemoryImage が生成できるかを検証する
135 using (var image = this.CreateImage()) { }
139 catch (InvalidImageException)
141 this.isImage = false;
145 return this.isImage.Value;
149 /// <summary>IsImage の検証結果をキャッシュする。未検証なら null</summary>
150 private bool? isImage = null;
152 public MemoryImage CreateImage()
154 using (var fs = this.FileInfo.OpenRead())
156 return MemoryImage.CopyFromStream(fs);
160 public Stream OpenRead()
162 return this.FileInfo.OpenRead();
165 public void CopyTo(Stream stream)
167 using (var fs = this.FileInfo.OpenRead())
175 /// MemoryImage 用の MediaItem クラス
178 /// 用途の関係上、メモリ使用量が大きくなるため、不要になればできるだけ破棄すること
180 public class MemoryImageMediaItem : IMediaItem, IDisposable
182 public const string PathPrefix = "<>MemoryImage://";
183 private static int _fileNumber = 0;
185 private bool _disposed = false;
187 private readonly MemoryImage _image;
189 public MemoryImageMediaItem(MemoryImage image)
192 throw new ArgumentNullException(nameof(image));
196 var num = Interlocked.Increment(ref _fileNumber);
197 this.Path = PathPrefix + num + this._image.ImageFormatExt;
200 public string Path { get; }
204 get { return this.Path.Substring(PathPrefix.Length); }
207 public string Extension
209 get { return this._image.ImageFormatExt; }
214 get { return this._image != null; }
219 get { return this._image.Stream.Length; }
227 public bool IsDisposed
229 get { return this._disposed; }
232 public MemoryImage CreateImage()
234 return this._image.Clone();
237 public Stream OpenRead()
239 MemoryStream memstream = null;
243 memstream = new MemoryStream();
245 this._image.Stream.WriteTo(memstream);
246 memstream.Seek(0, SeekOrigin.Begin);
252 memstream?.Dispose();
257 public void CopyTo(Stream stream)
259 this._image.Stream.WriteTo(stream);
262 protected virtual void Dispose(bool disposing)
264 if (this._disposed) return;
268 this._image.Dispose();
271 this._disposed = true;
274 public void Dispose()
278 // 明示的にDisposeが呼ばれた場合はファイナライザを使用しない
279 GC.SuppressFinalize(this);
282 ~MemoryImageMediaItem()