Adds delete_file util (#239)

This commit is contained in:
Kevin Chung
2017-04-08 01:34:22 -04:00
committed by GitHub
parent fd22ef98dc
commit 72f7eeb0a4
2 changed files with 12 additions and 5 deletions

View File

@@ -163,11 +163,8 @@ def admin_files(chalid):
return jsonify(json_data) return jsonify(json_data)
if request.method == 'POST': if request.method == 'POST':
if request.form['method'] == "delete": if request.form['method'] == "delete":
f = Files.query.filter_by(id=request.form['file']).first_or_404() utils.delete_file(request.form['file'])
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() db.session.commit()
db.session.close() db.session.close()
return '1' return '1'

View File

@@ -349,6 +349,16 @@ def upload_file(file, chalid):
return True 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() @cache.memoize()
def get_config(key): def get_config(key):
config = Config.query.filter_by(key=key).first() config = Config.query.filter_by(key=key).first()