Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dicom server #84

Merged
merged 1 commit into from
Sep 18, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 51 additions & 1 deletion SlideServer.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def secure_relative_path(filename):
raise ValueError("Filepath contains '/./' which is forbidden")
if ".." in filename:
raise ValueError("Filepath contains '..' which is forbidden")
if filename[0] == '.':
raise ValueError("Filepath starts with '.' (or is '.') which is forbidden")
level_names = filename.split(os.sep)
filename = ""
for name in level_names:
Expand Down Expand Up @@ -193,7 +195,9 @@ def finish_upload(token):
shutil.move(tmppath, filepath)
return flask.Response(json.dumps({"ended": token, "filepath": filepath, "filename": filename, "relpath": relpath}), status=200, mimetype='text/json')
else:
return flask.Response(json.dumps({"error": "File with name '" + filename + "' already exists", "filename": filename}), status=400, mimetype='text/json')
return flask.Response(json.dumps({"error": "File with name '" + filename + "' already exists", "filepath": filepath, "filename": filename}), status=400, mimetype='text/json')
# The above return "filename" to show the user the sanitized filename
# and on success, return relpath for subsequent SlideLoader calls by the frontend.
else:
return flask.Response(json.dumps({"error": "Invalid filename"}), status=400, mimetype='text/json')

Expand Down Expand Up @@ -268,6 +272,24 @@ def multiSlide(filepathlist):
else:
return flask.Response(json.dumps(res), status=200, mimetype='text/json')

# Used by Caracal; may be removed after our schema fully supports multifile formats in a subdir
@app.route("/data/folder/<path:relpath>", methods=['GET'])
def listFolderContents(relpath):
res = {}
try:
relpath = secure_relative_path(relpath)
absolutepath = os.path.join(app.config['UPLOAD_FOLDER'], relpath)
except BaseException as e:
res['error'] = "bad folderpath: " + str(e)
return flask.Response(json.dumps(res), status=400, mimetype='text/json')

try:
res['contents'] = os.listdir(absolutepath)
res['contents'] = [filename for filename in res['contents'] if not filename.startswith('.')]
return flask.Response(json.dumps(res), status=200, mimetype='text/json')
except:
res['contents'] = []
return flask.Response(json.dumps(res), status=200, mimetype='text/json')

@app.route("/getSlide/<path:image_name>")
def getSlide(image_name):
Expand Down Expand Up @@ -668,3 +690,31 @@ def checkDownloadStatus():
return flask.Response(json.dumps({"downloadDone": True}), status=200, mimetype='text/json')
return flask.Response(json.dumps({"downloadDone": False}), status=200, mimetype='text/json')

# DICOM Explorer UI and DICOM server hostname and port
@app.route('/dicomsrv/location', methods=['GET'])
def guiLocation():
port = os.getenv("DICOM_PORT")
hostname = os.getenv("DICOM_HOSTNAME")
ui_port = os.getenv("DICOM_UI_PORT")
ui_hostname = os.getenv("DICOM_UI_HOSTNAME")
res = {}
if port is not None:
res["port"] = int(port)
else:
print("DICOM_PORT env variable not found")

if ui_port is not None:
res["ui_port"] = int(ui_port)
else:
print("DICOM_UI_PORT env variable not found")


# If the DICOM server is on a different computer, this can be uncommented,
# the frontend will parse this, but it's better to keep this in a comment against env var poisoning
# if hostname is not None:
# res["hostname"] = hostname
# if ui_hostname is not None:
# res["ui_hostname"] = ui_hostname

success = "port" in res and "ui_port" in res
return flask.Response(json.dumps(res), status=200 if success else 500, mimetype='text/json')