Skip to content
Myr

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

6 files+36 added−10 removed

modified:flask/__init__.py+2 added-1 removed
16 unmodified lines
1718from .app import Flask, Request, Response19from .config import Config20from .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_attribute23from .globals import current_app, g, request, session, _request_ctx_stack24from .module import Module25from .templating import render_template, render_template_string26
3 unmodified lines
modified:flask/app.py+1 added-1 removed
271 unmodified lines
272 self.url_map = Map()273274 # 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)279280 #: The Jinja2 environment. It is created from the
565 unmodified lines
modified:flask/helpers.py+29 added-7 removed
289 unmodified lines
290 rv.headers.pop('x-sendfile', None)291 return rv292293294def 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.298299 Example usage::300301 @app.route('/uploads/<path:filename>')302 def download_file(filename):303 return send_from_directory(app.config['UPLOAD_FOLDER'],304 filename, as_attachment=True)305306 :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)319320321def _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 lines
360 folder to the browser.361362 .. 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)366367 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 lines
modified:flask/module.py+1 added-1 removed
27 unmodified lines
28 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 lines
added:tests/moduleapp/apps/admin/static/css/test.css+1 added
modified:tests/flask_tests.py+2 added
639 unmodified lines
640 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 */'646647 with app.test_request_context():648 assert flask.url_for('admin.static', filename='test.txt') \649 == '/admin/static/test.txt'
285 unmodified lines