Coder Social home page Coder Social logo

stellarbear / mimemagicsharp Goto Github PK

View Code? Open in Web Editor NEW
6.0 1.0 3.0 667 KB

Detects MIME type based on file content or file extension

License: MIT License

C# 100.00%
mime mime-type mime-parser csharp net magic mime-detector mime-database json detection

mimemagicsharp's Introduction

MimeMagicSharp

Detects MIME type based on file content or file extension. Nuget package is available. Sample files are included.

Description

This project was originally based on parsing /usr/share/mime/magic (Kali Linux) file (mime database). Location may differ. More information about this file structure could be found here ("the magic files" section).

This file contains mime type definitions with several rule sets, which are applied iteratively to the given file's content to guess mime type(s).

Original file's format (old) is hard to modify according to your needs. I decided to move on to json file format (new) with similary capabilities and some new features. New format could be easily modified and updated with new rule sets and mime type definitions.

Both database formats are supported. Conversion from the old to the new database format is supported. Detection by extension can be used only with new file format. Template original and json files will be included soon.

Structure

Each MIME definition contains a list of rule sets. Each rule set contains an hierarchical list of rules. Rule set pseudocode:

//  Level 0 rules: Rule 0
//  Level 1 rules: Rule 1, Rule 2, Rule 3
//  Level 2 rules: Rule 4
//  Level 3 rules: Rule 5, Rule 6
//	...

When this ruleset is applied, result will be calculated in the following manner:

//  Result (boolean) = 0 && (1 || 2 || 3) && 4 && (5 || 6)...

Rule example:

"Name": "application/x-deb",
"Description": "Debian package",
"Extensions": ["deb"],
"RuleSet": [
    {
    "Rule": [
        {
        "Level": 0,
        "Offset": 0,
        "Range": 0,
        "Data": "213C617263683E",
        "DataUTF8": "!<arch>"
        },
        {
        "Level": 1,
        "Offset": 8,
        "Range": 0,
        "Data": "64656269616E",
        "DataUTF8": "debian"
        }
    ]
    }, "Rule":[...
    ]...
  • Level. Explained earlier.
  • Offset. Seek start position
  • Range. Seek interval (0 means fixed position)
  • Data. Data to seek in hex format
  • Description and DataUTF8 are for visual purposes only.

Usage

//	Database File (new or old)
string MagicFile = Path.Combine(Environment.CurrentDirectory, "magic");

using (MimeMagicSharp.MimeMagicSharp ms = new MimeMagicSharp.MimeMagicSharp(MimeMagicSharp.EMagicFileType.Json, MagicFile))
{
   try
   {
	foreach (MimeTypeGuess mimeTypeGuess in ms.AssumeMimeType(EMimeTypeBy.Content,
                    	Path.Combine(Environment.CurrentDirectory, "Newtonsoft.Json.xml")))
	{
		//  Iterate over results
	}
    }
    catch (Exception Ex)
    { 
       //   Handle errors
    }
}

Convertion from original (old) format to json (new) is supported

MimeMagicSharp.MimeMagicSharp.ConvertFromOriginalToJson("magic_original", "magic_json_convert_test", out string ConvertError);

Limitations

Only first 4096 bytes of file are read during mime detection procedure. You can extend this limit in source code easily.

Other

Build in vs 2017

You can use or modify the sources however you want

mimemagicsharp's People

Contributors

hobbes1987 avatar stellarbear avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.