OSDN Git Service

C# 8.0 のnull許容参照型を有効化
[opentween/open-tween.git] / OpenTween / MediaItem.cs
index 5fc7507..e243c21 100644 (file)
@@ -19,6 +19,8 @@
 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
 // Boston, MA 02110-1301, USA.
 
+#nullable enable
+
 using System;
 using System.Drawing;
 using System.IO;
@@ -60,6 +62,11 @@ namespace OpenTween
         bool IsImage { get; }
 
         /// <summary>
+        /// 代替テキスト (アップロード先が対応している必要がある)
+        /// </summary>
+        string? AltText { get; set; }
+
+        /// <summary>
         /// 表示用の MemoryImage を作成する
         /// </summary>
         /// <remarks>
@@ -87,11 +94,10 @@ namespace OpenTween
     public class FileMediaItem : IMediaItem
     {
         public FileInfo FileInfo { get; }
+        public string? AltText { get; set; }
 
         public FileMediaItem(string path)
-        {
-            this.FileInfo = new FileInfo(path);
-        }
+            => this.FileInfo = new FileInfo(path);
 
         public FileMediaItem(FileInfo fileInfo)
             : this(fileInfo.FullName)
@@ -99,29 +105,19 @@ namespace OpenTween
         }
 
         public string Path
-        {
-            get { return this.FileInfo.FullName; }
-        }
+            => this.FileInfo.FullName;
 
         public string Name
-        {
-            get { return this.FileInfo.Name; }
-        }
+            => this.FileInfo.Name;
 
         public string Extension
-        {
-            get { return this.FileInfo.Extension; }
-        }
+            => this.FileInfo.Extension;
 
         public bool Exists
-        {
-            get { return this.FileInfo.Exists; }
-        }
+            => this.FileInfo.Exists;
 
         public long Size
-        {
-            get { return this.FileInfo.Length; }
-        }
+            => this.FileInfo.Length;
 
         public bool IsImage
         {
@@ -151,23 +147,17 @@ namespace OpenTween
 
         public MemoryImage CreateImage()
         {
-            using (var fs = this.FileInfo.OpenRead())
-            {
-                return MemoryImage.CopyFromStream(fs);
-            }
+            using var fs = this.FileInfo.OpenRead();
+            return MemoryImage.CopyFromStream(fs);
         }
 
         public Stream OpenRead()
-        {
-            return this.FileInfo.OpenRead();
-        }
+            => this.FileInfo.OpenRead();
 
         public void CopyTo(Stream stream)
         {
-            using (var fs = this.FileInfo.OpenRead())
-            {
-                fs.CopyTo(stream);
-            }
+            using var fs = this.FileInfo.OpenRead();
+            fs.CopyTo(stream);
         }
     }
 
@@ -181,62 +171,42 @@ namespace OpenTween
     {
         public const string PathPrefix = "<>MemoryImage://";
         private static int _fileNumber = 0;
-
-        private bool _disposed = false;
-
         private readonly MemoryImage _image;
 
+        public bool IsDisposed { get; private set; } = false;
+
         public MemoryImageMediaItem(MemoryImage image)
         {
-            if (image == null)
-                throw new ArgumentNullException(nameof(image));
-
-            this._image = image;
+            this._image = image ?? throw new ArgumentNullException(nameof(image));
 
             var num = Interlocked.Increment(ref _fileNumber);
             this.Path = PathPrefix + num + this._image.ImageFormatExt;
         }
 
         public string Path { get; }
+        public string? AltText { get; set; }
 
         public string Name
-        {
-            get { return this.Path.Substring(PathPrefix.Length); }
-        }
+            => this.Path.Substring(PathPrefix.Length);
 
         public string Extension
-        {
-            get { return this._image.ImageFormatExt; }
-        }
+            => this._image.ImageFormatExt;
 
         public bool Exists
-        {
-            get { return this._image != null; }
-        }
+            => this._image != null;
 
         public long Size
-        {
-            get { return this._image.Stream.Length; }
-        }
+            => this._image.Stream.Length;
 
         public bool IsImage
-        {
-            get { return true; }
-        }
-
-        public bool IsDisposed
-        {
-            get { return this._disposed; }
-        }
+            => true;
 
         public MemoryImage CreateImage()
-        {
-            return this._image.Clone();
-        }
+            => this._image.Clone();
 
         public Stream OpenRead()
         {
-            MemoryStream memstream = null;
+            MemoryStream? memstream = null;
             try
             {
                 // コピーを作成する
@@ -255,20 +225,18 @@ namespace OpenTween
         }
 
         public void CopyTo(Stream stream)
-        {
-            this._image.Stream.WriteTo(stream);
-        }
+            => this._image.Stream.WriteTo(stream);
 
         protected virtual void Dispose(bool disposing)
         {
-            if (this._disposed) return;
+            if (this.IsDisposed) return;
 
             if (disposing)
             {
                 this._image.Dispose();
             }
 
-            this._disposed = true;
+            this.IsDisposed = true;
         }
 
         public void Dispose()
@@ -280,8 +248,6 @@ namespace OpenTween
         }
 
         ~MemoryImageMediaItem()
-        {
-            this.Dispose(false);
-        }
+            => this.Dispose(false);
     }
 }