From 4203557f91fd55e3dd89c27d43ee48b06cd8a84c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Caio=20C=C3=A9sar=20Silva=20Sousa?=
 <caiyo.projeto@gmail.com>
Date: Tue, 9 Mar 2021 19:54:16 -0300
Subject: [PATCH] Include GetMimeType and Tests.

---
 .../Codout.Framework.Common.csproj            |  1 +
 Codout.Framework.Common/Extensions/IO.cs      | 18 +++++++++++++--
 Codout.Framework.sln                          | 13 ++++++++++-
 .../Codout.Framework.Common.Test.csproj       | 20 +++++++++++++++++
 .../Extensions/IOTest.cs                      | 22 +++++++++++++++++++
 5 files changed, 71 insertions(+), 3 deletions(-)
 create mode 100644 tests/Codout.Framework.Common.Test/Codout.Framework.Common.Test.csproj
 create mode 100644 tests/Codout.Framework.Common.Test/Extensions/IOTest.cs

diff --git a/Codout.Framework.Common/Codout.Framework.Common.csproj b/Codout.Framework.Common/Codout.Framework.Common.csproj
index cf333a3..44c11e6 100644
--- a/Codout.Framework.Common/Codout.Framework.Common.csproj
+++ b/Codout.Framework.Common/Codout.Framework.Common.csproj
@@ -22,6 +22,7 @@
   </PropertyGroup>
 
   <ItemGroup>
+    <PackageReference Include="MimeTypesMap" Version="1.0.8" />
     <PackageReference Include="System.ComponentModel.Annotations" Version="5.0.0" />
   </ItemGroup>
 
diff --git a/Codout.Framework.Common/Extensions/IO.cs b/Codout.Framework.Common/Extensions/IO.cs
index 6b2bef8..5563b10 100644
--- a/Codout.Framework.Common/Extensions/IO.cs
+++ b/Codout.Framework.Common/Extensions/IO.cs
@@ -1,4 +1,5 @@
-﻿using System.IO;
+﻿using HeyRed.Mime;
+using System.IO;
 using System.Net;
 
 namespace Codout.Framework.Common.Extensions
@@ -73,7 +74,7 @@ public static string ReadWebPage(string url)
             var request = WebRequest.Create(url);
             // ReSharper disable PossibleNullReferenceException
             using (Stream stream = request.GetResponse().GetResponseStream())
-                // ReSharper restore PossibleNullReferenceException
+            // ReSharper restore PossibleNullReferenceException
             {
                 if (stream != null)
                 {
@@ -550,6 +551,19 @@ public static string MimeType(this string strFileName)
                 }
             return retval;
         }
+
+        public static string GetMimeType(this string fileName)
+        {
+            if (string.IsNullOrWhiteSpace(fileName) || !Path.HasExtension(fileName))
+                return null;
+
+            switch (Path.GetExtension(fileName).ToLower())
+            {
+                case ".ogg": return "video/ogg";
+            }
+
+            return MimeTypesMap.GetMimeType(fileName);
+        }
         #endregion
     }
 }
diff --git a/Codout.Framework.sln b/Codout.Framework.sln
index 0ca40f4..51d8e18 100644
--- a/Codout.Framework.sln
+++ b/Codout.Framework.sln
@@ -27,7 +27,11 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Codout.Mailer.SendGrid", "C
 EndProject
 Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Codout.Multitenancy", "Codout.Multitenancy\Codout.Multitenancy.csproj", "{5D77842D-DD96-4927-976A-FFFD8207ED70}"
 EndProject
-Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Codout.Mailer.AWS", "Codout.Mailer.AWS\Codout.Mailer.AWS.csproj", "{A51B2EDB-B261-41B2-B86F-BBA84AE46288}"
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Codout.Mailer.AWS", "Codout.Mailer.AWS\Codout.Mailer.AWS.csproj", "{A51B2EDB-B261-41B2-B86F-BBA84AE46288}"
+EndProject
+Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Codout.Framework.Common.Test", "tests\Codout.Framework.Common.Test\Codout.Framework.Common.Test.csproj", "{6BDCEA2D-905D-4D64-AEB2-ECDFFA1D1451}"
+EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{4AB797AD-1525-46E7-B738-6B2400602CA4}"
 EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -87,10 +91,17 @@ Global
 		{A51B2EDB-B261-41B2-B86F-BBA84AE46288}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{A51B2EDB-B261-41B2-B86F-BBA84AE46288}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{A51B2EDB-B261-41B2-B86F-BBA84AE46288}.Release|Any CPU.Build.0 = Release|Any CPU
+		{6BDCEA2D-905D-4D64-AEB2-ECDFFA1D1451}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{6BDCEA2D-905D-4D64-AEB2-ECDFFA1D1451}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{6BDCEA2D-905D-4D64-AEB2-ECDFFA1D1451}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{6BDCEA2D-905D-4D64-AEB2-ECDFFA1D1451}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE
 	EndGlobalSection
+	GlobalSection(NestedProjects) = preSolution
+		{6BDCEA2D-905D-4D64-AEB2-ECDFFA1D1451} = {4AB797AD-1525-46E7-B738-6B2400602CA4}
+	EndGlobalSection
 	GlobalSection(ExtensibilityGlobals) = postSolution
 		SolutionGuid = {F077B358-8F0D-4791-8E85-DDED3550C27A}
 	EndGlobalSection
diff --git a/tests/Codout.Framework.Common.Test/Codout.Framework.Common.Test.csproj b/tests/Codout.Framework.Common.Test/Codout.Framework.Common.Test.csproj
new file mode 100644
index 0000000..65ae88b
--- /dev/null
+++ b/tests/Codout.Framework.Common.Test/Codout.Framework.Common.Test.csproj
@@ -0,0 +1,20 @@
+<Project Sdk="Microsoft.NET.Sdk">
+
+  <PropertyGroup>
+    <TargetFramework>net5.0</TargetFramework>
+
+    <IsPackable>false</IsPackable>
+  </PropertyGroup>
+
+  <ItemGroup>
+    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
+    <PackageReference Include="MSTest.TestAdapter" Version="2.1.1" />
+    <PackageReference Include="MSTest.TestFramework" Version="2.1.1" />
+    <PackageReference Include="coverlet.collector" Version="1.3.0" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <ProjectReference Include="..\..\Codout.Framework.Common\Codout.Framework.Common.csproj" />
+  </ItemGroup>
+
+</Project>
diff --git a/tests/Codout.Framework.Common.Test/Extensions/IOTest.cs b/tests/Codout.Framework.Common.Test/Extensions/IOTest.cs
new file mode 100644
index 0000000..7fcf436
--- /dev/null
+++ b/tests/Codout.Framework.Common.Test/Extensions/IOTest.cs
@@ -0,0 +1,22 @@
+﻿using Codout.Framework.Common.Extensions;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+
+namespace Codout.Framework.Common.Test.Extensions
+{
+    [TestClass]
+    public class IOTest
+    {
+        [TestMethod]
+        [DataRow(".JPG", "image/jpeg")]
+        [DataRow(".ogg", "video/ogg")]
+        [DataRow(".webm", "video/webm")]
+        [DataRow(null, null)]
+        [DataRow("", null)]
+        [DataRow("noFileExtension", null)]
+        [DataRow(".invalidExtension", "application/octet-stream")]
+        public void GetMimeTypeTest(string fileName, string expectedMimeType)
+        {
+            Assert.AreEqual(expectedMimeType, IO.GetMimeType(fileName));
+        }
+    }
+}
\ No newline at end of file
