@@ -406,7 +406,7 @@ def get_flashed_messages(with_categories=False, category_filter=[]):
406
406
407
407
def send_file (filename_or_fp , mimetype = None , as_attachment = False ,
408
408
attachment_filename = None , add_etags = True ,
409
- cache_timeout = 60 * 60 * 12 , conditional = False ):
409
+ cache_timeout = None , conditional = False ):
410
410
"""Sends the contents of a file to the client. This will use the
411
411
most efficient method available and configured. By default it will
412
412
try to use the WSGI server's file_wrapper support. Alternatively
@@ -420,10 +420,6 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
420
420
guessing requires a `filename` or an `attachment_filename` to be
421
421
provided.
422
422
423
- Note `get_send_file_options` in :class:`flask.Flask` hooks the
424
- ``SEND_FILE_MAX_AGE_DEFAULT`` configuration variable to set the default
425
- cache_timeout.
426
-
427
423
Please never pass filenames to this function from user sources without
428
424
checking them first. Something like this is usually sufficient to
429
425
avoid security problems::
@@ -443,6 +439,9 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
443
439
able to, otherwise attach an etag yourself. This functionality
444
440
will be removed in Flask 1.0
445
441
442
+ .. versionchanged:: 0.9
443
+ cache_timeout pulls its default from application config, when None.
444
+
446
445
:param filename_or_fp: the filename of the file to send. This is
447
446
relative to the :attr:`~Flask.root_path` if a
448
447
relative path is specified.
@@ -459,7 +458,11 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
459
458
differs from the file's filename.
460
459
:param add_etags: set to `False` to disable attaching of etags.
461
460
:param conditional: set to `True` to enable conditional responses.
462
- :param cache_timeout: the timeout in seconds for the headers.
461
+
462
+ :param cache_timeout: the timeout in seconds for the headers. When `None`
463
+ (default), this value is set by
464
+ :meth:`~Flask.get_send_file_max_age` of
465
+ :data:`~flask.current_app`.
463
466
"""
464
467
mtime = None
465
468
if isinstance (filename_or_fp , basestring ):
@@ -523,6 +526,8 @@ def send_file(filename_or_fp, mimetype=None, as_attachment=False,
523
526
rv .last_modified = int (mtime )
524
527
525
528
rv .cache_control .public = True
529
+ if cache_timeout is None :
530
+ cache_timeout = current_app .get_send_file_max_age (filename )
526
531
if cache_timeout :
527
532
rv .cache_control .max_age = cache_timeout
528
533
rv .expires = int (time () + cache_timeout )
@@ -757,26 +762,31 @@ def jinja_loader(self):
757
762
return FileSystemLoader (os .path .join (self .root_path ,
758
763
self .template_folder ))
759
764
760
- def get_send_file_options (self , filename ):
761
- """Provides keyword arguments to send to :func:`send_from_directory`.
765
+ def get_send_file_max_age (self , filename ):
766
+ """Provides default cache_timeout for the :func:`send_file` functions.
767
+
768
+ By default, this function returns ``SEND_FILE_MAX_AGE_DEFAULT`` from
769
+ the configuration of :data:`~flask.current_app`.
770
+
771
+ Static file functions such as :func:`send_from_directory` use this
772
+ function, and :func:`send_file` calls this function on
773
+ :data:`~flask.current_app` when the given cache_timeout is `None`. If a
774
+ cache_timeout is given in :func:`send_file`, that timeout is used;
775
+ otherwise, this method is called.
762
776
763
777
This allows subclasses to change the behavior when sending files based
764
778
on the filename. For example, to set the cache timeout for .js files
765
- to 60 seconds (note the options are keywords for :func:`send_file`) ::
779
+ to 60 seconds::
766
780
767
781
class MyFlask(flask.Flask):
768
- def get_send_file_options(self, filename):
769
- options = super(MyFlask, self).get_send_file_options(filename)
770
- if filename.lower().endswith('.js'):
771
- options['cache_timeout'] = 60
772
- options['conditional'] = True
773
- return options
782
+ def get_send_file_max_age(self, name):
783
+ if name.lower().endswith('.js'):
784
+ return 60
785
+ return flask.Flask.get_send_file_max_age(self, name)
774
786
775
787
.. versionadded:: 0.9
776
788
"""
777
- options = {}
778
- options ['cache_timeout' ] = current_app .config ['SEND_FILE_MAX_AGE_DEFAULT' ]
779
- return options
789
+ return current_app .config ['SEND_FILE_MAX_AGE_DEFAULT' ]
780
790
781
791
def send_static_file (self , filename ):
782
792
"""Function used internally to send static files from the static
@@ -786,8 +796,11 @@ def send_static_file(self, filename):
786
796
"""
787
797
if not self .has_static_folder :
788
798
raise RuntimeError ('No static folder for this object' )
799
+ # Ensure get_send_file_max_age is called in all cases.
800
+ # Here, we ensure get_send_file_max_age is called for Blueprints.
801
+ cache_timeout = self .get_send_file_max_age (filename )
789
802
return send_from_directory (self .static_folder , filename ,
790
- ** self . get_send_file_options ( filename ) )
803
+ cache_timeout = cache_timeout )
791
804
792
805
def open_resource (self , resource , mode = 'rb' ):
793
806
"""Opens a resource from the application's resource folder. To see
0 commit comments