Here comes my trump card
This commit is contained in:
parent
fa544f17f5
commit
5023dadc4e
13
Hyozan/db.py
13
Hyozan/db.py
|
@ -25,6 +25,19 @@ def add_b2(filename, file_id):
|
||||||
db.commit()
|
db.commit()
|
||||||
db.close()
|
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):
|
def check_value(column, value):
|
||||||
db = connect('files.db')
|
db = connect('files.db')
|
||||||
cur = db.execute('SELECT EXISTS(SELECT 1 FROM files WHERE ? = ?)', [column, value])
|
cur = db.execute('SELECT EXISTS(SELECT 1 FROM files WHERE ? = ?)', [column, value])
|
||||||
|
|
|
@ -18,6 +18,9 @@ config["KEY"] = ""
|
||||||
# File settings
|
# File settings
|
||||||
config["UPLOAD_FOLDER"] = './data'
|
config["UPLOAD_FOLDER"] = './data'
|
||||||
config["ALLOWED_EXTENSIONS"] = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
|
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
|
# Site info displayed to the user
|
||||||
config["SITE_DATA"] = {
|
config["SITE_DATA"] = {
|
||||||
|
|
14
run.py
14
run.py
|
@ -28,6 +28,18 @@ log = logging.getLogger('werkzeug')
|
||||||
log.setLevel(logging.ERROR)
|
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):
|
def auth(key):
|
||||||
if config["KEY"] == "":
|
if config["KEY"] == "":
|
||||||
return True
|
return True
|
||||||
|
@ -78,8 +90,10 @@ 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('Main', 'Hit "' + filename + '" - ' + time_to_string(time.time()))
|
||||||
|
db.update_file(filename)
|
||||||
return send_from_directory(config['UPLOAD_FOLDER'], filename)
|
return send_from_directory(config['UPLOAD_FOLDER'], filename)
|
||||||
|
|
||||||
|
delete_old()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(
|
app.run(
|
||||||
|
|
Loading…
Reference in New Issue