OSDN Git Service

メンバの修飾子を調整
[opentween/open-tween.git] / OpenTween / MediaItem.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 spx (@5px)
3 // All rights reserved.
4 // 
5 // This file is part of OpenTween.
6 // 
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)
10 // any later version.
11 // 
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
15 // for more details. 
16 // 
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.
21
22 using System;
23 using System.Drawing;
24 using System.IO;
25 using System.Linq;
26 using System.Threading;
27
28 namespace OpenTween
29 {
30     public interface IMediaItem
31     {
32         /// <summary>
33         /// メディアへの絶対パス
34         /// </summary>
35         string Path { get; }
36
37         /// <summary>
38         /// メディア名
39         /// </summary>
40         string Name { get; }
41
42         /// <summary>
43         /// メディアの拡張子
44         /// </summary>
45         string Extension { get; }
46
47         /// <summary>
48         /// メディアが存在するかどうかを示す真偽値
49         /// </summary>
50         bool Exists { get; }
51
52         /// <summary>
53         /// メディアのサイズ(バイト単位)
54         /// </summary>
55         long Size { get; }
56
57         /// <summary>
58         /// 表示用の MemoryImage を作成する
59         /// </summary>
60         MemoryImage CreateImage();
61
62         /// <summary>
63         /// メディアの内容を Stream へ書き込む
64         /// </summary>
65         void CopyTo(Stream stream);
66     }
67
68     /// <summary>
69     /// ファイル用の MediaItem クラス
70     /// </summary>
71     public class FileMediaItem : IMediaItem
72     {
73         private readonly FileInfo _fileInfo;
74
75         public FileMediaItem(string path)
76         {
77             this._fileInfo = new FileInfo(path);
78         }
79
80         public FileMediaItem(FileInfo fileInfo)
81             : this(fileInfo.FullName)
82         {
83         }
84
85         public string Path
86         {
87             get { return this._fileInfo.FullName; }
88         }
89
90         public string Name
91         {
92             get { return this._fileInfo.Name; }
93         }
94
95         public string Extension
96         {
97             get { return this._fileInfo.Extension; }
98         }
99
100         public bool Exists
101         {
102             get { return this._fileInfo.Exists; }
103         }
104
105         public long Size
106         {
107             get { return this._fileInfo.Length; }
108         }
109
110         public MemoryImage CreateImage()
111         {
112             using (var fs = this._fileInfo.OpenRead())
113             {
114                 return MemoryImage.CopyFromStream(fs);
115             }
116         }
117
118         public void CopyTo(Stream stream)
119         {
120             using (var fs = this._fileInfo.OpenRead())
121             {
122                 fs.CopyTo(stream);
123             }
124         }
125
126         public FileInfo FileInfo
127         {
128             get { return this._fileInfo; }
129         }
130     }
131
132     /// <summary>
133     /// MemoryImage 用の MediaItem クラス
134     /// </summary>
135     /// <remarks>
136     /// 用途の関係上、メモリ使用量が大きくなるため、不要になればできるだけ破棄すること
137     /// </remarks>
138     public class MemoryImageMediaItem : IMediaItem, IDisposable
139     {
140         public const string PathPrefix = "<>MemoryImage://";
141         private static int _fileNumber = 0;
142
143         private bool _disposed = false;
144
145         private readonly string _path;
146         private readonly MemoryImage _image;
147
148         public MemoryImageMediaItem(Image image)
149         {
150             if (image == null)
151                 throw new ArgumentNullException("image");
152
153             // image から png 形式の MemoryImage を生成
154             using (var bitmap = new Bitmap(image))
155             {
156                 this._image = MemoryImage.CopyFromBitmap(bitmap);
157             }
158
159             var num = Interlocked.Increment(ref _fileNumber);
160             this._path = PathPrefix + num + this._image.ImageFormatExt;
161         }
162
163         public string Path
164         {
165             get { return this._path; }
166         }
167
168         public string Name
169         {
170             get { return this._path.Substring(PathPrefix.Length); }
171         }
172
173         public string Extension
174         {
175             get { return this._image.ImageFormatExt; }
176         }
177
178         public bool Exists
179         {
180             get { return this._image != null; }
181         }
182
183         public long Size
184         {
185             get { return this._image.Stream.Length; }
186         }
187
188         public MemoryImage CreateImage()
189         {
190             return this._image.Clone();
191         }
192
193         public void CopyTo(Stream stream)
194         {
195             this._image.Stream.Seek(0, SeekOrigin.Begin);
196             this._image.Stream.CopyTo(stream);
197         }
198
199         protected virtual void Dispose(bool disposing)
200         {
201             if (this._disposed) return;
202
203             if (disposing)
204             {
205                 this._image.Dispose();
206             }
207
208             this._disposed = true;
209         }
210
211         public void Dispose()
212         {
213             this.Dispose(true);
214
215             // 明示的にDisposeが呼ばれた場合はファイナライザを使用しない
216             GC.SuppressFinalize(this);
217         }
218
219         ~MemoryImageMediaItem()
220         {
221             this.Dispose(false);
222         }
223     }
224 }