Re-added support for folder with static files, refactored static file sending
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent b710986 · Browse files · 6 files·+36 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+36 added−10 removed
- tests:+3 added−0 removed
- other:+33 added−10 removed
Provenance
- Commit
ac13def- Tree
a29ca33- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
b710986
6 files changed+36 added−10 removed
modified:flask/__init__.py+2 added-1 removed
16 unmodified lines17 18 from .app import Flask, Request, Response19 from .config import Config20 from .helpers import url_for, jsonify, json_available, flash, \21- send_file, get_flashed_messages, get_template_attribute21+ send_file, send_from_directory, get_flashed_messages, \22+ get_template_attribute23 from .globals import current_app, g, request, session, _request_ctx_stack24 from .module import Module25 from .templating import render_template, render_template_string26 3 unmodified linesmodified:flask/app.py+1 added-1 removed
271 unmodified lines272 self.url_map = Map()273 274 # if there is a static folder, register it for the application.275 if self.has_static_folder:276- self.add_url_rule(self.static_path + '/<filename>',276+ self.add_url_rule(self.static_path + '/<path:filename>',277 endpoint='static',278 view_func=self.send_static_file)279 280 #: The Jinja2 environment. It is created from the565 unmodified linesmodified:flask/helpers.py+29 added-7 removed
289 unmodified lines290 rv.headers.pop('x-sendfile', None)291 return rv292 293 294+def send_from_directory(directory, filename, **options):295+ """Send a file from a given directory with :func:`send_file`. This296+ is a secure way to quickly expose static files from an upload folder297+ or something similar.298+299+ Example usage::300+301+ @app.route('/uploads/<path:filename>')302+ def download_file(filename):303+ return send_from_directory(app.config['UPLOAD_FOLDER'],304+ filename, as_attachment=True)305+306+ :param directory: the directory where all the files are stored.307+ :param filename: the filename relative to that directory to308+ download.309+ :param options: optional keyword arguments that are directly310+ forwarded to :func:`send_file`.311+ """312+ filename = posixpath.normpath(filename)313+ if filename.startswith(('/', '../')):314+ raise NotFound()315+ filename = os.path.join(directory, filename)316+ if not os.path.isfile(filename):317+ raise NotFound()318+ return send_file(filename, conditional=True, **options)319+320+321 def _get_package_path(name):322 """Returns the path to a package or cwd if that cannot be found."""323 try:324 return os.path.abspath(os.path.dirname(sys.modules[name].__file__))35 unmodified lines360 folder to the browser.361 362 .. versionadded:: 0.5363 """337- filename = posixpath.normpath(filename)338- if filename.startswith(('/', '../')):339- raise NotFound()340- filename = os.path.join(self.root_path, 'static', filename)341- if not os.path.isfile(filename):342- raise NotFound()343- return send_file(filename, conditional=True)364+ return send_from_directory(os.path.join(self.root_path, 'static'),365+ filename)366 367 def open_resource(self, resource):368 """Opens a resource from the application's resource folder. To see369 how this works, consider the following folder structure::20 unmodified linesmodified:flask/module.py+1 added-1 removed
27 unmodified lines28 if path is None:29 path = state.app.static_path30 if state.url_prefix:31 path = state.url_prefix + path32- state.app.add_url_rule(path + '/<filename>',32+ state.app.add_url_rule(path + '/<path:filename>',33 '%s.static' % module.name,34 view_func=module.send_static_file)35 return _register36 166 unmodified linesadded:tests/moduleapp/apps/admin/static/css/test.css+1 added
1/* nested file */modified:tests/flask_tests.py+2 added
639 unmodified lines640 rv = c.get('/admin/')641 assert rv.data == 'Hello from the Admin'642 rv = c.get('/admin/static/test.txt')643 assert rv.data.strip() == 'Admin File'644+ rv = c.get('/admin/static/css/test.css')645+ assert rv.data.strip() == '/* nested file */'646 647 with app.test_request_context():648 assert flask.url_for('admin.static', filename='test.txt') \649 == '/admin/static/test.txt'285 unmodified lines