From 7d5937ebe64d1f26f103df3040b15588bc4556a4 Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Thu, 19 Apr 2012 21:26:42 -0400 Subject: [PATCH] Rename SpecFile and move it to utils --- imagekit/generators.py | 28 ++-------------------------- imagekit/utils.py | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/imagekit/generators.py b/imagekit/generators.py index a104772..e6cfb2f 100644 --- a/imagekit/generators.py +++ b/imagekit/generators.py @@ -1,36 +1,12 @@ -import mimetypes import os from StringIO import StringIO -from django.core.files.base import ContentFile -from django.utils.encoding import smart_str, smart_unicode - from .processors import ProcessorPipeline, AutoConvert -from .utils import (img_to_fobj, open_image, extension_to_format, +from .utils import (img_to_fobj, open_image, IKContentFile, extension_to_format, UnknownExtensionError) -class SpecFile(ContentFile): - """ - Wraps a ContentFile in a file-like object with a filename - and a content_type. - """ - def __init__(self, filename, content): - 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 - - def __str__(self): - return smart_str(self.file.name or '') - - def __unicode__(self): - return smart_unicode(self.file.name or u'') - - class SpecFileGenerator(object): def __init__(self, processors=None, format=None, options=None, autoconvert=True, storage=None): @@ -72,7 +48,7 @@ class SpecFileGenerator(object): options.items()) imgfile = img_to_fobj(img, format, **options) - content = SpecFile(filename, imgfile.read()) + content = IKContentFile(filename, imgfile.read()) return img, content def generate_file(self, filename, source_file, save=True): diff --git a/imagekit/utils.py b/imagekit/utils.py index 5d21e71..11f4a4d 100644 --- a/imagekit/utils.py +++ b/imagekit/utils.py @@ -1,13 +1,36 @@ import os +import mimetypes import tempfile import types +from django.core.files.base import ContentFile from django.db.models.loading import cache from django.utils.functional import wraps +from django.utils.encoding import smart_str, smart_unicode from imagekit.lib import Image, ImageFile +class IKContentFile(ContentFile): + """ + Wraps a ContentFile in a file-like object with a filename + and a content_type. + """ + def __init__(self, filename, content): + 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 + + def __str__(self): + return smart_str(self.file.name or '') + + def __unicode__(self): + return smart_unicode(self.file.name or u'') + + def img_to_fobj(img, format, **kwargs): tmp = tempfile.TemporaryFile() try: