Skip to content

Commit

Permalink
0.0.3, upgrade to csproj and bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkis117 committed Apr 5, 2017
1 parent 5a9c462 commit 992115b
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 212 deletions.
5 changes: 2 additions & 3 deletions .vs/restore.dg
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
#:C:\Users\clark\Source\Repos\Mime-Detective4\src\Mime-Detective\Mime-Detective.xproj
#:C:\Users\clark\Source\Repos\Mime-Detective4\test\Mime-Detective.Tests\Mime-Detective.Tests.xproj
C:\Users\clark\Source\Repos\Mime-Detective4\test\Mime-Detective.Tests\Mime-Detective.Tests.xproj|C:\Users\clark\Source\Repos\Mime-Detective4\src\Mime-Detective\Mime-Detective.xproj
#:C:\Users\clark\Source\Repos\Mime-Detective\test\Mime-Detective.Tests\Mime-Detective.Tests.xproj
C:\Users\clark\Source\Repos\Mime-Detective\test\Mime-Detective.Tests\Mime-Detective.Tests.xproj|C:\Users\clark\Source\Repos\Mime-Detective\src\Mime-Detective\Mime-Detective.xproj
5 changes: 2 additions & 3 deletions src/Mime-Detective/Extensions/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public static class ByteArrayExtensions
/// <param name="file">The FileInfo object.</param>
/// <returns>FileType or null not identified</returns>
public static FileType GetFileType(this byte[] bytes)
{
return MimeTypes.GetFileType(() => MimeTypes.ReadHeaderFromByteArray(bytes, MimeTypes.MaxHeaderSize), null, bytes);
}
=> MimeTypes.GetFileType(() => MimeTypes.ReadHeaderFromByteArray(bytes, MimeTypes.MaxHeaderSize), null, bytes);

}
}
9 changes: 3 additions & 6 deletions src/Mime-Detective/Extensions/FileInfo/ArchiveExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public static partial class FileInfoExtensions
/// <c>true</c> if the specified file info is zip; otherwise, <c>false</c>.
/// </returns>
public static bool IsZip(this FileInfo fileInfo)
{
return fileInfo.IsType(MimeTypes.ZIP);
}
=> fileInfo.IsType(MimeTypes.ZIP);

