Support disabling response compression in fileserver

This commit is contained in:
allegroai 2022-03-15 16:27:31 +02:00
parent b6ad8f8790
commit c17cedd93a
2 changed files with 32 additions and 9 deletions

View File

@ -13,11 +13,14 @@ from werkzeug.exceptions import NotFound
from werkzeug.security import safe_join from werkzeug.security import safe_join
from config import config from config import config
from utils import get_env_bool
DEFAULT_UPLOAD_FOLDER = "/mnt/fileserver" DEFAULT_UPLOAD_FOLDER = "/mnt/fileserver"
app = Flask(__name__) app = Flask(__name__)
CORS(app, **config.get("fileserver.cors")) CORS(app, **config.get("fileserver.cors"))
if get_env_bool("CLEARML_COMPRESS_RESP", default=True):
Compress(app) Compress(app)
app.config["UPLOAD_FOLDER"] = first( app.config["UPLOAD_FOLDER"] = first(
@ -29,9 +32,17 @@ app.config["SEND_FILE_MAX_AGE_DEFAULT"] = config.get(
) )
@app.before_request
def before_request():
if request.content_encoding:
return f"Content encoding is not supported ({request.content_encoding})", 415
@app.after_request @app.after_request
def after_request(response): def after_request(response):
response.headers["server"] = config.get("fileserver.response.headers.server", "clearml") response.headers["server"] = config.get(
"fileserver.response.headers.server", "clearml"
)
return response return response
@ -60,7 +71,10 @@ def download(path):
mimetype = "application/octet-stream" if encoding == "gzip" else None mimetype = "application/octet-stream" if encoding == "gzip" else None
response = send_from_directory( response = send_from_directory(
app.config["UPLOAD_FOLDER"], path, as_attachment=as_attachment, mimetype=mimetype app.config["UPLOAD_FOLDER"],
path,
as_attachment=as_attachment,
mimetype=mimetype,
) )
if config.get("fileserver.download.disable_browser_caching", False): if config.get("fileserver.download.disable_browser_caching", False):
headers = response.headers headers = response.headers
@ -74,12 +88,7 @@ def download(path):
@app.route("/<path:path>", methods=["DELETE"]) @app.route("/<path:path>", methods=["DELETE"])
def delete(path): def delete(path):
real_path = Path( real_path = Path(safe_join(os.fspath(app.config["UPLOAD_FOLDER"]), os.fspath(path)))
safe_join(
os.fspath(app.config["UPLOAD_FOLDER"]),
os.fspath(path)
)
)
if not real_path.exists() or not real_path.is_file(): if not real_path.exists() or not real_path.is_file():
abort(Response(f"File {str(path)} not found", 404)) abort(Response(f"File {str(path)} not found", 404))

14
fileserver/utils.py Normal file
View File

@ -0,0 +1,14 @@
from distutils.util import strtobool
from os import getenv
from typing import Optional
def get_env_bool(*keys: str, default: bool = None) -> Optional[bool]:
try:
value = next(env for env in (getenv(key) for key in keys) if env is not None)
except StopIteration:
return default
try:
return bool(strtobool(value))
except ValueError:
return bool(value)