From 978733a85d09f12432f6628387f52daca9a44178 Mon Sep 17 00:00:00 2001 From: Luke Burden Date: Thu, 14 Mar 2019 14:56:54 -0700 Subject: [PATCH] Add configurable storage for image uploads --- markdownx/forms.py | 13 ++++++++++--- markdownx/settings.py | 2 ++ markdownx/utils.py | 9 +++++++++ markdownx/views.py | 3 +-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/markdownx/forms.py b/markdownx/forms.py index c52e90d..8d77815 100755 --- a/markdownx/forms.py +++ b/markdownx/forms.py @@ -10,11 +10,12 @@ from django.core.files.uploadedfile import InMemoryUploadedFile # Internal. -from .utils import scale_and_crop, xml_has_javascript +from .utils import import_attribute, scale_and_crop, xml_has_javascript from .exceptions import MarkdownxImageUploadError from .settings import ( MARKDOWNX_IMAGE_MAX_SIZE, + MARKDOWNX_IMAGE_STORAGE, MARKDOWNX_MEDIA_PATH, MARKDOWNX_UPLOAD_CONTENT_TYPES, MARKDOWNX_UPLOAD_MAX_SIZE, @@ -22,6 +23,12 @@ ) +if MARKDOWNX_IMAGE_STORAGE is not None: + image_storage = import_attribute(MARKDOWNX_IMAGE_STORAGE)() +else: + image_storage = default_storage + + class ImageForm(forms.Form): """ Used for the handling of images uploaded using the editor through :guilabel:`AJAX`. @@ -107,8 +114,8 @@ def _save(self, image, file_name, commit): full_path = path.join(MARKDOWNX_MEDIA_PATH, unique_file_name) if commit: - default_storage.save(full_path, image) - return default_storage.url(full_path) + full_path = image_storage.save(full_path, image) + return image_storage.url(full_path) # If `commit is False`, return the path and in-memory image. image_data = namedtuple('image_data', ['path', 'image']) diff --git a/markdownx/settings.py b/markdownx/settings.py index 1b3619a..b0853fb 100755 --- a/markdownx/settings.py +++ b/markdownx/settings.py @@ -68,6 +68,8 @@ def _mdx(var, default): MARKDOWNX_IMAGE_MAX_SIZE = _mdx('IMAGE_MAX_SIZE', dict(size=(IM_WIDTH, IM_HEIGHT), quality=NINETY_DPI)) +MARKDOWNX_IMAGE_STORAGE = _mdx('IMAGE_STORAGE', None) + MARKDOWNX_SVG_JAVASCRIPT_PROTECTION = True diff --git a/markdownx/utils.py b/markdownx/utils.py index a7c691b..cd60b8e 100755 --- a/markdownx/utils.py +++ b/markdownx/utils.py @@ -1,3 +1,5 @@ +import importlib + from markdown import markdown from PIL import Image @@ -181,3 +183,10 @@ def xml_has_javascript(data): # It is (hopefully) safe. return False + + +def import_attribute(path): + assert isinstance(path, str) + pkg, attr = path.rsplit(".", 1) + ret = getattr(importlib.import_module(pkg), attr) + return ret diff --git a/markdownx/views.py b/markdownx/views.py index 33ca64d..507313f 100755 --- a/markdownx/views.py +++ b/markdownx/views.py @@ -34,6 +34,7 @@ class ImageUploadView(BaseFormView): # template_name = "dummy.html" form_class = ImageForm success_url = '/' + http_method_names = 'post' def form_invalid(self, form): """ @@ -72,10 +73,8 @@ def form_valid(self, form): :rtype: django.http.JsonResponse, django.http.HttpResponse """ response = super(ImageUploadView, self).form_valid(form) - if self.request.is_ajax(): image_path = form.save(commit=True) image_code = '![]({})'.format(image_path) return JsonResponse({'image_code': image_code}) - return response