/// <summary>
/// Determines whether the specified file is RAR-archive.
Expand All @@ -27,8 +25,7 @@ public static bool IsZip(this FileInfo fileInfo)
/// <c>true</c> if the specified file info is RAR; otherwise, <c>false</c>.
/// </returns>
public static bool IsRar(this FileInfo fileInfo)
{
return fileInfo.IsType(MimeTypes.RAR);
}
=> fileInfo.IsType(MimeTypes.RAR);

}
}
114 changes: 53 additions & 61 deletions src/Mime-Detective/Extensions/FileInfo/DocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,70 +5,62 @@
namespace MimeDetective
{

/// <summary>
/// A set of extension methods for use with document formats.
/// </summary>
public static partial class FileInfoExtensions
{
/// <summary>
/// Determines whether the specified file is RTF document.
/// </summary>
/// <param name="fileInfo">The FileInfo.</param>
/// <returns>
/// <c>true</c> if the specified file is RTF; otherwise, <c>false</c>.
/// </returns>
public static bool IsRtf(this FileInfo fileInfo)
{
return fileInfo.IsType(MimeTypes.RTF);
}
/// <summary>
/// A set of extension methods for use with document formats.
/// </summary>
public static partial class FileInfoExtensions
{
/// <summary>
/// Determines whether the specified file is RTF document.
/// </summary>
/// <param name="fileInfo">The FileInfo.</param>
/// <returns>
/// <c>true</c> if the specified file is RTF; otherwise, <c>false</c>.
/// </returns>
public static bool IsRtf(this FileInfo fileInfo)
=> fileInfo.IsType(MimeTypes.RTF);

/// <summary>
/// Determines whether the specified file is PDF.
/// </summary>
/// <param name="file">The file.</param>
/// <returns>
/// <c>true</c> if the specified file is PDF; otherwise, <c>false</c>.
/// </returns>
public static bool IsPdf(this FileInfo file)
{
return file.IsType(MimeTypes.PDF);
}
/// <summary>
/// Determines whether the specified file is PDF.
/// </summary>
/// <param name="file">The file.</param>
/// <returns>
/// <c>true</c> if the specified file is PDF; otherwise, <c>false</c>.
/// </returns>
public static bool IsPdf(this FileInfo file)
=> file.IsType(MimeTypes.PDF);


/// <summary>
/// Determines whether the specified file info is ms-word document file
/// </summary>
/// <param name="fileInfo">The file info.</param>
/// <returns>
/// <c>true</c> if the specified file info is doc; otherwise, <c>false</c>.
/// </returns>
public static bool IsWord(this FileInfo fileInfo)
{
return (fileInfo.IsType(MimeTypes.WORD) || fileInfo.IsType(MimeTypes.WORDX));
}

/// <summary>
/// Determines whether the specified file info is ms-word document file
/// </summary>
/// <param name="fileInfo">The file info.</param>
/// <returns>
/// <c>true</c> if the specified file info is doc; otherwise, <c>false</c>.
/// </returns>
public static bool IsPowerPoint(this FileInfo fileInfo)
{
return (fileInfo.IsType(MimeTypes.PPT) || fileInfo.IsType(MimeTypes.PPTX));
}
/// <summary>
/// Determines whether the specified file info is ms-word document file
/// </summary>
/// <param name="fileInfo">The file info.</param>
/// <returns>
/// <c>true</c> if the specified file info is doc; otherwise, <c>false</c>.
/// </returns>
public static bool IsWord(this FileInfo fileInfo)
=> (fileInfo.IsType(MimeTypes.WORD) || fileInfo.IsType(MimeTypes.WORDX));

/// <summary>
/// Determines whether the specified file info is ms-word document file
/// </summary>
/// <param name="fileInfo">The file info.</param>
/// <returns>
/// <c>true</c> if the specified file info is doc; otherwise, <c>false</c>.
/// </returns>
public static bool IsExcel(this FileInfo fileInfo)
{
return (fileInfo.IsType(MimeTypes.EXCEL) || fileInfo.IsType(MimeTypes.EXCELX));
}
}

/// <summary>
/// Determines whether the specified file info is ms-word document file
/// </summary>
/// <param name="fileInfo">The file info.</param>
/// <returns>
/// <c>true</c> if the specified file info is doc; otherwise, <c>false</c>.
/// </returns>
public static bool IsPowerPoint(this FileInfo fileInfo)
=> (fileInfo.IsType(MimeTypes.PPT) || fileInfo.IsType(MimeTypes.PPTX));

/// <summary>
/// Determines whether the specified file info is ms-word document file
/// </summary>
/// <param name="fileInfo">The file info.</param>
/// <returns>
/// <c>true</c> if the specified file info is doc; otherwise, <c>false</c>.
/// </returns>
public static bool IsExcel(this FileInfo fileInfo)
=> (fileInfo.IsType(MimeTypes.EXCEL) || fileInfo.IsType(MimeTypes.EXCELX));
}
}
4 changes: 1 addition & 3 deletions src/Mime-Detective/Extensions/FileInfo/FileInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,7 @@ public static bool IsType(this FileInfo file, FileType type)
/// <param name="fileInfo"></param>
/// <returns></returns>
public static bool IsExe(this FileInfo fileInfo)
{
return fileInfo.IsType(MimeTypes.DLL_EXE);
}
=> fileInfo.IsType(MimeTypes.DLL_EXE);

/// <summary>
/// Check if the file is Microsoft Installer.
Expand Down
72 changes: 33 additions & 39 deletions src/Mime-Detective/Extensions/FileInfo/GraphicsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,39 @@

