Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/adi-/django-markdownx
Browse files Browse the repository at this point in the history
  • Loading branch information
adi committed Jun 18, 2016
2 parents daa8d63 + fc8c27f commit 89491f4
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions markdownx/forms.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import os
import uuid


from django import forms
from django.conf import settings
from django.utils.six import BytesIO
from django.core.files.storage import default_storage
from django.utils.translation import ugettext_lazy as _
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.template import defaultfilters as filters
Expand All @@ -25,23 +24,17 @@ class ImageForm(forms.Form):
def save(self, commit=True):
img = scale_and_crop(self.files['image'], **MARKDOWNX_IMAGE_MAX_SIZE)
thumb_io = BytesIO()
img.save(thumb_io, self.files['image'].content_type.split('/')[-1].upper())
img.save(thumb_io, self.files['image'].content_type.split('/')[-1].upper())

file_name = str(self.files['image'])
thumb_io.seek(0, os.SEEK_END)
img = InMemoryUploadedFile(thumb_io, "image", file_name, self.files['image'].content_type, thumb_io.tell(), None)

unique_file_name = self.get_unique_file_name(file_name)
full_path = os.path.join(settings.MEDIA_ROOT, MARKDOWNX_MEDIA_PATH, unique_file_name)
if not os.path.exists(os.path.dirname(full_path)):
os.makedirs(os.path.dirname(full_path))

destination = open(full_path, 'wb+')
for chunk in img.chunks():
destination.write(chunk)
destination.close()
full_path = os.path.join(MARKDOWNX_MEDIA_PATH, unique_file_name)
default_storage.save(full_path, img)

return os.path.join(settings.MEDIA_URL, MARKDOWNX_MEDIA_PATH, unique_file_name)
return default_storage.url(full_path)

def get_unique_file_name(instance, filename):
ext = filename.split('.')[-1]
Expand Down

0 comments on commit 89491f4

Please sign in to comment.