silverstripe/mimevalidator

Checks uploaded file content roughly matches a known MIME type for the file extension.

2.0.0 2018-01-26 00:41 UTC

README

Build Status Scrutinizer Code Quality codecov SilverStripe supported module

Introduction

Checks uploaded file content roughly matches a known MIME type for the file extension.

It can be used with FileField or any subclasses like UploadField.

For example, it will fail validation if someone renames a .exe file to .jpg and attempts to upload the file.

Requirements

Note: For a SilverStripe 3.x compatible version, please use the 1.x release line.

Installation via Composer

Install with composer by running composer require silverstripe/mimevalidator in the root of your SilverStripe project.

Configuration

The validator is not used by default. It can be enabled in a couple of ways:

Enable globally

In your mysite/_config/config.yml file:

SilverStripe\Core\Injector\Injector:
  SilverStripe\Assets\Upload_Validator:
    class: SilverStripe\MimeValidator\MimeUploadValidator

Enable on an individual upload field

$field = UploadField::create();
$field->setValidator(MimeUploadValidator::create());

Adding MIME types

By default MIME types are checked against HTTP.MimeTypes config set in framework. This can be limiting as this only allows for one MIME type per extension. To allow for multiple MIME types per extension, you can add these in your YAML config as below:

SilverStripe\MimeValidator\MimeUploadValidator:
  MimeTypes:
    ics:
      - 'text/plain'
      - 'text/calendar'