diff --git a/CTFd/admin/challenges.py b/CTFd/admin/challenges.py index 562bfaba..c79ebe8c 100644 --- a/CTFd/admin/challenges.py +++ b/CTFd/admin/challenges.py @@ -163,11 +163,8 @@ def admin_files(chalid): return jsonify(json_data) if request.method == 'POST': if request.form['method'] == "delete": - f = Files.query.filter_by(id=request.form['file']).first_or_404() - upload_folder = os.path.join(app.root_path, app.config['UPLOAD_FOLDER']) - if os.path.exists(os.path.join(upload_folder, f.location)): # Some kind of os.path.isfile issue on Windows... - os.unlink(os.path.join(upload_folder, f.location)) - db.session.delete(f) + utils.delete_file(request.form['file']) + db.session.commit() db.session.close() return '1' diff --git a/CTFd/utils.py b/CTFd/utils.py index 5dd74c02..b86e2098 100644 --- a/CTFd/utils.py +++ b/CTFd/utils.py @@ -349,6 +349,16 @@ def upload_file(file, chalid): return True +def delete_file(filename): + f = Files.query.filter_by(id=filename).first_or_404() + upload_folder = os.path.join(app.root_path, app.config['UPLOAD_FOLDER']) + if os.path.exists(os.path.join(upload_folder, f.location)): # Some kind of os.path.isfile issue on Windows... + os.unlink(os.path.join(upload_folder, f.location)) + db.session.delete(f) + db.session.commit() + return True + + @cache.memoize() def get_config(key): config = Config.query.filter_by(key=key).first()