浏览代码

[backend/file-browser] config for file-download cache-control

There were several complaints of browsers caching the files downloaded
from Hue, even though the files were modified on the server. We tried
using `no-cache` and `must-revalidate` but there were some corner cases
in both.

With this commit, we are letting the admin configure the `Cache-Control`
header based on their needs. The default value is `None` and will let
browsers use their default setting. The config is now available to be
overridden in hue.ini as below.
> [filebrowser]
>   file_download_cache_control=max-age=0, must-revalidate

Steps to verify:
1. Upload a file using file-browser
2. Download the file, with the `Network` tab open in the js console
3. Verify the `Cache-Control` headers on the download. There should be
   nothing with the default configurations.
4. Update the configs to set `file_download_cache_control`
5. Try step-2 again. You should see the configure `Cache-Control` value
   in the headers.
Amit Srivastava 2 年之前
父节点
当前提交
73b9041358

+ 6 - 0
apps/filebrowser/src/filebrowser/conf.py

@@ -76,3 +76,9 @@ REMOTE_STORAGE_HOME = Config(
   type=str,
   type=str,
   default=None,
   default=None,
   help="Optionally set this if you want a different home directory path. e.g. s3a://gethue.")
   help="Optionally set this if you want a different home directory path. e.g. s3a://gethue.")
+
+FILE_DOWNLOAD_CACHE_CONTROL = Config(
+  key="file_download_cache_control",
+  type=str,
+  default=None,
+  help="Optionally set this to control the caching strategy for files download")

+ 3 - 2
apps/filebrowser/src/filebrowser/views.py

@@ -69,7 +69,7 @@ from hadoop.fs.fsutils import do_overwrite_save
 from useradmin.models import User, Group
 from useradmin.models import User, Group
 
 
 from filebrowser.conf import ENABLE_EXTRACT_UPLOADED_ARCHIVE, MAX_SNAPPY_DECOMPRESSION_SIZE,\
 from filebrowser.conf import ENABLE_EXTRACT_UPLOADED_ARCHIVE, MAX_SNAPPY_DECOMPRESSION_SIZE,\
-    SHOW_DOWNLOAD_BUTTON, SHOW_UPLOAD_BUTTON, REDIRECT_DOWNLOAD
+    SHOW_DOWNLOAD_BUTTON, SHOW_UPLOAD_BUTTON, REDIRECT_DOWNLOAD, FILE_DOWNLOAD_CACHE_CONTROL
 from filebrowser.lib.archives import archive_factory
 from filebrowser.lib.archives import archive_factory
 from filebrowser.lib.rwx import filetype, rwx
 from filebrowser.lib.rwx import filetype, rwx
 from filebrowser.lib import xxd
 from filebrowser.lib import xxd
@@ -203,7 +203,8 @@ def download(request, path):
     setattr(response, 'redirect_override', True)
     setattr(response, 'redirect_override', True)
   else:
   else:
     response = StreamingHttpResponse(file_reader(fh), content_type=content_type)
     response = StreamingHttpResponse(file_reader(fh), content_type=content_type)
-    response["Cache-Control"] = 'no-cache' # Browsers must not cache files but always download
+    if FILE_DOWNLOAD_CACHE_CONTROL.get():
+      response["Cache-Control"] = FILE_DOWNLOAD_CACHE_CONTROL.get()
     response["Last-Modified"] = http_date(stats['mtime'])
     response["Last-Modified"] = http_date(stats['mtime'])
     response["Content-Length"] = stats['size']
     response["Content-Length"] = stats['size']
     response['Content-Disposition'] = request.GET.get('disposition', 'attachment; filename="' + stats['name'] + '"') \
     response['Content-Disposition'] = request.GET.get('disposition', 'attachment; filename="' + stats['name'] + '"') \

+ 3 - 0
desktop/conf.dist/hue.ini

@@ -1590,6 +1590,9 @@ submit_to=True
 # Optionally set this if you want a different home directory path. e.g. s3a://gethue.
 # Optionally set this if you want a different home directory path. e.g. s3a://gethue.
 ## remote_storage_home=s3a://gethue
 ## remote_storage_home=s3a://gethue
 
 
+# Optionally set this to control the caching strategy for files download
+## file_download_cache_control=no-store, no-cache, max-age=0, must-revalidate, proxy-revalidate
+
 ###########################################################################
 ###########################################################################
 # Settings to configure Pig
 # Settings to configure Pig
 ###########################################################################
 ###########################################################################

+ 3 - 0
desktop/conf/pseudo-distributed.ini.tmpl

@@ -1573,6 +1573,9 @@
   # Optionally set this if you want a different home directory path. e.g. s3a://gethue.
   # Optionally set this if you want a different home directory path. e.g. s3a://gethue.
   ## remote_storage_home=s3a://gethue
   ## remote_storage_home=s3a://gethue
 
 
+  # Optionally set this to control the caching strategy for files download
+  ## file_download_cache_control=no-store, no-cache, max-age=0, must-revalidate, proxy-revalidate
+
 
 
 ###########################################################################
 ###########################################################################
 # Settings to configure Pig
 # Settings to configure Pig