diff --git a/.env.template b/.env.template index 13f15e044..09bd6f74b 100644 --- a/.env.template +++ b/.env.template @@ -58,6 +58,7 @@ GUNICORN_MEDIA=0 # S3_SECRET_ACCESS_KEY= # S3_BUCKET_NAME= # S3_QUERYSTRING_AUTH=1 # default true, set to 0 to serve media from a public bucket without signed urls +# AWS_QUERYSTRING_EXPIRE=3600 # number of seconds querystring are valid for # S3_ENDPOINT_URL= # when using a custom endpoint like minio # Email Settings, see https://docs.djangoproject.com/en/3.2/ref/settings/#email-host diff --git a/cookbook/helper/CustomStorageClass.py b/cookbook/helper/CustomStorageClass.py new file mode 100644 index 000000000..b20089397 --- /dev/null +++ b/cookbook/helper/CustomStorageClass.py @@ -0,0 +1,19 @@ +import hashlib + +from django.conf import settings +from django.core.cache import cache +from storages.backends.s3boto3 import S3Boto3Storage + + +class CachedS3Boto3Storage(S3Boto3Storage): + def url(self, name, **kwargs): + key = hashlib.md5(f'recipes_media_urls_{name}'.encode('utf-8')).hexdigest() + if result := cache.get(key): + return result + + result = super(CachedS3Boto3Storage, self).url(name, **kwargs) + + timeout = int(settings.AWS_QUERYSTRING_EXPIRE * .95) + cache.set(key, result, timeout) + + return result diff --git a/cookbook/helper/recipe_search.py b/cookbook/helper/recipe_search.py index d5bb2fb78..5f6d4199d 100644 --- a/cookbook/helper/recipe_search.py +++ b/cookbook/helper/recipe_search.py @@ -27,7 +27,7 @@ def search_recipes(request, queryset, params): last_viewed_recipes = ViewLog.objects.filter(created_by=request.user, space=request.space, created_at__gte=datetime.now() - timedelta(days=14)).order_by('pk').values_list('recipe__pk', flat=True).distinct() - return queryset.filter(pk__in=last_viewed_recipes[len(last_viewed_recipes)-search_last_viewed:]) + return queryset.filter(pk__in=last_viewed_recipes[len(last_viewed_recipes) - min(len(last_viewed_recipes), search_last_viewed):]) queryset = queryset.annotate( new_recipe=Case(When(created_at__gte=(datetime.now() - timedelta(days=7)), then=Value(100)), diff --git a/recipes/settings.py b/recipes/settings.py index 023b11d7c..121363b01 100644 --- a/recipes/settings.py +++ b/recipes/settings.py @@ -300,12 +300,13 @@ STATIC_URL = os.getenv('STATIC_URL', '/static/') STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles") if os.getenv('S3_ACCESS_KEY', ''): - DEFAULT_FILE_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' + DEFAULT_FILE_STORAGE = 'cookbook.helper.CustomStorageClass.CachedS3Boto3Storage' AWS_ACCESS_KEY_ID = os.getenv('S3_ACCESS_KEY', '') AWS_SECRET_ACCESS_KEY = os.getenv('S3_SECRET_ACCESS_KEY', '') AWS_STORAGE_BUCKET_NAME = os.getenv('S3_BUCKET_NAME', '') AWS_QUERYSTRING_AUTH = bool(int(os.getenv('S3_QUERYSTRING_AUTH', True))) + AWS_QUERYSTRING_EXPIRE = int(os.getenv('S3_QUERYSTRING_EXPIRE', 3600)) if os.getenv('S3_ENDPOINT_URL', ''): AWS_S3_ENDPOINT_URL = os.getenv('S3_ENDPOINT_URL', '')