from django.core.exceptions import ValidationError
import magic


class MimetypeValidator(object):
	def __init__(self, mimetypes):
		self.mimetypes = mimetypes
	
	def __call__(self, value):
		mime = magic.from_buffer(value.read(1024), mime=True)
		if not mime in self.mimetypes:
			raise ValidationError('%s is not an acceptable file type' % value)

from django.core.exceptions import ValidationError
import magic


class MimetypeValidator(object):
	def __init__(self, mimetypes):
		self.mimetypes = mimetypes
	
	def __call__(self, value):
		try:
			mime = magic.from_buffer(value.read(1024), mime=True)
			if not mime in self.mimetypes:
				raise ValidationError('%s is not an acceptable file type' % value)
		except AttributeError as e:
			raise ValidationError('This value could not be validated for file type' % value)

Make mimetype validator slightly more robust
Make mimetype validator slightly more robust

  from django.core.exceptions import ValidationError
  import magic
  
  
  class MimetypeValidator(object):
  	def __init__(self, mimetypes):
  		self.mimetypes = mimetypes
  	
  	def __call__(self, value):
+ 		try:
- 		mime = magic.from_buffer(value.read(1024), mime=True)
+ 			mime = magic.from_buffer(value.read(1024), mime=True)
- 		if not mime in self.mimetypes:
+ 			if not mime in self.mimetypes:
- 			raise ValidationError('%s is not an acceptable file type' % value)
+ 				raise ValidationError('%s is not an acceptable file type' % value)
+ 		except AttributeError as e:
+ 			raise ValidationError('This value could not be validated for file type' % value)
  
Make mimetype validator slightly more robust
## Code Before:
from django.core.exceptions import ValidationError
import magic


class MimetypeValidator(object):
	def __init__(self, mimetypes):
		self.mimetypes = mimetypes
	
	def __call__(self, value):
		mime = magic.from_buffer(value.read(1024), mime=True)
		if not mime in self.mimetypes:
			raise ValidationError('%s is not an acceptable file type' % value)

## Instruction:
Make mimetype validator slightly more robust
## Code After:
from django.core.exceptions import ValidationError
import magic


class MimetypeValidator(object):
	def __init__(self, mimetypes):
		self.mimetypes = mimetypes
	
	def __call__(self, value):
		try:
			mime = magic.from_buffer(value.read(1024), mime=True)
			if not mime in self.mimetypes:
				raise ValidationError('%s is not an acceptable file type' % value)
		except AttributeError as e:
			raise ValidationError('This value could not be validated for file type' % value)
