Skip to content

Commit

Permalink
Added additional tests, changed FileType back to Class
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkis117 committed Feb 27, 2017
1 parent 22aa90e commit 9214d28
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 70 deletions.
11 changes: 5 additions & 6 deletions src/Mime-Detective/Extensions/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,21 @@ public static class ByteArrayExtensions
/// <summary>
/// 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.
/// 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>
/// <remarks>
/// A temp file is written to get a FileInfo from the given bytes.
/// If this is not intended use
///
/// GetFileType(() => bytes);
///
/// If this is not intended use
///
/// GetFileType(() => bytes);
///
/// </remarks>
/// <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);
}

}
}
6 changes: 3 additions & 3 deletions src/Mime-Detective/Extensions/StreamExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public static class StreamExtensions
/// <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.
/// 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>
Expand All @@ -28,7 +28,7 @@ public static FileType GetFileType(this Stream stream)
/// <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.
/// 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>
Expand All @@ -42,7 +42,7 @@ public static Task<FileType> GetFileTypeAsync(this Stream stream)
/// <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.
/// 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>
Expand Down
20 changes: 2 additions & 18 deletions src/Mime-Detective/FileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,12 @@

namespace MimeDetective
{
/*
public struct FileDetectionResult
{
public readonly FileType FileType;
public readonly bool WasFileTypeDetected;
public FileDetectionResult(bool wasFileTypeDetected, FileType fileType)
{
FileType = fileType;
WasFileTypeDetected = wasFileTypeDetected;
}
}
*/

/// <summary>
/// Little data structure to hold information about file types.
/// Holds information about binary header at the start of the file
/// these are mostly static they can be structs
/// </summary>
public struct FileType
public class FileType
{
public byte?[] Header { get; }
public ushort HeaderOffset { get; }
Expand All @@ -42,7 +26,7 @@ public FileType(byte?[] header, string extension, string mime, ushort offset = 0
{
//header cannot be null, file type normal operation requires the data
if (header == null)
throw new ArgumentNullException(nameof(header), "cannot be null file type needs file header data");
throw new ArgumentNullException(nameof(header), "cannot be null, FileType needs file header data");

Header = header;
HeaderOffset = offset;
Expand Down
2 changes: 1 addition & 1 deletion src/Mime-Detective/MimeDetective.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static FileType LearnMimeType(FileInfo file, string mimeType, int headerS
return new FileType(data, file.Extension, mimeType, offset);
}

public static FileType? LearnMimeType(FileInfo first, FileInfo second, string mimeType, int maxHeaderSize = 12, int minMatches = 2, int maxNonMatch = 3)
public static FileType LearnMimeType(FileInfo first, FileInfo second, string mimeType, int maxHeaderSize = 12, int minMatches = 2, int maxNonMatch = 3)
{
byte?[] header = null;

Expand Down
52 changes: 17 additions & 35 deletions src/Mime-Detective/MimeTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,6 @@ EML is also used by Outlook Express and QuickMail.

#region Main Methods

/*
public static void SaveToXmlFile(string path)
{
using (FileStream file = File.OpenWrite(path))
Expand All @@ -226,15 +225,8 @@ public static FileType[] LoadFromXmlFile(string path)
var serializer = new System.Xml.Serialization.XmlSerializer(Types.GetType());

return (FileType[])serializer.Deserialize(file);
//int typeOrgLenth = Types.Length;
//Array.Resize(ref Types, Types.Length + tmpTypes.Length);
//Array.Copy(tmpTypes, 0, Types, typeOrgLenth, tmpTypes.Length);
}
}
*/

/*
public static FileType GetFileType(FileInfo file)
Expand All @@ -247,7 +239,6 @@ public static FileType GetFileType(FileInfo file)
/// 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
/// </summary>
/// <param name="fileHeaderReadFunc">A function which returns the bytes found</param>
/// <param name="fileFullName">If given and file typ is a zip file, a check for docx and xlsx is done</param>
Expand Down Expand Up @@ -294,13 +285,13 @@ private static FileType getFileType(IReadOnlyList<byte> fileHeader, Stream strea
var officeXml = CheckForDocxAndXlsxStream(zipData);

if (officeXml != null)
return officeXml.Value;
return officeXml;

//check for open office formats
var openOffice = CheckForOdtAndOds(zipData);

if (openOffice != null)
return openOffice.Value;
return openOffice;
}
}
}
Expand All @@ -311,7 +302,8 @@ private static FileType getFileType(IReadOnlyList<byte> fileHeader, Stream strea
}
}

throw new Exception("No file type match found");
//no match return null
return null;
}

/// <summary>
Expand All @@ -333,7 +325,7 @@ public static List<FileType> GetFileTypesByExtensions(string CSV)
return result;
}

private static FileType? CheckForDocxAndXlsxStream(ZipArchive zipData)
private static FileType CheckForDocxAndXlsxStream(ZipArchive zipData)
{
if (zipData.Entries.Any(e => e.FullName.StartsWith("word/")))
return WORDX;
Expand Down Expand Up @@ -365,7 +357,7 @@ private static FileType CheckForDocxAndXlsx(FileType type, FileInfo fileInfo)
*/

//check for open doc formats
private static FileType? CheckForOdtAndOds(ZipArchive zipFile)
private static FileType CheckForOdtAndOds(ZipArchive zipFile)
{
var ooMimeType = zipFile.Entries.FirstOrDefault(e => e.FullName == "mimetype");

Expand Down Expand Up @@ -431,7 +423,7 @@ internal static IReadOnlyList<byte> ReadFileHeader(FileInfo file, ushort MaxHead
}
catch (Exception e) // file could not be found/read
{
throw new Exception($"Could not read file: {e.Message}");
throw new System.IO.IOException($"Could not read {nameof(file)}", e);
}

return header;
Expand All @@ -454,7 +446,7 @@ internal static async Task<IReadOnlyList<byte>> ReadFileHeaderAsync(FileInfo fil
}
catch (Exception e) // file could not be found/read
{
throw new System.IO.FileLoadException($"Could not read file: {e.Message}", file.FullName, e);
throw new System.IO.IOException($"Could not read {nameof(file)}", e);
}

return header;
Expand All @@ -470,20 +462,13 @@ internal static IReadOnlyList<byte> ReadHeaderFromStream(Stream stream, ushort M
{
byte[] header = new byte[MaxHeaderSize];

try // read stream
{
if (!stream.CanRead)
throw new System.IO.IOException("Could not read from Stream");

if (stream.Position > 0)
stream.Seek(0, SeekOrigin.Begin);

stream.Read(header, 0, MaxHeaderSize);
}
catch (Exception e) // file could not be found/read
{
throw new Exception("Could not read Stream : " + e.Message);
}

return header;
}
Expand All @@ -498,32 +483,29 @@ internal static async Task<IReadOnlyList<byte>> ReadHeaderFromStreamAsync(Stream
{
byte[] header = new byte[MaxHeaderSize];

try // read stream
{
if (!stream.CanRead)
throw new System.IO.IOException("Could not read from Stream");
throw new IOException($"Could not read from {nameof(stream)}");

if (stream.Position > 0)
stream.Seek(0, SeekOrigin.Begin);

await stream.ReadAsync(header, 0, MaxHeaderSize);
}
catch (Exception e) // file could not be found/read
{
throw new Exception("Could not read Stream : " + e.Message);
}

return header;
}

internal static IReadOnlyList<byte> ReadHeaderFromByteArray(byte[] byteArray, ushort MaxHeaderSize)
internal static IReadOnlyList<byte> ReadHeaderFromByteArray(IReadOnlyList<byte> byteArray, ushort MaxHeaderSize)
{
if (byteArray.Length < MaxHeaderSize)
throw new ArgumentException($"{nameof(byteArray)}:{byteArray} Is smaller than {nameof(MaxHeaderSize)}:{MaxHeaderSize}", nameof(byteArray));
if (byteArray.Count < MaxHeaderSize)
throw new ArgumentException($"{nameof(byteArray)}:{byteArray.Count} Is smaller than {nameof(MaxHeaderSize)}:{MaxHeaderSize}", nameof(byteArray));

byte[] header = new byte[MaxHeaderSize];

Array.Copy(byteArray, header, MaxHeaderSize);
//Array.Copy(byteArray, header, MaxHeaderSize);
for (int i = 0; i < MaxHeaderSize; i++)
{
header[i] = byteArray[i];
}

return header;
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace MimeDetective.Tests.Documents
{
public class DocumentsTests
public class MsOfficeFormats
{
public const string DocsPath = "./Data/Documents/test.";

Expand All @@ -18,7 +18,7 @@ public async Task FileInfoDocx()
{
var info = new FileInfo(DocsPath + "docx");

var a = System.IO.Directory.GetCurrentDirectory();
var a = Directory.GetCurrentDirectory();

var fileInfo = await info.GetFileTypeAsync();

Expand Down
6 changes: 3 additions & 3 deletions test/Mime-Detective.Tests/Tests/FileType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@

namespace MimeDetective.Tests
{
public class FileTypeTests
public class FileType
{
[Fact]
public void Constructors()
{
var info = new FileType(new byte?[] { 0x12, 0x14, 0x13, 0x15, 0x16 }, "png", "image/png", 4);
var info = new global::MimeDetective.FileType(new byte?[] { 0x12, 0x14, 0x13, 0x15, 0x16 }, "png", "image/png", 4);

Assert.Throws(typeof(ArgumentNullException), () => { var a = new FileType(null, "png", "image/png", 4); });
Assert.Throws(typeof(ArgumentNullException), () => { var a = new global::MimeDetective.FileType(null, "png", "image/png", 4); });
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ namespace MimeDetective.Tests.Images
{
// This project can output the Class library as a NuGet Package.
// To enable this option, right-click on the project and select the Properties menu item. In the Build tab select "Produce outputs on build".
public class IsFile
public class CommonFormats
{
public IsFile()
public CommonFormats()
{
}

Expand Down
Loading

0 comments on commit 9214d28

Please sign in to comment.