Skip to content

Add configurable storage for image uploads #156

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions markdownx/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,25 @@
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,
MARKDOWNX_SVG_JAVASCRIPT_PROTECTION
)


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`.
Expand Down Expand Up @@ -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'])
Expand Down
2 changes: 2 additions & 0 deletions markdownx/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
9 changes: 9 additions & 0 deletions markdownx/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib

from markdown import markdown

from PIL import Image
Expand Down Expand Up @@ -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
3 changes: 1 addition & 2 deletions markdownx/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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