mirror of
https://github.com/Hopiu/django-imagekit.git
synced 2026-05-01 18:04:42 +00:00
IKContentFile accepts format hint
This commit is contained in:
parent
b466ff3723
commit
89eb05668e
2 changed files with 13 additions and 8 deletions
|
|
@ -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):
|
||||
|
|
|
|||
|
|
@ -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 '')
|
||||
|
|
|
|||
Loading…
Reference in a new issue