]> git.0d.be Git - panikdb.git/commitdiff
wiki: automatically resize uploaded images
authorFrédéric Péters <fpeters@0d.be>
Thu, 11 Jun 2020 18:12:59 +0000 (20:12 +0200)
committerFrédéric Péters <fpeters@0d.be>
Thu, 11 Jun 2020 18:12:59 +0000 (20:12 +0200)
panikdb/static/js/combo.wiki.js
panikdb/wiki/views.py

index c80e4a177e57693df674f5a352983e7f00a44f85..d8f75f297076d6bcac9510e30da4175a74650a1e 100644 (file)
@@ -109,6 +109,9 @@ function auto_anchors() {
       $.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);
       });
     }
index 43cd686e54a3560a139fd1e4ee4f2b09f8059308..0622e9cf638ad96620c6a098ae076138274f0dd2 100644 (file)
@@ -6,6 +6,8 @@ from django.utils.text import slugify
 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
@@ -55,6 +57,10 @@ def ajax_new_page(request, *args, **kwargs):
 @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)