implemented cron thread for deleting files

This commit is contained in:
iou1name 2018-05-21 17:50:43 -04:00
parent 88e462b0e0
commit bd8e83426a

View File

@ -53,9 +53,17 @@ class ReverseProxied(object):
class CronThread(threading.Thread): class CronThread(threading.Thread):
def __init__(self): def __init__(self):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.stop = threading.Event()
def run(self): def run(self):
db_execute("SELECT filename, delete_date FROM uploads WHERE delete_date") while not self.stop.is_set():
records = db_execute(
"SELECT filename, delete_date FROM uploads WHERE delete_date"
).fetchall()
for filename, delete_date in records:
if time.time() >= delete_date:
delete_file(filename)
time.sleep(60)
app = Flask(__name__) app = Flask(__name__)
@ -87,6 +95,7 @@ def init():
""" """
os.makedirs(app.config.get("UPLOAD_DIR"), exist_ok=True) os.makedirs(app.config.get("UPLOAD_DIR"), exist_ok=True)
# init secret key
if os.path.exists("secret_key"): if os.path.exists("secret_key"):
with open("secret_key", "rb") as file: with open("secret_key", "rb") as file:
secret_key = file.read() secret_key = file.read()
@ -96,6 +105,7 @@ def init():
file.write(secret_key) file.write(secret_key)
app.secret_key = secret_key app.secret_key = secret_key
# init db
try: try:
db_execute("SELECT * FROM users").fetchone() db_execute("SELECT * FROM users").fetchone()
db_execute("SELECT * FROM uploads").fetchone() db_execute("SELECT * FROM uploads").fetchone()
@ -111,7 +121,11 @@ def init():
"uploaded_by TEXT," "uploaded_by TEXT,"
"uploaded_date INTEGER DEFAULT (STRFTIME('%s', 'now'))," "uploaded_date INTEGER DEFAULT (STRFTIME('%s', 'now')),"
"delete_date INTEGER)") "delete_date INTEGER)")
# init cron thread
t = CronThread()
t.start()
app.config["CRON_THREAD"] = t
def add_user(username, password, admin="FALSE"): def add_user(username, password, admin="FALSE"):