""" Functions responsible for returning filenames for the given image generator. Users are free to define their own functions; these are just some some sensible choices. """ from django.conf import settings import os from ..utils import suggest_extension def spec_slug_as_path(generator): """ A namer that, given the following source file name:: /original/bulldog.jpg will generate a relative name like this:: /5ff3233527c5ac3e4b596343b440ff67.jpg which is meant to be added to both /cache/ /manual/ . """ source_filename = getattr(generator.source, 'name', None) spec_slug = getattr(generator, 'slug', None) ext = suggest_extension(source_filename or '', generator.format) if source_filename is None or os.path.isabs(source_filename): # Generally, we put the file right in the cache file directory. dir = settings.IMAGEKIT_CACHEFILE_DIR else: # For source files with relative names (like Django media files), # use the source's name to create the new filename. dir = os.path.join(settings.IMAGEKIT_CACHEFILE_DIR, os.path.splitext(source_filename)[0]) return os.path.normpath(os.path.join(dir, '%s%s' % (generator.get_hash(), ext)))