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())
137 catch (InvalidImageException)
139 this.isImage = false;
143 return this.isImage.Value;
147 /// <summary>IsImage の検証結果をキャッシュする。未検証なら null</summary>
148 private bool? isImage = null;
150 public MemoryImage CreateImage()
152 using var fs = this.FileInfo.OpenRead();
153 return MemoryImage.CopyFromStream(fs);
156 public Stream OpenRead()
157 => this.FileInfo.OpenRead();
159 public void CopyTo(Stream stream)
161 using var fs = this.FileInfo.OpenRead();
167 /// MemoryImage 用の MediaItem クラス
170 /// 用途の関係上、メモリ使用量が大きくなるため、不要になればできるだけ破棄すること
172 public class MemoryImageMediaItem : IMediaItem, IDisposable
174 public const string PathPrefix = "<>MemoryImage://";
175 private static int fileNumber = 0;
176 private readonly MemoryImage image;
178 public bool IsDisposed { get; private set; } = false;
180 public MemoryImageMediaItem(MemoryImage image)
182 this.image = image ?? throw new ArgumentNullException(nameof(image));
184 var num = Interlocked.Increment(ref fileNumber);
185 this.Path = PathPrefix + num + this.image.ImageFormatExt;
188 public string Path { get; }
189 public string? AltText { get; set; }
192 => this.Path.Substring(PathPrefix.Length);
194 public string Extension
195 => this.image.ImageFormatExt;
198 => this.image != null;
201 => this.image.Stream.Length;
206 public MemoryImage CreateImage()
207 => this.image.Clone();
209 public Stream OpenRead()
211 MemoryStream? memstream = null;
215 memstream = new MemoryStream();
217 this.image.Stream.WriteTo(memstream);
218 memstream.Seek(0, SeekOrigin.Begin);
224 memstream?.Dispose();
229 public void CopyTo(Stream stream)
230 => this.image.Stream.WriteTo(stream);
232 protected virtual void Dispose(bool disposing)
234 if (this.IsDisposed) return;
238 this.image.Dispose();
241 this.IsDisposed = true;
244 public void Dispose()
248 // 明示的にDisposeが呼ばれた場合はファイナライザを使用しない
249 GC.SuppressFinalize(this);
252 ~MemoryImageMediaItem()
253 => this.Dispose(false);