add stats

This commit is contained in:
Alexandru 2024-09-13 22:04:52 +03:00
parent 79982ecb58
commit 5e749cb3a4
3 changed files with 20 additions and 2 deletions

19
app.py
View File

@ -4,6 +4,7 @@ import os, sys, time, hashlib
from apscheduler.schedulers.background import BackgroundScheduler
import markdown
import shutil
from pathlib import Path
app = Flask(__name__)
db = sqlite3.connect('wastetape.db', check_same_thread=False)
@ -12,7 +13,7 @@ app.secret_key = hashlib.md5(os.urandom(32)).hexdigest()
maxFileSize = 1024 * 1024 * 128
maxExpiry = 60 * 60 * 24 * 7
allowedFormats = ["png", "jpg", "jpeg", "gif", "pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx", "mp4", "mpg", "wmv", "mov", "avi", "swf", "zip", "tar.gz", "tar", "rar", "7z", "mp3", "txt", "py", "php", "htm", "html", "css", "js", "ts", "cr", "c", "cpp", "rs", "rst", "md"]
allowedFormats = ["png", "jpg", "jpeg", "gif", "pdf", "doc", "docx", "ppt", "pptx", "xls", "xlsx", "mp4", "mpg", "wmv", "mov", "avi", "swf", "zip", "tar.gz", "tar", "rar", "7z", "mp3", "txt", "py", "php", "htm", "html", "css", "js", "ts", "cr", "c", "cpp", "rs", "rst", "md", "webp", "webm"]
def removal():
cs.execute("SELECT * FROM files WHERE deletion < ?;", (int(time.time()),))
@ -49,6 +50,20 @@ def gettxt(value):
with open(os.path.join('static/files', value), 'r') as f:
return f.read()
@app.template_filter('totalfiles')
def totalfiles(value):
files = os.listdir("static/files")
return len(files)
@app.template_filter('totalfilesize')
def totalfilesize(value):
size = sum(p.stat().st_size for p in Path("static/files").rglob('*'))
print(size)
for unit in ("B", "K", "M", "G", "T"):
if size < 1024:
return f"{size:.1f}{unit}"
size /= 1024
@app.route('/')
def index():
@ -87,7 +102,7 @@ def upload():
os.remove(os.path.join('tmp', filename))
flash('File too large')
return redirect(url_for('index'))
if filename.split('.')[-1] not in allowedFormats:
if filename.lower().split('.')[-1] not in allowedFormats:
os.remove(os.path.join('tmp', filename))
flash('File format not allowed')
return redirect(url_for('index'))

View File

@ -27,5 +27,7 @@
</select>
<button class="upload-button">Upload</button><br> max file size: 134mb (temporary), 32mb (forever)
</form>
{% include 'stats.html' %}
</div>
{% endblock %}

1
templates/stats.html Normal file
View File

@ -0,0 +1 @@
<p class="center">{{ ""|totalfiles }} files uploaded // {{ ""|totalfilesize }} occupied</p>