Add File server CORS support

This commit is contained in:
allegroai 2019-07-17 18:16:43 +03:00
parent 02671910b2
commit bed714890d
6 changed files with 24 additions and 4 deletions

View File

@ -0,0 +1,8 @@
download {
# Add response headers requesting no caching for served files
disable_browser_caching: false
}
cors {
origins: "*"
}

View File

@ -1,16 +1,18 @@
""" A Simple file server for uploading and downloading files """ """ A Simple file server for uploading and downloading files """
import json import json
import logging.config
import os import os
from argparse import ArgumentParser from argparse import ArgumentParser
from pathlib import Path from pathlib import Path
from flask import Flask, request, send_from_directory, safe_join from flask import Flask, request, send_from_directory, safe_join
from pyhocon import ConfigFactory from flask_compress import Compress
from flask_cors import CORS
logging.config.dictConfig(ConfigFactory.parse_file("logging.conf")) from config import config
app = Flask(__name__) app = Flask(__name__)
CORS(app, **config.get("fileserver.cors"))
Compress(app)
@app.route("/", methods=["POST"]) @app.route("/", methods=["POST"])
@ -29,7 +31,15 @@ def upload():
@app.route("/<path:path>", methods=["GET"]) @app.route("/<path:path>", methods=["GET"])
def download(path): def download(path):
return send_from_directory(app.config["UPLOAD_FOLDER"], path) response = send_from_directory(app.config["UPLOAD_FOLDER"], path)
if config.get("fileserver.download.disable_browser_caching", False):
headers = response.headers
headers["Pragma-directive"] = "no-cache"
headers["Cache-directive"] = "no-cache"
headers["Cache-control"] = "no-cache"
headers["Pragma"] = "no-cache"
headers["Expires"] = "0"
return response
def main(): def main():

View File

@ -1,2 +1,4 @@
Flask Flask
Flask-Cors>=3.0.5
Flask-Compress>=1.4.0
pyhocon>=0.3.35 pyhocon>=0.3.35