$.post({url: '/wiki/ajax/image/', processData: false, data: params, contentType: false}).success(function(data) {
var img = document.createElement('IMG');
img.src = data.url;
+ if (data.orig_url) {
+ img.setAttribute('data-orig-url', data.orig_url);
+ }
$(window.active_figure).empty().append(img);
});
}
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.edit import FormView
+from sorl.thumbnail.shortcuts import get_thumbnail
+
from combo.data.models import Page, TextCell
from .forms import NewPageForm
@csrf_exempt
def ajax_image(request, *args, **kwargs):
img = request.FILES['image']
- # TODO: resize if necessary, and store both original and resized images
saved_path = default_storage.save('wiki/images/%s' % img.name, img)
- return JsonResponse({'url': '/media/' + saved_path})
+ url = '/media/' + saved_path
+ response = {'url': url}
+ if default_storage.size(saved_path) > 100_000 and not img.name.endswith('.svg'):
+ response['orig_url'] = url
+ response['url'] = get_thumbnail(saved_path, '1000', upscale=False).url
+ return JsonResponse(response)