diff --git a/app/templates/dashboard.html b/app/templates/dashboard.html index 54106d7..f7b7d4d 100644 --- a/app/templates/dashboard.html +++ b/app/templates/dashboard.html @@ -20,16 +20,16 @@ Filename Size Retention - Uploaded + Timestamp Actions {% for file in files %} {{ file['id'] }} {{ file["filename"] }} - {{ file["size"] }} + {{ file["filesize"] }}mb {{ file["retention"] }} - {{ file["uploaded"] }} + {{ file["date"] }} Delete {% endfor %} @@ -57,7 +57,7 @@

Forgot your UserID?

Click below to view it.

- + diff --git a/app/worker.py b/app/worker.py index 3b4cc7b..40a2a41 100644 --- a/app/worker.py +++ b/app/worker.py @@ -44,7 +44,8 @@ def uploadFile(file, ip, userid, filename, id, retention): with open(f"{os.path.abspath(Config.fileDir)}/{filename}", "wb") as f: f.write(file.read()) - date = time.mktime(datetime.datetime.now().timetuple()) + timestamp = datetime.datetime.now() + timestamp = timestamp.timestamp() # Create the dictionary that we'll insert into the db data = { @@ -55,8 +56,8 @@ def uploadFile(file, ip, userid, filename, id, retention): 'retention': retention, 'userid': userid, 'ip': ip, - 'date': date, - 'expiry': date + retention + 'date': timestamp, + 'expiry': timestamp + retention } # Add the data and verify its there. @@ -87,13 +88,18 @@ def shortenURL(url, ip, userid, id, retention): retention = 604800 elif retention > 31540000: retention = 31540000 - + + timestamp = datetime.datetime.now() + timestamp = timestamp.timestamp() + data = { - "id": id, - "url": url, - "userid": userid, - "retention": retention, - "ip": ip + 'id': id, + 'url': url, + 'retention': retention, + 'userid': userid, + 'ip': ip, + 'date': timestamp, + 'expiry': timestamp + retention } Config.url.insert_one(data) diff --git a/autoclean-xygt.service b/autoclean-xygt.service new file mode 100644 index 0000000..23bbed5 --- /dev/null +++ b/autoclean-xygt.service @@ -0,0 +1,13 @@ +[Unit] +Description=Runs every minute to autoclean xygt. +StartLimitIntervalSec=0 + +[Service] +Type=simple +Restart=always +RestartSec=1 +User=root +ExecStart=/usr/bin/autoclean.py + +[Install] +WantedBy=multi-user.target \ No newline at end of file diff --git a/autoclean.py b/autoclean.py index be51573..b0443db 100755 --- a/autoclean.py +++ b/autoclean.py @@ -10,6 +10,7 @@ import datetime import os from pymongo import MongoClient from config import Config +import time class Config: # MongoDB init stuff @@ -21,28 +22,31 @@ class Config: fileDir = "./data" def main(): - print("Starting cleanup script...") + while True: + print("Starting cleanup script...") - # Get current time in unix timestamp - now = datetime.datetime.now() - now = now.timestamp() + # Get current time in unix timestamp + now = datetime.datetime.now() + now = now.timestamp() - # Get all expired files - expiredFiles = Config.files.find({"expiry": {"$lt": now}}) - expiredURLs = Config.url.find({"expiry": {"$lt": now}}) + # Get all expired files + expiredFiles = Config.files.find({"expiry": {"$lt": now}}) + expiredURLs = Config.url.find({"expiry": {"$lt": now}}) - # Delete all expired files - for file in expiredFiles: - print(f"Deleting file {file['id']}") - Config.files.delete_one({"id": file["id"]}) - os.remove(os.path.join(Config.fileDir, file["filename"])) + # Delete all expired files + for file in expiredFiles: + print(f"Deleting file {file['id']}") + Config.files.delete_one({"id": file["id"]}) + os.remove(os.path.join(Config.fileDir, file["filename"])) - # Delete all expired URL's - for url in expiredURLs: - print(f"Deleting URL {url['id']}") - Config.url.delete_one({"id": url["id"]}) + # Delete all expired URL's + for url in expiredURLs: + print(f"Deleting URL {url['id']}") + Config.url.delete_one({"id": url["id"]}) + + print("Cleanup complete.") + time.sleep(60) - print("Cleanup complete.") if __name__ == "__main__": main() \ No newline at end of file diff --git a/install.sh b/install.sh new file mode 100755 index 0000000..e085efd --- /dev/null +++ b/install.sh @@ -0,0 +1,18 @@ +# Might dockerise this stuff sooner or later, not now tho. + +if [ $EUID -ne 0 ]; then + echo "This script must be run as root." + exit 1 +fi + +apt install mongodb-org python3-pip python3-venv + +systemctl enable --now mongod + +cp autoclean-xygt.service /lib/systemd/system/ +cp autoclean.py /usr/bin/ +cp config.py /usr/bin/ + +systemctl enable --now autoclean-xygt.service + +echo "Installation complete, launch xygt with ./run.py (in the venv)" \ No newline at end of file diff --git a/moderation.py b/moderation.py new file mode 100644 index 0000000..bec1ecd --- /dev/null +++ b/moderation.py @@ -0,0 +1,31 @@ +from pymongo import MongoClient +import datetime + +class Config: + # MongoDB init stuff + client = MongoClient("mongodb://localhost:27017/") + db = client["xygt"] + files = db["file"] + url = db["url"] + users = db["users"] + + fileDir = "./data" + +def main(): + # Grab everything from the past day + now = datetime.datetime.now() + now = now.timestamp() + yesterday = now - 86400 + files = Config.files.find({'date': {'$gt': yesterday}}).sort('date', -1) + urls = Config.url.find({'date': {'$gt': yesterday}}).sort('date', -1) + + print("Files:") + for file in files: + print(f"File ID: {file['id']}, UserID: {file['userid']}, File Name: {file['filename']}, File Size: {file['filesize']}, File Type: {file['mimetype']}") + + print("URLs:") + for url in urls: + print(f"URL ID: {url['id']}, UserID: {url['userid']}, URL: {url['url']}") + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/run.py b/run.py new file mode 100755 index 0000000..1a175db --- /dev/null +++ b/run.py @@ -0,0 +1,9 @@ +#!/usr/bin/env python3 +# Run the app - MADE FOR SYSTEMD SERVICE + +from app import app + +if __name__ == '__main__': + app.run(host='0.0.0.0', debug=False) + +######################################## \ No newline at end of file