Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload To Media Root Under User's Own Username Folder #641

Open
jaradc opened this issue Jun 17, 2020 · 3 comments
Open

Upload To Media Root Under User's Own Username Folder #641

jaradc opened this issue Jun 17, 2020 · 3 comments

Comments

@jaradc
Copy link

jaradc commented Jun 17, 2020

# models.py
from sorl.thumbnail import ImageField

def featured_images_folder(instance, filename):
    # docs to why this works: https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.FileField.upload_to
    return "{}/featured-images/{}".format(instance.user.username, filename)

class Post(models.Model):
    ...
    featured_image = ImageField(upload_to=featured_images_folder, blank=True)

I am overriding the ThumbnailBackend to be more SEO-friendly (code found on MicroPyramid):

from sorl.thumbnail.conf import settings
from sorl.thumbnail.helpers import tokey, serialize
import os.path


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)
        return '%s%s.%s' % (settings.THUMBNAIL_PREFIX, path, EXTENSIONS[options['format']])
THUMBNAIL_BACKEND = 'myapp.thumbnail.SEOThumbnailBackend'

The settings.THUMBNAIL_PREFIX is the reason files are being uploaded to a cache/ 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?

# settings.py
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'uploads')

Ex user with username "jason1" uploads an image. It gets uploaded to either:

mediaroot / username / featured_images / put stuff here
uploads/jason1/featured_images/{ let sorl thumbnail do what it does here }

or

mediaroot / username / featured_images / cache / put stuff here
uploads/jason1/featured_images/cache/{ let sorl thumbnail do what it does here }
@jaradc
Copy link
Author

jaradc commented Jun 19, 2020

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...

@skiv23
Copy link

skiv23 commented Aug 17, 2020

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']])

image

@jaradc
Copy link
Author

jaradc commented Oct 8, 2020

Thanks, I did something very similar.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants