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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
import io
import logging
from datetime import datetime
from io import BytesIO
from typing import BinaryIO, Optional
from uuid import UUID
from zipfile import ZipFile
import asyncpg
from fastapi import HTTPException
from core.base import Handler, R2RException
from .base import PostgresConnectionManager
logger = logging.getLogger()
class PostgresFilesHandler(Handler):
"""PostgreSQL implementation of the FileHandler."""
TABLE_NAME = "files"
connection_manager: PostgresConnectionManager
async def create_tables(self) -> None:
"""Create the necessary tables for file storage."""
query = f"""
CREATE TABLE IF NOT EXISTS {self._get_table_name(PostgresFilesHandler.TABLE_NAME)} (
document_id UUID PRIMARY KEY,
name TEXT NOT NULL,
oid OID NOT NULL,
size BIGINT NOT NULL,
type TEXT,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Create trigger for updating the updated_at timestamp
CREATE OR REPLACE FUNCTION {self.project_name}.update_files_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = CURRENT_TIMESTAMP;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS update_files_updated_at
ON {self._get_table_name(PostgresFilesHandler.TABLE_NAME)};
CREATE TRIGGER update_files_updated_at
BEFORE UPDATE ON {self._get_table_name(PostgresFilesHandler.TABLE_NAME)}
FOR EACH ROW
EXECUTE FUNCTION {self.project_name}.update_files_updated_at();
"""
await self.connection_manager.execute_query(query)
async def upsert_file(
self,
document_id: UUID,
file_name: str,
file_oid: int,
file_size: int,
file_type: Optional[str] = None,
) -> None:
"""Add or update a file entry in storage."""
query = f"""
INSERT INTO {self._get_table_name(PostgresFilesHandler.TABLE_NAME)}
(document_id, name, oid, size, type)
VALUES ($1, $2, $3, $4, $5)
ON CONFLICT (document_id) DO UPDATE SET
name = EXCLUDED.name,
oid = EXCLUDED.oid,
size = EXCLUDED.size,
type = EXCLUDED.type,
updated_at = NOW();
"""
await self.connection_manager.execute_query(
query, [document_id, file_name, file_oid, file_size, file_type]
)
async def store_file(
self,
document_id: UUID,
file_name: str,
file_content: io.BytesIO,
file_type: Optional[str] = None,
) -> None:
"""Store a new file in the database."""
size = file_content.getbuffer().nbytes
async with (
self.connection_manager.pool.get_connection() as conn # type: ignore
):
async with conn.transaction():
oid = await conn.fetchval("SELECT lo_create(0)")
await self._write_lobject(conn, oid, file_content)
await self.upsert_file(
document_id, file_name, oid, size, file_type
)
async def _write_lobject(
self, conn, oid: int, file_content: io.BytesIO
) -> None:
"""Write content to a large object."""
lobject = await conn.fetchval("SELECT lo_open($1, $2)", oid, 0x20000)
try:
chunk_size = 8192 # 8 KB chunks
while True:
if chunk := file_content.read(chunk_size):
await conn.execute(
"SELECT lowrite($1, $2)", lobject, chunk
)
else:
break
await conn.execute("SELECT lo_close($1)", lobject)
except Exception as e:
await conn.execute("SELECT lo_unlink($1)", oid)
raise HTTPException(
status_code=500,
detail=f"Failed to write to large object: {e}",
) from e
async def retrieve_file(
self, document_id: UUID
) -> Optional[tuple[str, BinaryIO, int]]:
"""Retrieve a file from storage."""
query = f"""
SELECT name, oid, size
FROM {self._get_table_name(PostgresFilesHandler.TABLE_NAME)}
WHERE document_id = $1
"""
result = await self.connection_manager.fetchrow_query(
query, [document_id]
)
if not result:
raise R2RException(
status_code=404,
message=f"File for document {document_id} not found",
)
file_name, oid, size = (
result["name"],
result["oid"],
result["size"],
)
async with self.connection_manager.pool.get_connection() as conn: # type: ignore
file_content = await self._read_lobject(conn, oid)
return file_name, io.BytesIO(file_content), size
async def retrieve_files_as_zip(
self,
document_ids: Optional[list[UUID]] = None,
start_date: Optional[datetime] = None,
end_date: Optional[datetime] = None,
) -> tuple[str, BinaryIO, int]:
"""Retrieve multiple files and return them as a zip file."""
query = f"""
SELECT document_id, name, oid, size
FROM {self._get_table_name(PostgresFilesHandler.TABLE_NAME)}
WHERE 1=1
"""
params: list = []
if document_ids:
query += f" AND document_id = ANY(${len(params) + 1})"
params.append([str(doc_id) for doc_id in document_ids])
if start_date:
query += f" AND created_at >= ${len(params) + 1}"
params.append(start_date)
if end_date:
query += f" AND created_at <= ${len(params) + 1}"
params.append(end_date)
query += " ORDER BY created_at DESC"
results = await self.connection_manager.fetch_query(query, params)
if not results:
raise R2RException(
status_code=404,
message="No files found matching the specified criteria",
)
zip_buffer = BytesIO()
total_size = 0
async with self.connection_manager.pool.get_connection() as conn: # type: ignore
with ZipFile(zip_buffer, "w") as zip_file:
for record in results:
file_content = await self._read_lobject(
conn, record["oid"]
)
zip_file.writestr(record["name"], file_content)
total_size += record["size"]
zip_buffer.seek(0)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
zip_filename = f"files_export_{timestamp}.zip"
return zip_filename, zip_buffer, zip_buffer.getbuffer().nbytes
async def _read_lobject(self, conn, oid: int) -> bytes:
"""Read content from a large object."""
file_data = io.BytesIO()
chunk_size = 8192
async with conn.transaction():
try:
lo_exists = await conn.fetchval(
"SELECT EXISTS(SELECT 1 FROM pg_catalog.pg_largeobject_metadata WHERE oid = $1);",
oid,
)
if not lo_exists:
raise R2RException(
status_code=404,
message=f"Large object {oid} not found.",
)
lobject = await conn.fetchval(
"SELECT lo_open($1, 262144)", oid
)
if lobject is None:
raise R2RException(
status_code=404,
message=f"Failed to open large object {oid}.",
)
while True:
chunk = await conn.fetchval(
"SELECT loread($1, $2)", lobject, chunk_size
)
if not chunk:
break
file_data.write(chunk)
except asyncpg.exceptions.UndefinedObjectError:
raise R2RException(
status_code=404,
message=f"Failed to read large object {oid}",
) from None
finally:
await conn.execute("SELECT lo_close($1)", lobject)
return file_data.getvalue()
async def delete_file(self, document_id: UUID) -> bool:
"""Delete a file from storage."""
query = f"""
SELECT oid FROM {self._get_table_name(PostgresFilesHandler.TABLE_NAME)}
WHERE document_id = $1
"""
async with self.connection_manager.pool.get_connection() as conn: # type: ignore
async with conn.transaction():
oid = await conn.fetchval(query, document_id)
if not oid:
raise R2RException(
status_code=404,
message=f"File for document {document_id} not found",
)
await self._delete_lobject(conn, oid)
delete_query = f"""
DELETE FROM {self._get_table_name(PostgresFilesHandler.TABLE_NAME)}
WHERE document_id = $1
"""
await conn.execute(delete_query, document_id)
return True
async def _delete_lobject(self, conn, oid: int) -> None:
"""Delete a large object."""
await conn.execute("SELECT lo_unlink($1)", oid)
async def get_files_overview(
self,
offset: int,
limit: int,
filter_document_ids: Optional[list[UUID]] = None,
filter_file_names: Optional[list[str]] = None,
) -> list[dict]:
"""Get an overview of stored files."""
conditions = []
params: list[str | list[str] | int] = []
query = f"""
SELECT document_id, name, oid, size, type, created_at, updated_at
FROM {self._get_table_name(PostgresFilesHandler.TABLE_NAME)}
"""
if filter_document_ids:
conditions.append(f"document_id = ANY(${len(params) + 1})")
params.append([str(doc_id) for doc_id in filter_document_ids])
if filter_file_names:
conditions.append(f"name = ANY(${len(params) + 1})")
params.append(filter_file_names)
if conditions:
query += " WHERE " + " AND ".join(conditions)
query += f" ORDER BY created_at DESC OFFSET ${len(params) + 1} LIMIT ${len(params) + 2}"
params.extend([offset, limit])
results = await self.connection_manager.fetch_query(query, params)
if not results:
raise R2RException(
status_code=404,
message="No files found with the given filters",
)
return [
{
"document_id": row["document_id"],
"file_name": row["name"],
"file_oid": row["oid"],
"file_size": row["size"],
"file_type": row["type"],
"created_at": row["created_at"],
"updated_at": row["updated_at"],
}
for row in results
]
|