Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
280 lines (227 loc) · 9.34 KB
/
Copy pathdatabase.py
File metadata and controls
280 lines (227 loc) · 9.34 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
"""
══════════════════════════════════════════════════════════════════════════════
MENACRAFT TRUSTCHECK — Supabase Persistence Layer (database.py)
══════════════════════════════════════════════════════════════════════════════
Handles all Supabase operations:
- Client initialization
- Analysis results storage & retrieval
- File upload to Supabase Storage
- Report history
Tables expected in Supabase:
analyses → stores every analysis run + JSON results
reports → stores generated PDF certificate metadata
Storage buckets:
uploads → original uploaded files
reports → generated PDF certificates
heatmaps → heatmap overlay images
══════════════════════════════════════════════════════════════════════════════
"""
import os
import json
import logging
import datetime
from typing import Optional, Dict, Any, List
from dotenv import load_dotenv
load_dotenv()
logger = logging.getLogger("trustcheck.database")
# ─────────────────────────────────────────────────────────────
# SUPABASE CLIENT INIT
# ─────────────────────────────────────────────────────────────
_supabase_client = None
def get_supabase():
"""
Lazy-initialize and return the Supabase client.
Returns None if credentials are not configured (graceful degradation).
"""
global _supabase_client
if _supabase_client is not None:
return _supabase_client
url = os.environ.get("SUPABASE_URL", "")
key = os.environ.get("SUPABASE_KEY", "")
if not url or not key:
logger.warning(
"SUPABASE_URL or SUPABASE_KEY not set. "
"Database persistence is disabled — results will be returned but not stored."
)
return None
try:
from supabase import create_client
_supabase_client = create_client(url, key)
logger.info("Supabase client connected successfully.")
return _supabase_client
except Exception as e:
logger.error(f"Failed to initialize Supabase client: {e}")
return None
def is_connected() -> bool:
"""Check if Supabase is available."""
return get_supabase() is not None
# ─────────────────────────────────────────────────────────────
# ANALYSIS RESULTS — CRUD
# ─────────────────────────────────────────────────────────────
def save_analysis(
file_hash: str,
filename: str,
analysis_type: str,
results: Dict[str, Any],
) -> Optional[Dict]:
"""
Persist an analysis result to the `analyses` table.
Args:
file_hash: SHA-256 hash of the uploaded file
filename: Original filename
analysis_type: "image" | "video" | "batch"
results: Full JSON results dict from the analysis pipeline
Returns:
The inserted row dict, or None if Supabase is unavailable.
"""
sb = get_supabase()
if not sb:
return None
# Strip non-serialisable bytes from results before storing
clean_results = _strip_bytes(results)
row = {
"file_hash": file_hash,
"filename": filename,
"analysis_type": analysis_type,
"results": json.dumps(clean_results),
"created_at": datetime.datetime.utcnow().isoformat(),
}
try:
response = sb.table("analyses").insert(row).execute()
if response.data:
logger.info(f"Analysis saved: {file_hash[:12]}... ({analysis_type})")
return response.data[0]
except Exception as e:
logger.error(f"Failed to save analysis: {e}")
return None
def get_analysis(file_hash: str) -> Optional[Dict]:
"""Retrieve the most recent analysis for a given file hash."""
sb = get_supabase()
if not sb:
return None
try:
response = (
sb.table("analyses")
.select("*")
.eq("file_hash", file_hash)
.order("created_at", desc=True)
.limit(1)
.execute()
)
if response.data:
row = response.data[0]
row["results"] = json.loads(row["results"])
return row
except Exception as e:
logger.error(f"Failed to retrieve analysis: {e}")
return None
def get_recent_analyses(limit: int = 20) -> List[Dict]:
"""Retrieve recent analyses for dashboard display."""
sb = get_supabase()
if not sb:
return []
try:
response = (
sb.table("analyses")
.select("id, file_hash, filename, analysis_type, created_at")
.order("created_at", desc=True)
.limit(limit)
.execute()
)
return response.data or []
except Exception as e:
logger.error(f"Failed to retrieve recent analyses: {e}")
return []
# ─────────────────────────────────────────────────────────────
# SUPABASE STORAGE — FILE UPLOADS
# ─────────────────────────────────────────────────────────────
def upload_file(
bucket: str,
path: str,
file_bytes: bytes,
content_type: str = "application/octet-stream",
) -> Optional[str]:
"""
Upload a file to Supabase Storage.
Args:
bucket: Storage bucket name ("uploads", "reports", "heatmaps")
path: File path within the bucket (e.g., "abc123/image.jpg")
file_bytes: Raw file bytes
content_type: MIME type
Returns:
Public URL of the uploaded file, or None.
"""
sb = get_supabase()
if not sb:
return None
try:
sb.storage.from_(bucket).upload(
path,
file_bytes,
file_options={"content-type": content_type},
)
public_url = sb.storage.from_(bucket).get_public_url(path)
logger.info(f"File uploaded to {bucket}/{path}")
return public_url
except Exception as e:
logger.error(f"Storage upload failed: {e}")
return None
def get_file_url(bucket: str, path: str) -> Optional[str]:
"""Get the public URL for a file in Supabase Storage."""
sb = get_supabase()
if not sb:
return None
try:
return sb.storage.from_(bucket).get_public_url(path)
except Exception as e:
logger.error(f"Failed to get file URL: {e}")
return None
# ─────────────────────────────────────────────────────────────
# REPORT STORAGE
# ─────────────────────────────────────────────────────────────
def save_report(
file_hash: str,
filename: str,
pdf_bytes: bytes,
) -> Optional[Dict]:
"""
Save a generated PDF report to Supabase Storage + record in DB.
Returns dict with: id, file_hash, report_url
"""
sb = get_supabase()
if not sb:
return None
# Upload PDF to storage
storage_path = f"{file_hash[:16]}/{filename}.pdf"
report_url = upload_file("reports", storage_path, pdf_bytes, "application/pdf")
# Record in DB
row = {
"file_hash": file_hash,
"filename": filename,
"report_url": report_url or "",
"created_at": datetime.datetime.utcnow().isoformat(),
}
try:
response = sb.table("reports").insert(row).execute()
if response.data:
logger.info(f"Report saved: {file_hash[:12]}...")
return response.data[0]
except Exception as e:
logger.error(f"Failed to save report: {e}")
return None
# ─────────────────────────────────────────────────────────────
# HELPERS
# ─────────────────────────────────────────────────────────────
def _strip_bytes(obj):
"""
Recursively strip bytes values from a dict/list so it's JSON-serialisable.
Bytes are replaced with a placeholder string.
"""
if isinstance(obj, dict):
return {k: _strip_bytes(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [_strip_bytes(item) for item in obj]
elif isinstance(obj, bytes):
return f"[binary {len(obj)} bytes]"
else:
return obj