1 # -*- coding: utf-8 -*-
3 # This file is part of MIL/PEER, licensed under GNU Affero GPLv3 or later.
4 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
8 from urllib import quote
9 from django.conf import settings
10 from django.http import HttpResponse, Http404
11 from django.utils.decorators import method_decorator
12 from django.views.decorators.vary import vary_on_headers
13 from django.views.generic import FormView
14 from .forms import UploadForm
17 # Use sorl.thumbnail if available.
19 from sorl.thumbnail import default
21 def thumbnail(relpath):
22 return settings.MEDIA_URL + relpath
24 def thumbnail(relpath):
26 return default.backend.get_thumbnail(relpath, "x50").url
28 # That's not an image. No thumb.
32 class JSONResponse(HttpResponse):
33 """JSON response class."""
34 def __init__(self, obj='', mimetype="application/json", *args, **kwargs):
35 content = json.dumps(obj)
36 super(JSONResponse, self).__init__(content, mimetype, *args, **kwargs)
39 class UploadViewMixin(object):
40 def get_safe_path(self, filename=""):
41 """Finds absolute filesystem path of the browsed dir of file.
43 Makes sure it's inside MEDIA_ROOT.
46 path = os.path.abspath(os.path.join(
50 if not path.startswith(os.path.abspath(settings.MEDIA_ROOT)):
53 if not path.startswith(self.get_safe_path()):
58 class UploadView(UploadViewMixin, FormView):
59 template_name = "fileupload/picture_form.html"
60 form_class = UploadForm
62 def get_object(self, request, *args, **kwargs):
63 """Get any data for later use."""
66 def get_directory(self):
67 """Directory relative to MEDIA_ROOT. Must end with a slash."""
68 return self.kwargs['path']
70 def breadcrumbs(self):
71 """List of tuples (name, url) or just (name,) for breadcrumbs.
73 Probably only the last item (representing currently browsed dir)
77 directory = self.get_directory()
78 now_path = os.path.dirname(self.request.get_full_path())
79 directory = os.path.dirname(directory)
82 (os.path.basename(directory),)
84 directory = os.path.dirname(directory)
85 now_path = (os.path.dirname(now_path))
87 crumbs.insert(0, (os.path.basename(directory), now_path + '/'))
88 directory = os.path.dirname(directory)
89 now_path = os.path.dirname(now_path)
90 crumbs.insert(0, ('media', now_path))
95 def get_url(self, filename):
96 """Finds URL of a file in browsed dir."""
97 return settings.MEDIA_URL + self.get_directory() + quote(filename.encode('utf-8'))
99 @method_decorator(vary_on_headers('Accept'))
100 def dispatch(self, request, *args, **kwargs):
101 self.object = self.get_object(request, *args, **kwargs)
102 return super(UploadView, self).dispatch(request, *args, **kwargs)
104 def get(self, request, *args, **kwargs):
105 if request.is_ajax():
107 path = self.get_safe_path()
108 if os.path.isdir(path):
109 for f in sorted(os.listdir(path)):
113 if os.path.isdir(os.path.join(path, f)):
115 "url": "%s%s/" % (request.get_full_path(), f),
119 "url": self.get_url(f),
120 'thumbnail_url': thumbnail(self.get_directory() + f),
121 'delete_url': "%s?file=%s" % (
122 request.get_full_path(),
123 quote(f.encode('utf-8'))),
124 'delete_type': "DELETE"
126 files.append(file_info)
127 return JSONResponse(files)
129 return super(UploadView, self).get(request, *args, **kwargs)
131 def form_valid(self, form):
132 flist = self.request.FILES.getlist('files')
133 path = self.get_safe_path()
134 if not os.path.isdir(path):
138 with open(self.get_safe_path(f.name), 'w') as destination:
139 for chunk in f.chunks():
140 destination.write(chunk)
143 'url': self.get_url(f.name),
144 'thumbnail_url': thumbnail(self.get_directory() + f.name),
145 'delete_url': "%s?file=%s" % (
146 self.request.get_full_path(),
147 quote(f.name.encode('utf-8'))),
148 'delete_type': "DELETE"
150 response = JSONResponse(data)
151 response['Content-Disposition'] = 'inline; filename=files.json'
154 def delete(self, request, *args, **kwargs):
155 os.unlink(self.get_safe_path(request.GET.get('file')))
156 response = JSONResponse(True)
157 response['Content-Disposition'] = 'inline; filename=files.json'