From 3d3a6feec834c1830c69f3855df63c4a2a24e0a4 Mon Sep 17 00:00:00 2001 From: Anett Seeland Date: Tue, 12 Sep 2023 14:43:26 +0200 Subject: [PATCH] Add get of individual file for intermediate results --- src/volumecreator.py | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/volumecreator.py b/src/volumecreator.py index a57ddb9..3f7b6bb 100644 --- a/src/volumecreator.py +++ b/src/volumecreator.py @@ -27,14 +27,11 @@ def jsonify_no_content(): r'(?::\d+)?' # optional port r'(?:/?|[/?]\S+)$', re.IGNORECASE) -def sha256sum(filename): - h = hashlib.sha256() - b = bytearray(128*1024) - mv = memoryview(b) - with open(filename, 'rb', buffering=0) as f: - while n := f.readinto(mv): - h.update(mv[:n]) - return h.hexdigest() +def create_fileinfo(file_path): + name = file_path[len(app.config["UPLOAD_FOLDER"]):] + size = os.path.getsize(file_path) + mimetype = get_mimetype(file_path) + return {"name": name, "size": size, "mimetype" : mimetype} def add_mimetypes(): mimetypes.init() @@ -62,11 +59,13 @@ def get_mimetype(file_path): return mimetype + # init mimetypes and add missing types add_mimetypes() app = Flask(__name__) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER +# TODO: Check if necessary, only needed for sessions? app.config['SECRET_KEY'] = 'o5sGDoMC6LS2L0XIVBwprbgPADbxb3SJCaGLnzxVbCXr14JiWRGJFvAYq7eJoAA8' @app.route('/', methods=['GET', 'POST']) @@ -99,13 +98,16 @@ def upload_file(): @app.route('/list') def list_files(): files = [] - for file in glob.glob("%s/*"%app.config["UPLOAD_FOLDER"]) : - name = file[len(app.config["UPLOAD_FOLDER"]):] - size = os.path.getsize(file) - mimetype = get_mimetype(file) - files.append({"name": name, "size": size, "mimetype" : mimetype}) + for file_path in glob.glob("%s/*" % app.config["UPLOAD_FOLDER"]) : + files.append(create_fileinfo(file_path)) return jsonify(files) +@app.route('/list/') +def list_file(name): + file_path = os.path.join(app.config["UPLOAD_FOLDER"], name) + result = create_fileinfo(file_path) + return jsonify(result) + @app.route('/data/') def download_file(name): return send_from_directory(app.config["UPLOAD_FOLDER"], name)