Debian Codegraph @

django_downloadview/nginx/middlewares.py

raw
1 import warnings
2
3 from django.conf import settings
4 from django.core.exceptions import ImproperlyConfigured
5
6 from django_downloadview.middlewares import (
7 NoRedirectionMatch,
8 ProxiedDownloadMiddleware,
9 )
10 from django_downloadview.nginx.response import XAccelRedirectResponse
11
12
13 class XAccelRedirectMiddleware(ProxiedDownloadMiddleware):
14 """Configurable middleware, for use in decorators or in global middlewares.
15
16 Standard Django middlewares are configured globally via settings. Instances
17 of this class are to be configured individually. It makes it possible to
18 use this class as the factory in
19 :py:class:`django_downloadview.decorators.DownloadDecorator`.
20
21 """
22
23 def __init__(
24 self,
25 get_response=None,
26 source_dir=None,
27 source_url=None,
28 destination_url=None,
29 expires=None,
30 with_buffering=None,
31 limit_rate=None,
32 media_root=None,
33 media_url=None,
34 ):
35 """Constructor."""
36 if media_url is not None:
37 warnings.warn(
38 "%s ``media_url`` is deprecated. Use "
39 "``destination_url`` instead." % self.__class__.__name__,
40 DeprecationWarning,
41 )
42 if destination_url is None:
43 destination_url = media_url
44 else:
45 destination_url = destination_url
46 else:
47 destination_url = destination_url
48 if media_root is not None:
49 warnings.warn(
50 "%s ``media_root`` is deprecated. Use "
51 "``source_dir`` instead." % self.__class__.__name__,
52 DeprecationWarning,
53 )
54 if source_dir is None:
55 source_dir = media_root
56 else:
57 source_dir = source_dir
58 else:
59 source_dir = source_dir
60
61 super().__init__(get_response, source_dir, source_url, destination_url)
62
63 self.expires = expires
64 self.with_buffering = with_buffering
65 self.limit_rate = limit_rate
66
67 def process_download_response(self, request, response):
68 """Replace DownloadResponse instances by NginxDownloadResponse ones."""
69 try:
70 redirect_url = self.get_redirect_url(response)
71 except NoRedirectionMatch:
72 return response
73 if self.expires:
74 expires = self.expires
75 else:
76 try:
77 expires = response.expires
78 except AttributeError:
79 expires = None
80 return XAccelRedirectResponse(
81 redirect_url=redirect_url,
82 content_type=response["Content-Type"],
83 basename=response.basename,
84 expires=expires,
85 with_buffering=self.with_buffering,
86 limit_rate=self.limit_rate,
87 attachment=response.attachment,
88 headers=response.headers,
89 )
90
91
92 class SingleXAccelRedirectMiddleware(XAccelRedirectMiddleware):
93 """Apply X-Accel-Redirect globally, via Django settings.
94
95 Available settings are:
96
97 NGINX_DOWNLOAD_MIDDLEWARE_SOURCE_URL:
98 The string at the beginning of URLs to replace with
99 ``NGINX_DOWNLOAD_MIDDLEWARE_DESTINATION_URL``.
100 If ``None``, then URLs aren't captured.
101 Defaults to ``settings.MEDIA_URL``.
102
103 NGINX_DOWNLOAD_MIDDLEWARE_SOURCE_DIR:
104 The string at the beginning of filenames (path) to replace with
105 ``NGINX_DOWNLOAD_MIDDLEWARE_DESTINATION_URL``.
106 If ``None``, then filenames aren't captured.
107 Defaults to ``settings.MEDIA_ROOT``.
108
109 NGINX_DOWNLOAD_MIDDLEWARE_DESTINATION_URL:
110 The base URL where requests are proxied to.
111 If ``None`` an ImproperlyConfigured exception is raised.
112
113 .. note::
114
115 The following settings are deprecated since version 1.1.
116 URLs can be used as redirection source since 1.1, and then "MEDIA_ROOT"
117 and "MEDIA_URL" became too confuse.
118
119 NGINX_DOWNLOAD_MIDDLEWARE_MEDIA_ROOT:
120 Replaced by ``NGINX_DOWNLOAD_MIDDLEWARE_SOURCE_DIR``.
121
122 NGINX_DOWNLOAD_MIDDLEWARE_MEDIA_URL:
123 Replaced by ``NGINX_DOWNLOAD_MIDDLEWARE_DESTINATION_URL``.
124
125 """
126
127 def __init__(self, get_response=None):
128 """Use Django settings as configuration."""
129 if settings.NGINX_DOWNLOAD_MIDDLEWARE_DESTINATION_URL is None:
130 raise ImproperlyConfigured(
131 "settings.NGINX_DOWNLOAD_MIDDLEWARE_DESTINATION_URL is "
132 "required by %s middleware" % self.__class__.__name__
133 )
134 super().__init__(
135 get_response=get_response,
136 source_dir=settings.NGINX_DOWNLOAD_MIDDLEWARE_SOURCE_DIR,
137 source_url=settings.NGINX_DOWNLOAD_MIDDLEWARE_SOURCE_URL,
138 destination_url=settings.NGINX_DOWNLOAD_MIDDLEWARE_DESTINATION_URL,
139 expires=settings.NGINX_DOWNLOAD_MIDDLEWARE_EXPIRES,
140 with_buffering=settings.NGINX_DOWNLOAD_MIDDLEWARE_WITH_BUFFERING,
141 limit_rate=settings.NGINX_DOWNLOAD_MIDDLEWARE_LIMIT_RATE,
142 )