fix pictures

This commit is contained in:
Thomas Steen Rasmussen 2017-03-30 00:20:14 +02:00
parent c11979083c
commit 9e3ed81731
2 changed files with 15 additions and 9 deletions

View file

@ -80,20 +80,16 @@ class PictureViewMixin(SingleObjectMixin):
# alright, continue with the request
return super().dispatch(request, *args, **kwargs)
def get_picture_response(self):
def get_picture_response(self, path):
if 'runserver' in sys.argv or 'runserver_plus' in sys.argv:
# this is a local devserver situation, guess mimetype from extension and return picture directly
response = HttpResponse(self.picture, content_type=mimetypes.types_map[".%s" % self.picture.name.split(".")[-1]])
else:
# make nginx serve the picture using X-Accel-Redirect
# (this works for nginx only, other webservers use x-sendfile)
# maybe make the header name configurable
# TODO: maybe make the header name configurable
response = HttpResponse()
response['X-Accel-Redirect'] = '/public/speakerproposals/%(campslug)s/%(proposaluuid)s/%(filename)s' % {
'campslug': self.camp.slug,
'proposaluuid': self.get_object().uuid,
'filename': os.path.basename(self.picture.name),
}
response['X-Accel-Redirect'] = path
response['Content-Type'] = ''
return response

View file

@ -86,7 +86,12 @@ class SpeakerProposalPictureView(LoginRequiredMixin, CampViewMixin, EnsureUserOw
raise Http404()
# get and return the response
response = self.get_picture_response()
response = self.get_picture_response('/public/speakerproposals/%(campslug)s/%(proposaluuid)s/%(filename)s' % {
'campslug': self.camp.slug,
'proposaluuid': self.get_object().uuid,
'filename': os.path.basename(self.picture.name),
})
return response
@ -144,7 +149,12 @@ class SpeakerPictureView(CampViewMixin, PictureViewMixin, DetailView):
model = models.Speaker
def get(self, request, *args, **kwargs):
response = self.get_picture_response()
# get and return the response
response = self.get_picture_response(path='/public/speakers/%(campslug)s/%(slug)s/%(filename)s' % {
'campslug': self.camp.slug,
'slug': self.get_object().slug,
'filename': os.path.basename(self.picture.name),
})
return response