finished project - working

This commit is contained in:
klein panic
2024-10-01 23:06:33 -04:00
parent da9156aaa3
commit 714c1fbe33
11 changed files with 359 additions and 78 deletions

View File

@@ -1,10 +1,13 @@
from flask import Flask, request, render_template, redirect, url_for, session, send_from_directory
from flask import Flask, request, render_template, redirect, url_for, session, send_from_directory, jsonify, send_file
from flask_talisman import Talisman
from functools import wraps
import os
from security import validate_user
from data_handler import save_link, save_file, retrieve_uploads, handle_download, get_file_path
from datetime import datetime
from zipfile import ZipFile
from preview import generate_preview # Import the preview module
from rename import rename_file # Import the rename module
app = Flask(__name__, template_folder='../templates')
app.secret_key = os.urandom(24)
@@ -35,6 +38,8 @@ def login_required(f):
def ensure_login():
if 'username' not in session and request.endpoint not in ('login', 'static'):
return redirect(url_for('login'))
elif request.endpoint == 'login' and 'username' in session:
session.clear()
@app.route('/')
@login_required
@@ -69,15 +74,18 @@ def upload_link():
save_link(uploader, link)
return redirect(url_for('index'))
@app.route('/upload/file', methods=['POST'])
@app.route('/upload/files', methods=['POST'])
@login_required
def upload_file():
if 'file' not in request.files:
def upload_files():
if 'files' not in request.files:
return redirect(url_for('index'))
file = request.files['file']
files = request.files.getlist('files')
uploader = session['username']
save_file(uploader, file)
for file in files:
save_file(uploader, file)
return redirect(url_for('index'))
@app.route('/uploads')
@@ -104,14 +112,12 @@ def view_uploads():
def download_link(link_id):
upload = handle_download(link_id)
if upload[2] == 'link':
if upload and upload[2] == 'link':
link_content = upload[3]
x = 1
while os.path.exists(os.path.join(DOWNLOADS_DIRECTORY, f"link_{x}.txt")):
x += 1
filename = f"link_{x}.txt"
filepath = os.path.join(DOWNLOADS_DIRECTORY, filename)
with open(filepath, 'w') as f:
f.write(link_content)
@@ -119,7 +125,6 @@ def download_link(link_id):
response = send_from_directory(DOWNLOADS_DIRECTORY, filename, as_attachment=True)
handle_download(link_id, delete_only=True)
return response
return "Link Not found", 404
@@ -137,13 +142,9 @@ def download_all_links():
for link in links:
f.write(link[3] + "\n")
# Serve the combined links file from the Downloads directory
response = send_from_directory(DOWNLOADS_DIRECTORY, filename, as_attachment=True)
# Now delete all the link entries from the database after serving
for link in links:
handle_download(link[0], delete_only=True)
return response
else:
return redirect(url_for('view_uploads'))
@@ -153,23 +154,22 @@ def download_all_links():
def download(upload_id):
upload = handle_download(upload_id)
if not upload:
print(f"Error: No upload found with ID {upload_id}")
return "The requested file does not exist or you do not have permission to access it.", 404
if upload[2] == 'file':
if upload and upload[2] == 'file':
file_path = get_file_path(upload[3])
print(f"Trying to download file: {file_path}")
if not os.path.isfile(file_path):
print(f"Error: File not found at {file_path}")
return "File not found", 404
response = send_from_directory(os.path.dirname(file_path), os.path.basename(file_path), as_attachment=True)
return redirect(url_for('view_uploads'), code=404)
response = send_from_directory(
directory=os.path.dirname(file_path),
path=os.path.basename(file_path),
as_attachment=True,
#download_name=os.path.basename(file_path) # Ensures the download retains the current name
download_name=upload[3]
)
handle_download(upload_id, delete_only=True)
return response
return response
return "The requested file does not exist or you do not have permission to access it.", 404
@app.route('/delete_link/<int:link_id>', methods=['GET'])
@login_required
@@ -183,6 +183,88 @@ def delete_file(file_id):
handle_download(file_id, delete_only=True)
return redirect(url_for('view_uploads'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, ssl_context='adhoc')
# Add routes for downloading all photos
@app.route('/download_all_photos', methods=['GET'])
@login_required
def download_all_photos():
photos = [upload for upload in retrieve_uploads() if upload[2] == 'file' and upload[3].lower().endswith(('.jpg', '.jpeg', '.png', '.gif'))]
if photos:
zip_filename = f"photos_{datetime.now().strftime('%m-%d-%Y')}.zip"
zip_filepath = os.path.join(DOWNLOADS_DIRECTORY, zip_filename)
# Create a zip file containing all photos
with ZipFile(zip_filepath, 'w') as zipf:
for photo in photos:
file_path = get_file_path(photo[3])
zipf.write(file_path, os.path.basename(file_path))
handle_download(photo[0], delete_only=True) # Delete each photo entry
return send_from_directory(DOWNLOADS_DIRECTORY, zip_filename, as_attachment=True)
else:
return redirect(url_for('view_uploads'))
# Add similar routes for videos and misc
@app.route('/download_all_videos', methods=['GET'])
@login_required
def download_all_videos():
videos = [upload for upload in retrieve_uploads() if upload[2] == 'file' and upload[3].lower().endswith(('.mp4', '.mkv', '.avi'))]
if videos:
zip_filename = f"videos_{datetime.now().strftime('%m-%d-%Y')}.zip"
zip_filepath = os.path.join(DOWNLOADS_DIRECTORY, zip_filename)
with ZipFile(zip_filepath, 'w') as zipf:
for video in videos:
file_path = get_file_path(video[3])
zipf.write(file_path, os.path.basename(file_path))
handle_download(video[0], delete_only=True) # Delete each video entry
return send_from_directory(DOWNLOADS_DIRECTORY, zip_filename, as_attachment=True)
else:
return redirect(url_for('view_uploads'))
@app.route('/download_all_misc', methods=['GET'])
@login_required
def download_all_misc():
misc_files = [upload for upload in retrieve_uploads() if upload[2] == 'file' and upload not in videos + photos]
if misc_files:
zip_filename = f"misc_{datetime.now().strftime('%m-%d-%Y')}.zip"
zip_filepath = os.path.join(DOWNLOADS_DIRECTORY, zip_filename)
with ZipFile(zip_filepath, 'w') as zipf:
for item in misc_files:
file_path = get_file_path(item[3])
zipf.write(file_path, os.path.basename(file_path))
handle_download(item[0], delete_only=True) # Delete each misc entry
return send_from_directory(DOWNLOADS_DIRECTORY, zip_filename, as_attachment=True)
else:
return redirect(url_for('view_uploads'))
# Route to handle file renaming
@app.route('/rename/<int:upload_id>', methods=['POST'])
@login_required
def rename(upload_id):
data = request.get_json() # Expecting JSON data from the frontend
new_name = data.get('new_name', '').strip()
if not new_name:
return jsonify({"error": "New name not provided"}), 400
success, message = rename_file(upload_id, new_name)
if success:
return jsonify({"success": True}), 200
else:
return jsonify({"error": message}), 400
# Route to handle file previews
@app.route('/preview/<int:upload_id>', methods=['GET'])
@login_required
def preview(upload_id):
return generate_preview(upload_id)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, debug=True, ssl_context='adhoc')