2019-06-10 21:24:35 +00:00
|
|
|
""" A Simple file server for uploading and downloading files """
|
|
|
|
import json
|
2021-08-05 13:46:25 +00:00
|
|
|
import mimetypes
|
2019-06-10 21:24:35 +00:00
|
|
|
import os
|
|
|
|
from argparse import ArgumentParser
|
|
|
|
from pathlib import Path
|
|
|
|
|
2021-05-03 14:00:38 +00:00
|
|
|
from boltons.iterutils import first
|
2021-05-27 14:13:02 +00:00
|
|
|
from flask import Flask, request, send_from_directory, abort, Response
|
2019-07-17 15:16:43 +00:00
|
|
|
from flask_compress import Compress
|
|
|
|
from flask_cors import CORS
|
2021-05-27 14:13:02 +00:00
|
|
|
from werkzeug.exceptions import NotFound
|
|
|
|
from werkzeug.security import safe_join
|
2019-06-10 21:24:35 +00:00
|
|
|
|
2019-07-17 15:16:43 +00:00
|
|
|
from config import config
|
2022-03-15 14:27:31 +00:00
|
|
|
from utils import get_env_bool
|
2019-06-10 21:24:35 +00:00
|
|
|
|
2020-06-01 10:05:45 +00:00
|
|
|
DEFAULT_UPLOAD_FOLDER = "/mnt/fileserver"
|
|
|
|
|
2019-06-10 21:24:35 +00:00
|
|
|
app = Flask(__name__)
|
2019-07-17 15:16:43 +00:00
|
|
|
CORS(app, **config.get("fileserver.cors"))
|
2022-03-15 14:27:31 +00:00
|
|
|
|
|
|
|
if get_env_bool("CLEARML_COMPRESS_RESP", default=True):
|
|
|
|
Compress(app)
|
2019-06-10 21:24:35 +00:00
|
|
|
|
2021-05-03 14:00:38 +00:00
|
|
|
app.config["UPLOAD_FOLDER"] = first(
|
|
|
|
(os.environ.get(f"{prefix}_UPLOAD_FOLDER") for prefix in ("CLEARML", "TRAINS")),
|
|
|
|
default=DEFAULT_UPLOAD_FOLDER,
|
2021-05-03 13:58:11 +00:00
|
|
|
)
|
|
|
|
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = config.get(
|
|
|
|
"fileserver.download.cache_timeout_sec", 5 * 60
|
|
|
|
)
|
2020-02-26 15:26:46 +00:00
|
|
|
|
2019-06-10 21:24:35 +00:00
|
|
|
|
2022-03-15 14:27:31 +00:00
|
|
|
@app.before_request
|
|
|
|
def before_request():
|
|
|
|
if request.content_encoding:
|
|
|
|
return f"Content encoding is not supported ({request.content_encoding})", 415
|
|
|
|
|
|
|
|
|
2022-03-15 14:21:52 +00:00
|
|
|
@app.after_request
|
|
|
|
def after_request(response):
|
2022-03-15 14:27:31 +00:00
|
|
|
response.headers["server"] = config.get(
|
|
|
|
"fileserver.response.headers.server", "clearml"
|
|
|
|
)
|
2022-03-15 14:21:52 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
2019-06-10 21:24:35 +00:00
|
|
|
@app.route("/", methods=["POST"])
|
|
|
|
def upload():
|
|
|
|
results = []
|
|
|
|
for filename, file in request.files.items():
|
|
|
|
if not filename:
|
|
|
|
continue
|
|
|
|
file_path = filename.lstrip(os.sep)
|
2021-05-27 14:13:02 +00:00
|
|
|
safe_path = safe_join(app.config["UPLOAD_FOLDER"], file_path)
|
|
|
|
if safe_path is None:
|
|
|
|
raise NotFound()
|
|
|
|
target = Path(safe_path)
|
2019-06-10 21:24:35 +00:00
|
|
|
target.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
file.save(str(target))
|
|
|
|
results.append(file_path)
|
2021-05-03 14:00:38 +00:00
|
|
|
return json.dumps(results), 200
|
2019-06-10 21:24:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
@app.route("/<path:path>", methods=["GET"])
|
|
|
|
def download(path):
|
2021-01-05 15:31:24 +00:00
|
|
|
as_attachment = "download" in request.args
|
2021-08-05 13:46:25 +00:00
|
|
|
|
|
|
|
_, encoding = mimetypes.guess_type(os.path.basename(path))
|
|
|
|
mimetype = "application/octet-stream" if encoding == "gzip" else None
|
|
|
|
|
2021-05-03 13:58:11 +00:00
|
|
|
response = send_from_directory(
|
2022-03-15 14:27:31 +00:00
|
|
|
app.config["UPLOAD_FOLDER"],
|
|
|
|
path,
|
|
|
|
as_attachment=as_attachment,
|
|
|
|
mimetype=mimetype,
|
2021-05-03 13:58:11 +00:00
|
|
|
)
|
2019-07-17 15:16:43 +00:00
|
|
|
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
|
2019-06-10 21:24:35 +00:00
|
|
|
|
|
|
|
|
2021-01-05 16:53:23 +00:00
|
|
|
@app.route("/<path:path>", methods=["DELETE"])
|
|
|
|
def delete(path):
|
2022-03-15 14:27:31 +00:00
|
|
|
real_path = Path(safe_join(os.fspath(app.config["UPLOAD_FOLDER"]), os.fspath(path)))
|
2021-05-03 15:14:56 +00:00
|
|
|
if not real_path.exists() or not real_path.is_file():
|
2021-05-03 13:58:11 +00:00
|
|
|
abort(Response(f"File {str(path)} not found", 404))
|
|
|
|
|
2021-05-03 15:14:56 +00:00
|
|
|
real_path.unlink()
|
2021-05-03 13:58:11 +00:00
|
|
|
return json.dumps(str(path)), 200
|
2021-01-05 16:53:23 +00:00
|
|
|
|
|
|
|
|
2019-06-10 21:24:35 +00:00
|
|
|
def main():
|
|
|
|
parser = ArgumentParser(description=__doc__)
|
|
|
|
parser.add_argument(
|
|
|
|
"--port", "-p", type=int, default=8081, help="Port (default %(default)d)"
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--ip", "-i", type=str, default="0.0.0.0", help="Address (default %(default)s)"
|
|
|
|
)
|
|
|
|
parser.add_argument("--debug", action="store_true", default=False)
|
|
|
|
parser.add_argument(
|
|
|
|
"--upload-folder",
|
|
|
|
"-u",
|
2021-01-05 16:53:23 +00:00
|
|
|
help=f"Upload folder (default {DEFAULT_UPLOAD_FOLDER})",
|
2019-06-10 21:24:35 +00:00
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
2021-01-05 16:53:23 +00:00
|
|
|
if args.upload_folder is not None:
|
2020-06-01 10:05:45 +00:00
|
|
|
app.config["UPLOAD_FOLDER"] = args.upload_folder
|
2019-06-10 21:24:35 +00:00
|
|
|
|
|
|
|
app.run(debug=args.debug, host=args.ip, port=args.port, threaded=True)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|