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.
23 #pragma warning disable SA1649
29 using System.Threading;
33 public interface IMediaItem : IDisposable
41 /// メディアが既に破棄されているかを示す真偽値
43 bool IsDisposed { get; }
58 string Extension { get; }
61 /// メディアが存在するかどうかを示す真偽値
71 /// 代替テキスト (アップロード先が対応している必要がある)
73 string? AltText { get; set; }
76 /// 表示用の MemoryImage を作成する
81 MemoryImage CreateImage();
84 /// メディアの内容を読み込むための Stream を開く
92 /// メディアの内容を Stream へ書き込む
94 void CopyTo(Stream stream);
98 /// ファイル用の MediaItem クラス
100 public class FileMediaItem : IMediaItem
102 public FileInfo FileInfo { get; }
104 public string? AltText { get; set; }
106 public FileMediaItem(string path)
107 => this.FileInfo = new FileInfo(path);
109 public FileMediaItem(FileInfo fileInfo)
110 : this(fileInfo.FullName)
114 public Guid Id { get; } = Guid.NewGuid();
116 public bool IsDisposed { get; private set; } = false;
119 => this.FileInfo.FullName;
122 => this.FileInfo.Name;
124 public string Extension
125 => this.FileInfo.Extension;
128 => this.FileInfo.Exists;
131 => this.FileInfo.Length;
133 public MemoryImage CreateImage()
135 using var fs = this.FileInfo.OpenRead();
136 return MemoryImage.CopyFromStream(fs);
139 public Stream OpenRead()
140 => this.FileInfo.OpenRead();
142 public void CopyTo(Stream stream)
144 using var fs = this.FileInfo.OpenRead();
148 public void Dispose()
153 this.IsDisposed = true;
158 /// MemoryImage 用の MediaItem クラス
161 /// 用途の関係上、メモリ使用量が大きくなるため、不要になればできるだけ破棄すること
163 public class MemoryImageMediaItem : IMediaItem
165 public const string PathPrefix = "<>MemoryImage://";
166 private static int fileNumber = 0;
167 private readonly MemoryImage image;
169 public bool IsDisposed { get; private set; } = false;
171 public MemoryImageMediaItem(MemoryImage image)
173 this.image = image ?? throw new ArgumentNullException(nameof(image));
175 var num = Interlocked.Increment(ref fileNumber);
176 this.Path = PathPrefix + num + this.image.ImageFormatExt;
179 public Guid Id { get; } = Guid.NewGuid();
181 public string Path { get; }
183 public string? AltText { get; set; }
186 => this.Path.Substring(PathPrefix.Length);
188 public string Extension
189 => this.image.ImageFormatExt;
192 => this.image != null;
195 => this.image.Stream.Length;
197 public MemoryImage CreateImage()
198 => this.image.Clone();
200 public Stream OpenRead()
202 MemoryStream? memstream = null;
206 memstream = new MemoryStream();
208 this.image.Stream.WriteTo(memstream);
209 memstream.Seek(0, SeekOrigin.Begin);
215 memstream?.Dispose();
220 public void CopyTo(Stream stream)
221 => this.image.Stream.WriteTo(stream);
223 protected virtual void Dispose(bool disposing)
225 if (this.IsDisposed) return;
229 this.image.Dispose();
232 this.IsDisposed = true;
235 public void Dispose()
239 // 明示的にDisposeが呼ばれた場合はファイナライザを使用しない
240 GC.SuppressFinalize(this);
243 ~MemoryImageMediaItem()
244 => this.Dispose(false);