finished project - working
This commit is contained in:
37
server/preview.py
Normal file
37
server/preview.py
Normal file
@@ -0,0 +1,37 @@
|
||||
# preview.py
|
||||
import os
|
||||
from flask import send_file, jsonify
|
||||
import sqlite3
|
||||
import mimetypes
|
||||
|
||||
UPLOAD_DIRECTORY = "../assets"
|
||||
|
||||
def generate_preview(upload_id):
|
||||
conn = sqlite3.connect('transfer_service.db')
|
||||
c = conn.cursor()
|
||||
|
||||
c.execute('SELECT file_type, content FROM uploads WHERE id = ?', (upload_id,))
|
||||
upload = c.fetchone()
|
||||
|
||||
if not upload:
|
||||
conn.close()
|
||||
return jsonify({"error": "File not found"}), 404
|
||||
|
||||
file_type, filename = upload
|
||||
file_path = os.path.join(UPLOAD_DIRECTORY, filename)
|
||||
|
||||
if not os.path.exists(file_path):
|
||||
return jsonify({"error": "File not found"}), 404
|
||||
|
||||
# Detect the MIME type
|
||||
mime_type, _ = mimetypes.guess_type(filename)
|
||||
|
||||
# Handle different file types for preview
|
||||
if file_type == "link":
|
||||
return jsonify({"link": filename}), 200
|
||||
|
||||
# Send the file with the correct MIME type
|
||||
if file_type == "file" and filename.lower().endswith(('.jpg', '.jpeg', '.png', '.gif')):
|
||||
return send_file(file_path, mimetype='image/jpeg', download_name=os.path.basename(file_path))
|
||||
|
||||
return jsonify({"error": "Preview not supported for this file type"}), 400
|
||||
Reference in New Issue
Block a user