|
6 | 6 | from sys import version_info as python_version
|
7 | 7 | from django.utils.timezone import now
|
8 | 8 | from lxml import etree
|
| 9 | +import mimetypes |
| 10 | +import urllib |
9 | 11 |
|
10 | 12 | from django.http import HttpResponseForbidden, HttpResponseNotAllowed, HttpResponseBadRequest, \
|
11 |
| - HttpResponseNotModified, HttpResponseRedirect, Http404 |
| 13 | + HttpResponseNotModified, HttpResponseRedirect, Http404, HttpResponse |
12 | 14 | from django.utils.decorators import method_decorator
|
13 | 15 | from django.utils.functional import cached_property
|
14 |
| -from django.utils.http import parse_etags |
| 16 | +from django.utils.http import parse_etags, http_date |
15 | 17 | from django.shortcuts import render_to_response
|
16 | 18 | from django.views.decorators.csrf import csrf_exempt
|
17 | 19 | from django.views.generic import View
|
18 | 20 |
|
19 |
| -from djangodav.responses import ResponseException, HttpResponsePreconditionFailed, HttpResponseCreated, HttpResponseNoContent, \ |
| 21 | +from djangodav.responses import HttpResponsePreconditionFailed, HttpResponseCreated, HttpResponseNoContent, \ |
20 | 22 | HttpResponseConflict, HttpResponseMediatypeNotSupported, HttpResponseBadGateway, \
|
21 |
| - HttpResponseMultiStatus, HttpResponseLocked, HttpResponse |
| 23 | + HttpResponseMultiStatus, HttpResponseLocked, RedirectFSException, ResponseException, AsSendFileFS |
22 | 24 | from djangodav.utils import WEBDAV_NSMAP, D, url_join, get_property_tag_list, rfc1123_date
|
23 | 25 | from djangodav import VERSION as djangodav_version
|
24 | 26 | from django import VERSION as django_version, get_version
|
25 | 27 |
|
| 28 | +from sendfile import sendfile |
| 29 | + |
26 | 30 | PATTERN_IF_DELIMITER = re.compile(r'(<([^>]+)>)|(\(([^\)]+)\))')
|
27 | 31 |
|
28 | 32 | class DavView(View):
|
@@ -190,7 +194,24 @@ def get(self, request, path, head=False, *args, **kwargs):
|
190 | 194 | response['ETag'] = self.resource.getetag
|
191 | 195 | if not head:
|
192 | 196 | response['Content-Length'] = self.resource.getcontentlength
|
193 |
| - response.content = self.resource.read() |
| 197 | + try: |
| 198 | + response.content = self.resource.read() |
| 199 | + except AsSendFileFS: |
| 200 | + assert sendfile is not None, "django-sendfile is not installed." |
| 201 | + full_path = self.resource.get_abs_path().encode('utf-8') |
| 202 | + if self.resource.quote: |
| 203 | + full_path = urllib.quote(full_path) |
| 204 | + response = sendfile(request, full_path) |
| 205 | + return response |
| 206 | + except RedirectFSException: |
| 207 | + response = HttpResponse() |
| 208 | + response['X-Accel-Redirect'] = url_join(self.resource.prefix, self.resource.get_path().encode('utf-8')) |
| 209 | + response['X-Accel-Charset'] = 'utf-8' |
| 210 | + response['Content-Type'] = mimetypes.guess_type(self.resource.displayname) |
| 211 | + response['Content-Length'] = self.resource.getcontentlength |
| 212 | + response['Last-Modified'] = http_date(self.resource.getlastmodified) |
| 213 | + response['ETag'] = self.resource.getetag |
| 214 | + raise ResponseException(response) |
194 | 215 | elif not head:
|
195 | 216 | response = render_to_response(self.template_name, dict(resource=self.resource, base_url=self.base_url))
|
196 | 217 | response['Last-Modified'] = self.resource.getlastmodified
|
|
0 commit comments