Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding mimetype .msg #37

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Mime-Detective/Extensions/ByteArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ namespace MimeDetective
{
public static class ByteArrayExtensions
{

public readonly static byte?[] EmptyHeader = new byte?[0];
public readonly static FileType TXT = new FileType(EmptyHeader, "txt", "text/plain");

/// <summary>
/// Read header of bytes and depending on the information in the header
/// return object FileType.
Expand All @@ -17,7 +21,12 @@ public static FileType GetFileType(this byte[] bytes)
int min = bytes.Length > MimeTypes.MaxHeaderSize ? MimeTypes.MaxHeaderSize : bytes.Length;
using (ReadResult readResult = new ReadResult(bytes, min))
{
return MimeAnalyzers.GetFileType(in readResult);

var mimeType = MimeAnalyzers.GetFileType(in readResult);

if (mimeType == null)
return TXT;
return mimeType;
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Mime-Detective/Extensions/FileInfo/DocumentExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ public static bool IsExcel(this FileInfo fileInfo)

return (fileType == MimeTypes.EXCEL) || (fileType == MimeTypes.EXCELX) || (fileType == MimeTypes.MS_OFFICE);
}

public static bool IsOutlookMSG(this FileInfo file) => file.IsType(MimeTypes.OUTLOOK_MSG);
}
}
25 changes: 21 additions & 4 deletions src/Mime-Detective/Extensions/FileInfo/FileInfoExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ namespace MimeDetective
{
public static partial class FileInfoExtensions
{
public readonly static byte?[] EmptyHeader = new byte?[0];
public readonly static FileType TXT = new FileType(EmptyHeader, "txt", "text/plain");

/// <summary>
/// Read header of a file and depending on the information in the header
/// return object FileType.
Expand All @@ -19,7 +22,11 @@ public static FileType GetFileType(this FileInfo file)
{
using (ReadResult readResult = ReadResult.ReadFileHeader(file))
{
return MimeAnalyzers.GetFileType(in readResult);
var mimeType = MimeAnalyzers.GetFileType(in readResult);

if(mimeType == null)
return TXT;
return mimeType;
}
}

Expand All @@ -35,7 +42,11 @@ public static async Task<FileType> GetFileTypeAsync(this FileInfo file)
{
using (ReadResult readResult = await ReadResult.ReadFileHeaderAsync(file))
{
return MimeAnalyzers.GetFileType(in readResult);
var mimeType = MimeAnalyzers.GetFileType(in readResult);

if (mimeType == null)
return TXT;
return mimeType;
}
}

Expand All @@ -51,8 +62,11 @@ public static bool IsFileOfTypes(this FileInfo file, List<FileType> requiredType
{
FileType currentType = file.GetFileType();

if (currentType == null)
currentType = TXT;

//TODO Write a test to check if this null check is correct
if (currentType.Mime == null)
if (currentType?.Mime == null)
return false;

return requiredTypes.Contains(currentType);
Expand Down Expand Up @@ -86,8 +100,11 @@ public static bool IsType(this FileInfo file, FileType type)
{
FileType actualType = GetFileType(file);

if (actualType == null)
actualType = TXT;

//TODO Write a test to check if this null check is correct
if (actualType.Mime is null)
if (actualType?.Mime is null)
return false;

return (actualType.Equals(type));
Expand Down
22 changes: 15 additions & 7 deletions src/Mime-Detective/Mime-Detective.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@
<PropertyGroup>
<Description>Detect MIME type from files.</Description>
<AssemblyTitle>Mime-Detective</AssemblyTitle>
<Authors>Joshua Clark</Authors>
<Authors>Matheus Ortiz</Authors>
<TargetFrameworks>netstandard1.3;net45;netstandard2.0</TargetFrameworks>
<AssemblyName>Mime-Detective</AssemblyName>
<PackageId>Mime-Detective</PackageId>
<PackageId>Mime-Detective-MO</PackageId>
<PackageTags>mime, mime detective, file types</PackageTags>
<LangVersion>latest</LangVersion>
<PackageProjectUrl>https://github.com/clarkis117/Mime-Detective</PackageProjectUrl>
<PackageLicenseUrl>https://github.com/clarkis117/Mime-Detective/blob/master/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/matheusz2/Mime-Detective-MO</PackageProjectUrl>
<PackageLicenseUrl></PackageLicenseUrl>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/clarkis117/Mime-Detective.git</RepositoryUrl>
<RepositoryUrl>https://github.com/matheusz2/Mime-Detective-MO.git</RepositoryUrl>
<AssemblyVersion>0.0.6.0</AssemblyVersion>
<FileVersion>0.0.6.0</FileVersion>
<PackageReleaseNotes>See beta5 PR</PackageReleaseNotes>
<Version>0.0.6.0-beta5</Version>
<PackageReleaseNotes>See beta10 PR</PackageReleaseNotes>
<Version>0.0.6.0-beta10-mo</Version>
<!--Needed to allow usage of fixed sized buffers-->
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net45'">
Expand All @@ -32,5 +33,12 @@
<ItemGroup>
<PackageReference Include="System.Buffers" Version="4.*" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>

</Project>
8 changes: 5 additions & 3 deletions src/Mime-Detective/MimeTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,13 @@ public static class MimeTypes

public readonly static FileType PDF = new FileType(new byte?[] { 0x25, 0x50, 0x44, 0x46 }, "pdf", "application/pdf");

//application/vnd.ms-outlook
public readonly static FileType OUTLOOK_MSG = new FileType(new byte?[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 }, "msg", "application/vnd.ms-outlook");

//todo place holder extension
public readonly static FileType MS_OFFICE = new FileType(new byte?[] { 0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1 }, "doc,ppt,xls", "application/octet-stream");

//application/xml text/xml
// r s i o n = " 1 . 0 " ? >
public readonly static FileType XML = new FileType(new byte?[] { 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3D, 0x22, 0x31, 0x2E, 0x30, 0x22, 0x3F, 0x3E }, "xml", "text/xml");

// XML file encoded with UTF-8 < ? x m l (spc)
Expand Down Expand Up @@ -223,7 +225,7 @@ EML is also used by Outlook Express and QuickMail.
//EVTX Windows Vista event log file
public readonly static FileType ELF = new FileType(new byte?[] { 0x45, 0x6C, 0x66, 0x46, 0x69, 0x6C, 0x65, 0x00 }, "elf", "text/plain");

public static readonly FileType[] Types = new FileType[] { PDF, JPEG, ZIP, ZIP_EMPTY, RAR, RTF, PNG, GIF, DLL_EXE, MS_OFFICE,
public static readonly FileType[] Types = new FileType[] { PDF, JPEG, ZIP, ZIP_EMPTY, RAR, RTF, PNG, GIF, DLL_EXE, MS_OFFICE, OUTLOOK_MSG,
BMP, DLL_EXE, ZIP_7z, GZ_TGZ, TAR_ZH, TAR_ZV, OGG, ICO, XML, XML_NoBom, XML_Utf8Bom, XML_UCS2BE, XML_UCS2LE, DWG, LIB_COFF, PST, PSD, BZ2,
AES, SKR, SKR_2, PKR, EML_FROM, ELF, TXT_UTF8, TXT_UTF16_BE, TXT_UTF16_LE, TXT_UTF32_BE, TXT_UTF32_LE,
Mp3ID3, Wav, Flac, MIDI,
Expand Down
Binary file not shown.
Binary file not shown.
8 changes: 8 additions & 0 deletions test/Mime-Detective.Tests/Tests/Documents/MsOfficeFormats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,13 @@ public async Task IsExcelX(string filePath)

await AssertIsType(info, MimeTypes.EXCELX);
}

[Theory]
[InlineData("Message.msg")]
public async Task IsMSG(string fileName)
{
var info = GetFileInfo(DocsPath, fileName);
await AssertIsType(info, MimeTypes.OUTLOOK_MSG);
}
}
}