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.
28 using System.Threading;
32 public interface IMediaItem
47 string Extension { get; }
50 /// メディアが存在するかどうかを示す真偽値
60 /// メディアが画像であるかどうかを示す真偽値
65 /// 代替テキスト (アップロード先が対応している必要がある)
67 string? AltText { get; set; }
70 /// 表示用の MemoryImage を作成する
75 MemoryImage CreateImage();
78 /// メディアの内容を読み込むための Stream を開く
86 /// メディアの内容を Stream へ書き込む
88 void CopyTo(Stream stream);
92 /// ファイル用の MediaItem クラス
94 public class FileMediaItem : IMediaItem
96 public FileInfo FileInfo { get; }
97 public string? AltText { get; set; }
99 public FileMediaItem(string path)
100 => this.FileInfo = new FileInfo(path);
102 public FileMediaItem(FileInfo fileInfo)
103 : this(fileInfo.FullName)
108 => this.FileInfo.FullName;
111 => this.FileInfo.Name;
113 public string Extension
114 => this.FileInfo.Extension;
117 => this.FileInfo.Exists;
120 => this.FileInfo.Length;
126 if (this.isImage == null)
130 // MemoryImage が生成できるかを検証する
131 using (var image = this.CreateImage()) { }
135 catch (InvalidImageException)
137 this.isImage = false;
141 return this.isImage.Value;
145 /// <summary>IsImage の検証結果をキャッシュする。未検証なら null</summary>
146 private bool? isImage = null;
148 public MemoryImage CreateImage()
150 using var fs = this.FileInfo.OpenRead();
151 return MemoryImage.CopyFromStream(fs);
154 public Stream OpenRead()
155 => this.FileInfo.OpenRead();
157 public void CopyTo(Stream stream)
159 using var fs = this.FileInfo.OpenRead();
165 /// MemoryImage 用の MediaItem クラス
168 /// 用途の関係上、メモリ使用量が大きくなるため、不要になればできるだけ破棄すること
170 public class MemoryImageMediaItem : IMediaItem, IDisposable
172 public const string PathPrefix = "<>MemoryImage://";
173 private static int _fileNumber = 0;
174 private readonly MemoryImage _image;
176 public bool IsDisposed { get; private set; } = false;
178 public MemoryImageMediaItem(MemoryImage image)
180 this._image = image ?? throw new ArgumentNullException(nameof(image));
182 var num = Interlocked.Increment(ref _fileNumber);
183 this.Path = PathPrefix + num + this._image.ImageFormatExt;
186 public string Path { get; }
187 public string? AltText { get; set; }
190 => this.Path.Substring(PathPrefix.Length);
192 public string Extension
193 => this._image.ImageFormatExt;
196 => this._image != null;
199 => this._image.Stream.Length;
204 public MemoryImage CreateImage()
205 => this._image.Clone();
207 public Stream OpenRead()
209 MemoryStream? memstream = null;
213 memstream = new MemoryStream();
215 this._image.Stream.WriteTo(memstream);
216 memstream.Seek(0, SeekOrigin.Begin);
222 memstream?.Dispose();
227 public void CopyTo(Stream stream)
228 => this._image.Stream.WriteTo(stream);
230 protected virtual void Dispose(bool disposing)
232 if (this.IsDisposed) return;
236 this._image.Dispose();
239 this.IsDisposed = true;
242 public void Dispose()
246 // 明示的にDisposeが呼ばれた場合はファイナライザを使用しない
247 GC.SuppressFinalize(this);
250 ~MemoryImageMediaItem()
251 => this.Dispose(false);