I think this is production ready now.

This commit is contained in:
Dominik V. Salonen 2015-12-23 23:27:12 +01:00
parent e54c7f80bf
commit f7d9c0e87c
3 changed files with 22 additions and 5 deletions

1
.gitignore vendored
View File

@ -4,5 +4,6 @@
conf.py conf.py
# Data # Data
data/* data/*
legacy/*
auth_data.json auth_data.json
files.db files.db

View File

@ -21,6 +21,7 @@ config["ALLOWED_EXTENSIONS"] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
# If this is set to true, old files will be deleted. TIME is how far behind (in seconds) the last accessed time can be before files get deleted # If this is set to true, old files will be deleted. TIME is how far behind (in seconds) the last accessed time can be before files get deleted
config["DELETE_FILES"] = True config["DELETE_FILES"] = True
config["TIME"] = 30 config["TIME"] = 30
config["CLEAN_INTERVAL"] = 120
# Site info displayed to the user # Site info displayed to the user
config["SITE_DATA"] = { config["SITE_DATA"] = {

25
run.py
View File

@ -28,16 +28,24 @@ log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR) log.setLevel(logging.ERROR)
def cleaner_thread():
# This is horrid
while True:
print_log('Notice', 'Cleaner started')
delete_old()
time.sleep(config["CLEAN_INTERVAL"])
def delete_old(): def delete_old():
targetTime = time.time() - config["TIME"] targetTime = time.time() - config["TIME"]
old = db.get_old_files(targetTime) old = db.get_old_files(targetTime)
for file in old: for file in old:
print_log('Notice', 'Removing old file "' + file["file"] + '"')
try: try:
os.remove(os.path.join(config["UPLOAD_FOLDER"], file["file"])) os.remove(os.path.join(config["UPLOAD_FOLDER"], file["file"]))
except Exception: except Exception:
pass print_log('Warning', 'Failed to delete old file "' + file["file"] + '"')
db.delete_entry(file["file"]) db.delete_entry(file["file"])
print_log('Notice', 'Removed old file "' + file["file"] + '"')
def auth(key): def auth(key):
@ -56,6 +64,7 @@ def allowed_file(filename):
@app.route('/', methods=['GET', 'POST']) @app.route('/', methods=['GET', 'POST'])
def upload_file(): def upload_file():
if request.method == 'POST': if request.method == 'POST':
print_log('Web', 'New file Relieved')
if not auth(request.headers.get('X-Hyozan-Auth')): if not auth(request.headers.get('X-Hyozan-Auth')):
abort(403) abort(403)
data = dict() data = dict()
@ -75,7 +84,7 @@ def upload_file():
data["file"] = filename data["file"] = filename
data["url"] = config["DOMAIN"] + "/" + filename data["url"] = config["DOMAIN"] + "/" + filename
print_log('Main', 'New file processed') print_log('Main', 'New file processed "' + filename + '"')
if request.form["source"] == "web": if request.form["source"] == "web":
return redirect(url_for('get_file', filename=filename), code=302) return redirect(url_for('get_file', filename=filename), code=302)
@ -89,15 +98,21 @@ def upload_file():
@app.route('/<filename>', methods=['GET']) @app.route('/<filename>', methods=['GET'])
def get_file(filename): def get_file(filename):
print_log('Main', 'Hit "' + filename + '" - ' + time_to_string(time.time())) print_log('Web', 'Hit "' + filename + '" - ' + time_to_string(time.time()))
db.update_file(filename) db.update_file(filename)
return send_from_directory(config['UPLOAD_FOLDER'], filename) return send_from_directory(config['UPLOAD_FOLDER'], filename)
delete_old() @app.route('/share/<filename>')
@app.route('/file/<filename>')
def serve_legacy(filename):
return send_from_directory('legacy', filename)
cleaner = Thread(target = cleaner_thread, )
cleaner.start()
if __name__ == '__main__': if __name__ == '__main__':
app.run( app.run(
port=config["PORT"], port=config["PORT"],
host=config["HOST"], host=config["HOST"],
debug=config["DEBUG"] debug=config["DEBUG"]
) )
cleaner.join(timeout=15)