manage_uploads deletes files now
This commit is contained in:
parent
90f7bf75ed
commit
62c444f703
65
fileHost.py
65
fileHost.py
|
@ -3,11 +3,11 @@
|
||||||
Simple file host using Flask.
|
Simple file host using Flask.
|
||||||
"""
|
"""
|
||||||
import os
|
import os
|
||||||
import time
|
|
||||||
import string
|
import string
|
||||||
import secrets
|
import secrets
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import threading
|
import threading
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
from passlib.hash import argon2
|
from passlib.hash import argon2
|
||||||
from flask import Flask, session, request, abort, redirect, url_for, g, \
|
from flask import Flask, session, request, abort, redirect, url_for, g, \
|
||||||
|
@ -147,27 +147,41 @@ def verify_username(username):
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@app.route("/delete_file", methods=["POST"])
|
def delete_file(filename):
|
||||||
def delete_file():
|
|
||||||
"""
|
"""
|
||||||
Allows an admin to delete a file from the upload directory and the database.
|
Deletes a file from the upload directory and from the database.
|
||||||
"""
|
"""
|
||||||
username = request.form.get("user")
|
|
||||||
password = request.form.get("pass")
|
|
||||||
filename = request.form.get("fname")
|
|
||||||
|
|
||||||
if not verify_password(username, password):
|
|
||||||
abort(401)
|
|
||||||
if not g.admin:
|
|
||||||
abort(401)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
os.remove(os.path.join(app.config.get("UPLOAD_DIR"), filename))
|
os.remove(os.path.join(app.config.get("UPLOAD_DIR"), filename))
|
||||||
db.execute("DELETE FROM uploads WHERE filename = ?", (filename,))
|
db_execute("DELETE FROM uploads WHERE filename = ?", (filename,))
|
||||||
con.commit()
|
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
return "Error: File not found."
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/delete_file", methods=["POST"])
|
||||||
|
def deleteFile():
|
||||||
|
"""
|
||||||
|
Allows a user to delete a file from the upload directory and the database.
|
||||||
|
"""
|
||||||
|
username = session.get("username")
|
||||||
|
filename = request.form.get("fname")
|
||||||
|
|
||||||
|
if not verify_username(username):
|
||||||
|
abort(401)
|
||||||
|
if not g.admin:
|
||||||
|
uploader = db_execute(
|
||||||
|
"SELECT uploaded_by FROM uploads WHERE filename=?",
|
||||||
|
(filename,)).fetchone()[0]
|
||||||
|
if uploader != username:
|
||||||
|
abort(401)
|
||||||
|
|
||||||
|
res = delete_file(filename)
|
||||||
|
if res:
|
||||||
return "Success"
|
return "Success"
|
||||||
|
else:
|
||||||
|
return "Error: File not found."
|
||||||
|
|
||||||
|
|
||||||
@app.route("/add_user", methods=["POST"])
|
@app.route("/add_user", methods=["POST"])
|
||||||
|
@ -258,18 +272,29 @@ def manage_uploads():
|
||||||
if not verify_username(username):
|
if not verify_username(username):
|
||||||
abort(401)
|
abort(401)
|
||||||
|
|
||||||
|
if request.method == "GET":
|
||||||
uploads = db_execute(
|
uploads = db_execute(
|
||||||
"SELECT filename, uploaded_date FROM uploads WHERE uploaded_by = ?",
|
"SELECT filename, uploaded_date FROM uploads WHERE uploaded_by = ?",
|
||||||
(username,)).fetchall()
|
(username,)).fetchall()
|
||||||
|
|
||||||
new_uploads = []
|
new_uploads = []
|
||||||
for file, date in uploads:
|
for file, date in uploads:
|
||||||
file = app.config.get("UPLOAD_URL") + file
|
date = datetime.fromtimestamp(date).strftime("%Y-%m-%d %H:%M")
|
||||||
date = time.strftime("%Y-%m-%d %H:%M", date)
|
|
||||||
new_uploads.append((file, date))
|
new_uploads.append((file, date))
|
||||||
|
|
||||||
if request.method == "GET":
|
return render_template("manage_uploads.html", uploads=new_uploads,
|
||||||
return render_template("manage_uploads.html", uploads=new_uploads)
|
upload_dir=app.config.get("UPLOAD_URL"))
|
||||||
|
|
||||||
|
deletes = [fname for fname,_ in request.form.items()]
|
||||||
|
deletes.remove("submit")
|
||||||
|
for filename in deletes:
|
||||||
|
uploader = db_execute(
|
||||||
|
"SELECT uploaded_by FROM uploads WHERE filename=?",
|
||||||
|
(filename,)).fetchone()[0]
|
||||||
|
if uploader != username:
|
||||||
|
abort(401)
|
||||||
|
delete_file(filename)
|
||||||
|
return redirect(url_for("manage_uploads"))
|
||||||
|
|
||||||
|
|
||||||
@app.route("/", methods=["POST", "GET"])
|
@app.route("/", methods=["POST", "GET"])
|
||||||
|
|
|
@ -5,11 +5,12 @@
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<form method="post" enctype="multipart/form-data" action="{{ url_for('index') }}">
|
<form method="post" enctype="multipart/form-data" action="{{ url_for('index') }}">
|
||||||
Hello, {{ session.username }}<br/>
|
Hello, {{ session.username }}<br />
|
||||||
<a href="{{ url_for('change_password') }}">Change password</a><br/>
|
<a href="{{ url_for('change_password') }}">Change password</a><br />
|
||||||
<a href="{{ url_for('logout') }}">Logout</a><br/>
|
<a href="{{ url_for('logout') }}">Logout</a><br />
|
||||||
|
<a href="{{ url_for('manage_uploads') }}">Manage Uploads</a><br />
|
||||||
<p>Select file to upload:
|
<p>Select file to upload:
|
||||||
<p><input type="file" name="file" required/><br/>
|
<p><input type="file" name="file" required/><br />
|
||||||
<input type="checkbox" name="randname"> Generate random filename.
|
<input type="checkbox" name="randname"> Generate random filename.
|
||||||
<p><input type="submit" value="Upload File" name="submit"/>
|
<p><input type="submit" value="Upload File" name="submit"/>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
@ -4,17 +4,22 @@
|
||||||
<title>Manage your uploads</title>
|
<title>Manage your uploads</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<form method="post" enctype="multipart/form-data" action="{{ url_for('manage_uploads') }}">
|
||||||
<table>
|
<table>
|
||||||
<tr>
|
<tr>
|
||||||
<th>File</th>
|
<th>File</th>
|
||||||
<th>Date Uploaded</th>
|
<th>Date Uploaded</th>
|
||||||
|
<th>Delete</th>
|
||||||
</tr>
|
</tr>
|
||||||
{% for file, date in uploads %}
|
{% for file, date in uploads %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ upload_url + file }}</td>
|
<td><a href="{{ upload_dir + file }}">{{ file }}</a></td>
|
||||||
<td>{{ date|string }}</td>
|
<td>{{ date }}</td>
|
||||||
|
<td><input type="checkbox" name="{{ file }}"/></td>
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</table>
|
</table>
|
||||||
|
<input type="submit" value="Delete this" name="submit"/>
|
||||||
|
</form>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user