Here comes my trump card

This commit is contained in:
Dominik V. Salonen 2015-12-23 22:45:40 +01:00
parent fa544f17f5
commit 5023dadc4e
3 changed files with 30 additions and 0 deletions

View File

@ -25,6 +25,19 @@ def add_b2(filename, file_id):
db.commit()
db.close()
def get_old_files(targetTime):
db = connect('files.db')
cur = db.execute('SELECT file FROM files WHERE accessed <= ?', [targetTime,])
rv = cur.fetchall()
db.close()
return [dict(file=row[0]) for row in rv]
def delete_entry(file):
db = connect('files.db')
db.execute('DELETE FROM files WHERE file = ?', [file,])
db.commit()
db.close()
def check_value(column, value):
db = connect('files.db')
cur = db.execute('SELECT EXISTS(SELECT 1 FROM files WHERE ? = ?)', [column, value])

View File

@ -18,6 +18,9 @@ config["KEY"] = ""
# File settings
config["UPLOAD_FOLDER"] = './data'
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
config["DELETE_FILES"] = True
config["TIME"] = 30
# Site info displayed to the user
config["SITE_DATA"] = {

14
run.py
View File

@ -28,6 +28,18 @@ log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
def delete_old():
targetTime = time.time() - config["TIME"]
old = db.get_old_files(targetTime)
for file in old:
try:
os.remove(os.path.join(config["UPLOAD_FOLDER"], file["file"]))
except Exception:
pass
db.delete_entry(file["file"])
print_log('Notice', 'Removed old file "' + file["file"] + '"')
def auth(key):
if config["KEY"] == "":
return True
@ -78,8 +90,10 @@ def upload_file():
@app.route('/<filename>', methods=['GET'])
def get_file(filename):
print_log('Main', 'Hit "' + filename + '" - ' + time_to_string(time.time()))
db.update_file(filename)
return send_from_directory(config['UPLOAD_FOLDER'], filename)
delete_old()
if __name__ == '__main__':
app.run(