From 0402b23e140c34ab2eeab5bf93344f6fd35fd712 Mon Sep 17 00:00:00 2001 From: Vince Panuccio Date: Tue, 24 Nov 2015 16:19:06 +1100 Subject: [PATCH] Move extension methods in to a separate file Extension methods are syntactic sugar to the main API 'master' methods. Moved externally to separate out their usage. Will maintain backward compatibility. --- MimeDetective/BaseExtensions.cs | 92 ++++++++++++++++++++++++++++++ MimeDetective/MimeDetective.csproj | 1 + MimeDetective/MimeTypes.cs | 87 +--------------------------- 3 files changed, 96 insertions(+), 84 deletions(-) create mode 100644 MimeDetective/BaseExtensions.cs diff --git a/MimeDetective/BaseExtensions.cs b/MimeDetective/BaseExtensions.cs new file mode 100644 index 0000000..70317c5 --- /dev/null +++ b/MimeDetective/BaseExtensions.cs @@ -0,0 +1,92 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace MimeDetective +{ + public static class BaseExtensions + { + /// + /// Read header of bytes and depending on the information in the header + /// return object FileType. + /// Return null in case when the file type is not identified. + /// Throws Application exception if the file can not be read or does not exist + /// + /// + /// A temp file is written to get a FileInfo from the given bytes. + /// If this is not intended use + /// + /// GetFileType(() => bytes); + /// + /// + /// The FileInfo object. + /// FileType or null not identified + public static FileType GetFileType(this byte[] bytes) + { + return MimeTypes.GetFileType(() => bytes); + } + + /// + /// Read header of a stream and depending on the information in the header + /// return object FileType. + /// Return null in case when the file type is not identified. + /// Throws Application exception if the file can not be read or does not exist + /// + /// The FileInfo object. + /// The stream from which to read for determining the MIME type. + /// FileType or null not identified + public static FileType GetFileType(this Stream stream) + { + if (stream.CanSeek) + { + long currentLocation = stream.Position; + + stream.Seek(0, SeekOrigin.Begin); + + byte[] buffer = new byte[MimeTypes.MaxHeaderSize]; + + stream.Read(buffer, 0, MimeTypes.MaxHeaderSize); + + stream.Seek(currentLocation, SeekOrigin.Begin); + + return MimeTypes.GetFileType(() => buffer); + } + + return new FileType(); + } + + /// + /// Read header of a file and depending on the information in the header + /// return object FileType. + /// Return null in case when the file type is not identified. + /// Throws Application exception if the file can not be read or does not exist + /// + /// The FileInfo object. + /// FileType or null not identified + public static FileType GetFileType(this FileInfo file) + { + return MimeTypes.GetFileType(() => MimeTypes.ReadFileHeader(file), file.FullName); + } + + /// + /// Determines whether the specified file is of provided type + /// + /// The file. + /// The FileType + /// + /// true if the specified file is type; otherwise, false. + /// + public static bool IsType(this FileInfo file, FileType type) + { + FileType actualType = file.GetFileType(); + + if (null == actualType) + return false; + + return (actualType.Equals(type)); + } + } +} diff --git a/MimeDetective/MimeDetective.csproj b/MimeDetective/MimeDetective.csproj index 8357da3..86f50ec 100644 --- a/MimeDetective/MimeDetective.csproj +++ b/MimeDetective/MimeDetective.csproj @@ -41,6 +41,7 @@ + diff --git a/MimeDetective/MimeTypes.cs b/MimeDetective/MimeTypes.cs index 03ef184..f5e6a4d 100644 --- a/MimeDetective/MimeTypes.cs +++ b/MimeDetective/MimeTypes.cs @@ -179,69 +179,7 @@ public static void LoadFromXmlFile(string path) foreach (var type in tmpTypes) types.Add(type); } - } - - /// - /// Read header of bytes and depending on the information in the header - /// return object FileType. - /// Return null in case when the file type is not identified. - /// Throws Application exception if the file can not be read or does not exist - /// - /// - /// A temp file is written to get a FileInfo from the given bytes. - /// If this is not intended use - /// - /// GetFileType(() => bytes); - /// - /// - /// The FileInfo object. - /// FileType or null not identified - public static FileType GetFileType(this byte[] bytes) - { - return GetFileType(new MemoryStream(bytes)); - } - - /// - /// Read header of a stream and depending on the information in the header - /// return object FileType. - /// Return null in case when the file type is not identified. - /// Throws Application exception if the file can not be read or does not exist - /// - /// The FileInfo object. - /// FileType or null not identified - public static FileType GetFileType(this Stream stream) - { - FileType fileType = null; - var fileName = Path.GetTempFileName(); - - try - { - using (var fileStream = File.Create(fileName)) - { - stream.Seek(0, SeekOrigin.Begin); - stream.CopyTo(fileStream); - } - fileType = GetFileType(new FileInfo(fileName)); - } - finally - { - File.Delete(fileName); - } - return fileType; - } - - /// - /// Read header of a file and depending on the information in the header - /// return object FileType. - /// Return null in case when the file type is not identified. - /// Throws Application exception if the file can not be read or does not exist - /// - /// The FileInfo object. - /// FileType or null not identified - public static FileType GetFileType(this FileInfo file) - { - return GetFileType(() => ReadFileHeader(file, MaxHeaderSize), file.FullName); - } + } /// /// Read header of a file and depending on the information in the header @@ -410,7 +348,7 @@ private static int GetFileMatchingCount(byte[] fileHeader, FileType type) /// /// The file to work with /// Array of bytes - private static Byte[] ReadFileHeader(FileInfo file, int MaxHeaderSize) + internal static Byte[] ReadFileHeader(FileInfo file) { Byte[] header = new byte[MaxHeaderSize]; try // read file @@ -431,26 +369,7 @@ private static Byte[] ReadFileHeader(FileInfo file, int MaxHeaderSize) } #endregion - #region isType functions - - - /// - /// Determines whether the specified file is of provided type - /// - /// The file. - /// The FileType - /// - /// true if the specified file is type; otherwise, false. - /// - public static bool IsType(this FileInfo file, FileType type) - { - FileType actualType = GetFileType(file); - - if (null == actualType) - return false; - - return (actualType.Equals(type)); - } + #region isType functions /// /// Determines whether the specified file is MS Excel spreadsheet