namespace MimeDetective
{
/// <summary>
/// A set of extension methods for use with graphics formats.
/// </summary>
public static partial class FileInfoExtensions
{
/// <summary>
/// Determines whether the specified file is PNG.
/// </summary>
/// <param name="fileInfo">The FileInfo object</param>
/// <returns>
/// <c>true</c> if the specified file info is PNG; otherwise, <c>false</c>.
/// </returns>
public static bool IsPng(this FileInfo fileInfo)
{
return fileInfo.IsType(MimeTypes.PNG);
}
/// <summary>
/// A set of extension methods for use with graphics formats.
/// </summary>
public static partial class FileInfoExtensions
{
/// <summary>
/// Determines whether the specified file is PNG.
/// </summary>
/// <param name="fileInfo">The FileInfo object</param>
/// <returns>
/// <c>true</c> if the specified file info is PNG; otherwise, <c>false</c>.
/// </returns>
public static bool IsPng(this FileInfo fileInfo)
=> fileInfo.IsType(MimeTypes.PNG);

/// <summary>
/// Determines whether the specified file is GIF image
/// </summary>
/// <param name="fileInfo">The FileInfo object</param>
/// <returns>
/// <c>true</c> if the specified file info is GIF; otherwise, <c>false</c>.
/// </returns>
public static bool IsGif(this FileInfo fileInfo)
{
return fileInfo.IsType(MimeTypes.GIF);
}
/// <summary>
/// Determines whether the specified file is GIF image
/// </summary>
/// <param name="fileInfo">The FileInfo object</param>
/// <returns>
/// <c>true</c> if the specified file info is GIF; otherwise, <c>false</c>.
/// </returns>
public static bool IsGif(this FileInfo fileInfo)
=> fileInfo.IsType(MimeTypes.GIF);

/// <summary>
/// Determines whether the specified file is JPEG image
/// </summary>
/// <param name="fileInfo">The FileInfo.</param>
/// <returns>
/// <c>true</c> if the specified file info is JPEG; otherwise, <c>false</c>.
/// </returns>
public static bool IsJpeg(this FileInfo fileInfo)
{
return fileInfo.IsType(MimeTypes.JPEG);
}
}
/// <summary>
/// Determines whether the specified file is JPEG image
/// </summary>
/// <param name="fileInfo">The FileInfo.</param>
/// <returns>
/// <c>true</c> if the specified file info is JPEG; otherwise, <c>false</c>.
/// </returns>
public static bool IsJpeg(this FileInfo fileInfo)
=> fileInfo.IsType(MimeTypes.JPEG);
}
}
55 changes: 10 additions & 45 deletions src/Mime-Detective/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ namespace MimeDetective.Extensions
{
public static class StreamExtensions
{

//todo fix so doesn't create temp file
/// <summary>
/// Read header of a stream and depending on the information in the header
/// return object FileType.
Expand All @@ -19,12 +17,12 @@ public static class StreamExtensions
/// </summary>
/// <param name="file">The FileInfo object.</param>
/// <returns>FileType or null not identified</returns>
public static FileType GetFileType(this Stream stream)
{
return MimeTypes.GetFileType(() => MimeTypes.ReadHeaderFromStream(stream, MimeTypes.MaxHeaderSize), stream);
}

//todo fix so doesn't create temp file
public static FileType GetFileType(this Stream stream)
=> MimeTypes.GetFileType
(MimeTypes.ReadHeaderFromStream
(stream, MimeTypes.MaxHeaderSize),
stream, null, shouldDisposeStream: false);

/// <summary>
/// Read header of a stream and depending on the information in the header
/// return object FileType.
Expand All @@ -33,42 +31,9 @@ public static FileType GetFileType(this Stream stream)
/// </summary>
/// <param name="file">The FileInfo object.</param>
/// <returns>FileType or null not identified</returns>
public static Task<FileType> GetFileTypeAsync(this Stream stream)
{
return MimeTypes.GetFileTypeAsync(() => MimeTypes.ReadHeaderFromStreamAsync(stream, MimeTypes.MaxHeaderSize), stream);
}

//todo fix so doesn't create temp file
/// <summary>
/// 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
/// </summary>
/// <param name="file">The FileInfo object.</param>
/// <returns>FileType or null not identified</returns>
/*
public static FileType GetFileTypeViaTempFile(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 = FileInfoExtensions.GetFileType(new FileInfo(fileName));
}
finally
{
File.Delete(fileName);
}
return fileType;
}
*/
public static async Task<FileType> GetFileTypeAsync(this Stream stream)
=> MimeTypes.GetFileType
(await MimeTypes.ReadHeaderFromStreamAsync(stream, MimeTypes.MaxHeaderSize),
stream, null, shouldDisposeStream: false);
}
}
28 changes: 14 additions & 14 deletions src/Mime-Detective/FileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,35 @@ public FileType(byte?[] header, string extension, string mime, ushort offset = 0

public static bool operator ==(FileType a, FileType b)
{
return a.Equals(b);
}
if (a is null && b is null)
return true;

public static bool operator !=(FileType a, FileType b)
{
return !(a.Equals(b));
if (b is null)
return a.Equals(b);
else
return b.Equals(a);
}

public static bool operator !=(FileType a, FileType b) => !(a == b);

public override bool Equals(object other)
{
if (other is null)
return false;

if (!(other is FileType))
return false;

FileType otherType = (FileType)other;

if (this.Extension == otherType.Extension && this.Mime == otherType.Mime)
if (Extension == otherType.Extension && Mime == otherType.Mime)
return true;

return base.Equals(other);
}

public override int GetHashCode()
{
return base.GetHashCode();
}
public override int GetHashCode() => (Header.GetHashCode() ^ HeaderOffset ^ Extension.GetHashCode() ^ Mime.GetHashCode());

public override string ToString()
{
return Extension;
}
public override string ToString() => Extension;
}
}
7 changes: 7 additions & 0 deletions src/Mime-Detective/Mime-Detective.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@
<Reference Include="System.IO.Compression" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net46' ">
<Reference Include="System" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.IO" />
<Reference Include="System.IO.Compression" />
</ItemGroup>

<ItemGroup Condition=" ' $(TargetFramework)' == 'netstandard1.3' ">
<PackageReference Include="System.IO" />
<PackageReference Include="System.IO.Compression" />
Expand Down
Loading

0 comments on commit 992115b

Please sign in to comment.