Rename SpecFile and move it to utils

This commit is contained in:
Matthew Tretter 2012-04-19 21:26:42 -04:00
parent 8044b97a33
commit 7d5937ebe6
2 changed files with 25 additions and 26 deletions

View file

@ -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):

View file

@ -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: