-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdependencies.py
184 lines (153 loc) · 6.24 KB
/
dependencies.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# dependancies.py
import os
import aiosmtplib
import aiofiles
import aiohttp
import tempfile
import shutil
import mimetypes
import asyncio
from fastapi import HTTPException, Depends
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
from typing import Optional
# Environment variables
ACCOUNT_EMAIL = os.getenv("ACCOUNT_EMAIL")
ACCOUNT_PASSWORD = os.getenv("ACCOUNT_PASSWORD")
ACCOUNT_SMTP_SERVER = os.getenv("ACCOUNT_SMTP_SERVER")
ACCOUNT_SMTP_PORT = os.getenv("ACCOUNT_SMTP_PORT")
ACCOUNT_REPLY_TO = os.getenv("ACCOUNT_REPLY_TO")
FROM_NAME = os.getenv("FROM_NAME", "Your Friendly AI")
SIGNATURE_PATH = "/app/sig/signature.html"
# Validate environment variables
if not all([ACCOUNT_EMAIL, ACCOUNT_PASSWORD, ACCOUNT_SMTP_SERVER, ACCOUNT_SMTP_PORT]):
raise HTTPException(status_code=500, detail="SMTP configuration is incomplete")
ACCOUNT_SMTP_PORT = int(ACCOUNT_SMTP_PORT)
ALLOWED_FILE_TYPES = {
".zip",
".txt",
".docx",
".png",
".webp",
".jpg",
".jpeg",
".pdf",
".rtf",
}
MAX_ATTACHMENT_SIZE = 20 * 1024 * 1024 # 20MB
async def fetch_file(session, url, temp_dir) -> str:
filename = url.split("/")[-1]
_, file_extension = os.path.splitext(filename)
# Check if the file type is allowed
if file_extension.lower() not in ALLOWED_FILE_TYPES:
raise HTTPException(status_code=400, detail=f"File type {file_extension} is not allowed")
# Set timeout for requests
timeout = aiohttp.ClientTimeout(total=10)
async with session.get(url, timeout=timeout) as response:
if response.status != 200:
print(f"Failed to download file from {url} with status {response.status}")
raise HTTPException(
status_code=response.status,
detail=f"Failed to download file from {url}",
)
file_path = os.path.join(temp_dir, filename)
async with aiofiles.open(file_path, "wb") as out_file:
content = await response.read()
await out_file.write(content)
return file_path
async def send_email(
to_address: str,
subject: str,
body: str,
file_url: Optional[str] = None,
) -> None:
msg = MIMEMultipart()
# Convert to_address string into a list of email addresses
to_addresses = [address.strip() for address in to_address.split(",")]
msg["From"] = f"{FROM_NAME} <{ACCOUNT_EMAIL}>"
msg["To"] = ", ".join(to_addresses)
msg["Subject"] = subject
msg["Reply-To"] = ACCOUNT_REPLY_TO
# Add the email body and optional signature
signature = ""
if os.path.exists(SIGNATURE_PATH) and os.access(SIGNATURE_PATH, os.R_OK):
async with aiofiles.open(SIGNATURE_PATH, "r") as file:
signature = await file.read()
else:
print("Signature file not found or is not readable. Proceeding without it.")
msg.attach(MIMEText(body + signature, "html"))
# Handle file attachments
if file_url:
# Convert the file_url string into a list of URLs
file_urls = [url.strip() for url in file_url.split(",")]
total_size = 0
temp_dir = tempfile.mkdtemp()
try:
async with aiohttp.ClientSession() as session:
# Download all files concurrently
file_paths = await asyncio.gather(
*(fetch_file(session, url, temp_dir) for url in file_urls)
)
for file_path in file_paths:
file_size = os.path.getsize(file_path)
if file_size + total_size > MAX_ATTACHMENT_SIZE:
raise HTTPException(
status_code=413,
detail="Total attachment size exceeds 20MB limit",
)
if file_size > MAX_ATTACHMENT_SIZE:
raise HTTPException(
status_code=413,
detail=f"Attachment {os.path.basename(file_path)} exceeds the 20MB limit",
)
total_size += file_size
mime_type, _ = mimetypes.guess_type(file_path)
main_type, sub_type = (
mime_type.split("/") if mime_type else ("application", "octet-stream")
)
part = MIMEBase(main_type, sub_type)
with open(file_path, "rb") as file:
part.set_payload(file.read())
encoders.encode_base64(part)
part.add_header(
"Content-Disposition",
f"attachment; filename={os.path.basename(file_path)}",
)
msg.attach(part)
except HTTPException as e:
print(f"HTTPException during file handling: {e.detail}")
raise
except Exception as e:
print(f"Unexpected error during file handling: {str(e)}")
raise
finally:
shutil.rmtree(temp_dir)
try:
await aiosmtplib.send(
msg,
hostname=ACCOUNT_SMTP_SERVER,
port=ACCOUNT_SMTP_PORT,
username=ACCOUNT_EMAIL,
password=ACCOUNT_PASSWORD,
start_tls=True,
)
except aiosmtplib.errors.SMTPException as e:
print(f"SMTPException: {str(e)}")
raise HTTPException(status_code=500, detail=f"SMTP server error: {str(e)}")
except Exception as e:
print(f"Unexpected error: {str(e)}")
raise HTTPException(status_code=500, detail=f"Unexpected error: {str(e)}")
async def get_api_key(
credentials: Optional[HTTPAuthorizationCredentials] = Depends(HTTPBearer(auto_error=False)),
) -> Optional[str]:
# Retrieve the API key from the environment
expected_key = os.getenv("API_KEY")
# If API_KEY is set in the environment, enforce validation
if expected_key:
if not credentials or credentials.credentials != expected_key:
raise HTTPException(status_code=403, detail="Invalid or missing API key")
# If API_KEY is not set, allow access without validation
return credentials.credentials if credentials else None