save document f

This commit is contained in:
2025-01-21 20:45:29 +08:00
parent 7db4b4da98
commit 95822c0bfb
3 changed files with 34 additions and 8 deletions
+25
View File
@@ -2,6 +2,12 @@ import random
import string
import smtplib
from email.mime.text import MIMEText
import os
import uuid
import config
from werkzeug.utils import secure_filename
UPLOAD_ROOT = os.path.join(os.getcwd(), 'uploads')
def generate_password(length=10):
characters = string.ascii_letters + string.digits
@@ -23,3 +29,22 @@ def send_email(recipient_email, subject, body):
server.send_message(msg)
except Exception as e:
print(f"Failed to send email: {e}")
def is_file_allowed(file):
return '.' in file.filename and file.filename.rsplit('.', 1)[1].lower() in config.ALLOWED_EXTENSIONS
def save_document(file, subfolder):
if not is_file_allowed(file):
raise ValueError("File type not allowed. Only images are permitted.")
if len(file.read()) > config.MAX_FILE_SIZE:
raise ValueError("File size exceeds the limit of 5 MB.")
file.seek(0)
upload_folder = os.path.join(UPLOAD_ROOT, subfolder)
os.makedirs(upload_folder, exist_ok=True)
filename = secure_filename(f"{uuid.uuid4().hex}.{file.filename.rsplit('.', 1)[1].lower()}")
filepath = os.path.join(upload_folder, filename)
file.save(filepath)
return filename