Open jaradc opened 4 years ago
TLDR;
How do I upload to a folder structure like this where the username is the name of my user:
uploads # <-- my mediaroot folder
username1
featured_images
cache
username2
featured_images
cache
I want to put the cache folder under a username within my mediaroot...
You need to parse image source and get path to user directory and then use it as a prefix for your final result:
class SEOThumbnailBackend(ThumbnailBackend):
def _get_thumbnail_filename(self, source, geometry_string, options):
"""
Computes the destination filename.
"""
key = tokey(source.key, geometry_string, serialize(options))
filename, _ext = os.path.splitext(os.path.basename(source.name))
path = '%s/%s' % (key, filename)
# trim filename to get original path to user's folder
user_prefix_path = '/'.join(source.name.split('/')[:-1])
return '%s/%s%s.%s' % (user_prefix_path, settings.THUMBNAIL_PREFIX, path, EXTENSIONS[options['format']])
Thanks, I did something very similar.
I am overriding the
ThumbnailBackend
to be more SEO-friendly (code found on MicroPyramid):The
settings.THUMBNAIL_PREFIX
is the reason files are being uploaded to acache/
directory.First Question The image that the user uploads does go to my
upload_to=
directory, but the ... I guess, cached images go to a folder in my media root named cache. Is this best-practice? Or should the uploaded image and its cached siblings be together?Second Question I want my media root directory to have folders for every user. This means, I'd like to have everything related to that user under one directory. How would I set the current TEMPLATE_PREFIX (
cache/
) to upload inside of a user's username folder in the media root?Ex user with username "jason1" uploads an image. It gets uploaded to either:
or