IKContentFile accepts format hint

This commit is contained in:
Matthew Tretter 2012-04-20 23:30:30 -04:00
parent b466ff3723
commit 89eb05668e
2 changed files with 13 additions and 8 deletions

View file

@ -40,7 +40,7 @@ class SpecFileGenerator(object):
format = format or img.format or original_format or 'JPEG'
imgfile = img_to_fobj(img, format, **options)
content = IKContentFile(filename, imgfile.read())
content = IKContentFile(filename, imgfile.read(), format=format)
return img, content
def generate_file(self, filename, source_file, save=True):

View file

@ -18,16 +18,21 @@ PALETTE_TRANSPARENCY_FORMATS = ['PNG', 'GIF']
class IKContentFile(ContentFile):
"""
Wraps a ContentFile in a file-like object with a filename
and a content_type.
Wraps a ContentFile in a file-like object with a filename and a
content_type. A PIL image format can be optionally be provided as a content
type hint.
"""
def __init__(self, filename, content):
def __init__(self, filename, content, format=None):
self.file = ContentFile(content)
self.file.name = filename
try:
self.file.content_type = mimetypes.guess_type(filename)[0]
except IndexError:
self.file.content_type = None
mimetype = getattr(self.file, 'content_type', None)
if format and not mimetype:
mimetype = format_to_mimetype(format)
if not mimetype:
ext = os.path.splitext(filename or '')[1]
mimetype = extension_to_mimetype(ext)
self.file.content_type = mimetype
def __str__(self):
return smart_str(self.file.name or '')