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

Add check MIME type functions to API #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

yaknikush
Copy link

Added in API of this repository an opportunity to check whether content matches a specific MIME type. For example this is could be used to detect that file isn't malformed or encrypted by ransomware much more faster (if we have a hint by its extension or content type).

Before this PR:

func checkFileIsOK(f *os.File) (ok bool, err error) {
    ext := path.Ext(f.Name())
    mime, err := mimetype.DetectReader(f)
    if err != nil {
        return false, err
    }
    return mime.Extension() == ext, nil
}

Benchmarks:

BenchmarkDetectSliceTar-8    	 5507715	       211 ns/op
BenchmarkDetectSliceZip-8    	 2538682	       474 ns/op
BenchmarkDetectSliceJpeg-8   	14020256	        83.1 ns/op
BenchmarkDetectSliceGif-8    	 6674289	       169 ns/op
BenchmarkDetectSlicePng-8    	14576334	        87.2 ns/op

After this PR:

func checkFileIsOK(f *os.File) (ok bool, err error) {
    ext := path.Ext(f.Name())
    return mimetype.MatchReaderExtension(f, ext)
}

Benchmarks:

BenchmarkMatchSliceTar-8     	33561585	        33.4 ns/op
BenchmarkMatchSliceZip-8     	36062886	        30.8 ns/op
BenchmarkMatchSliceJpeg-8    	63828153	        17.7 ns/op
BenchmarkMatchSliceGif-8     	64112824	        18.2 ns/op
BenchmarkMatchSlicePng-8     	65837989	        17.7 ns/op

So if we have lots of files to check, it's a huge difference.

@yaknikush yaknikush changed the title add Match and MatchExtension funcs Add check MIME type functions to API Sep 14, 2020
@coveralls
Copy link

coveralls commented Sep 14, 2020

Pull Request Test Coverage Report for Build 335

  • 80 of 80 (100.0%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.4%) to 94.906%

Totals Coverage Status
Change from base Build 319: 0.4%
Covered Lines: 1360
Relevant Lines: 1433

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants