]> git.0d.be Git - chloro.git/blobdiff - chloro/phyll/views.py
misc: add image/file upload for live edit (sync with panikdb)
[chloro.git] / chloro / phyll / views.py
index a03c93fe4beda075a88b04caa98d15bf30179d0b..2e77fc926842270e743d37c323eb62ef2d470b12 100644 (file)
 # You should have received a copy of the GNU Affero General Public License
 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
+import os
 import urllib.parse
 
 from django.conf import settings
 from django.contrib.syndication.views import Feed
 from django.core.exceptions import PermissionDenied
-from django.http import HttpResponse, Http404
+from django.core.files.storage import default_storage
+from django.http import HttpResponse, Http404, JsonResponse
 from django.utils.feedgenerator import Atom1Feed
 from django.views import View
+from django.views.decorators.csrf import csrf_exempt
 from django.views.generic import CreateView, DeleteView, DetailView, ListView, UpdateView, TemplateView
 
+from sorl.thumbnail.shortcuts import get_thumbnail
+
 from .models import Note
 
 
@@ -144,3 +149,22 @@ class AtomFeed(Feed):
 
     def item_pubdate(self, item):
         return item.creation_timestamp
+
+
+@csrf_exempt
+def ajax_upload(request, *args, **kwargs):
+    upload = request.FILES['upload']
+    upload_path = 'uploads'
+    if os.path.splitext(upload.name.lower())[-1] in ('.jpg', '.jpeg', '.png', '.gif', '.svg'):
+        upload_path = 'images'
+    saved_path = default_storage.save('%s/%s' % (upload_path, upload.name), upload)
+    url = '/media/' + saved_path
+    response = {'url': url, 'filename': upload.name}
+    if upload_path == 'images':
+        if default_storage.size(saved_path) > 500_000 and not upload.name.endswith('.svg'):
+            response['orig_url'] = url
+            try:
+                response['url'] = get_thumbnail(saved_path, '1000', upscale=False).url
+            except OSError:
+                pass
+    return JsonResponse(response)