-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfile_handler.py
More file actions
395 lines (306 loc) · 14.2 KB
/
file_handler.py
File metadata and controls
395 lines (306 loc) · 14.2 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
from __future__ import annotations
import hashlib
from dataclasses import dataclass
from io import BytesIO
from pathlib import Path
from typing import TYPE_CHECKING, BinaryIO, cast, overload
import anyio
import httpx
from .exceptions import AuthenticationError, NodeNotFoundError, ServerNotReachableError
if TYPE_CHECKING:
from .client import InfrahubClient, InfrahubClientSync
_SHA1_CHUNK_BYTES = 64 * 1024
def sha1_of_source(source: bytes | Path | BinaryIO) -> str:
"""Compute the SHA-1 hex digest of an upload/download source.
Accepts the same shapes as :meth:`FileHandlerBase.prepare_upload` so
callers can compare local content against a server-stored checksum
without materialising the full file in memory.
Args:
source: The content to hash. ``bytes`` are hashed in one shot.
A ``Path`` is read in 64 KiB chunks. A ``BinaryIO`` is read
from its current position, then rewound so downstream
callers can re-read it.
Returns:
Lowercase SHA-1 hex digest, matching the algorithm Infrahub
stores in ``CoreFileObject.checksum``.
Raises:
TypeError: If ``source`` is not one of the supported types.
"""
hasher = hashlib.sha1(usedforsecurity=False)
if isinstance(source, bytes):
hasher.update(source)
return hasher.hexdigest()
if isinstance(source, Path):
with source.open("rb") as fh:
while chunk := fh.read(_SHA1_CHUNK_BYTES):
hasher.update(chunk)
return hasher.hexdigest()
if hasattr(source, "read") and hasattr(source, "seek"):
start = source.tell()
try:
while chunk := source.read(_SHA1_CHUNK_BYTES):
hasher.update(chunk)
finally:
source.seek(start)
return hasher.hexdigest()
raise TypeError(f"sha1_of_source expects bytes, Path, or BinaryIO; got {type(source).__name__}")
@dataclass
class PreparedFile:
file_object: BinaryIO | None
filename: str | None
should_close: bool
class FileHandlerBase:
"""Base class for file handling operations.
Provides common functionality for both async and sync file handlers, including upload preparation and error handling.
"""
@staticmethod
async def prepare_upload(content: bytes | Path | BinaryIO | None, name: str | None = None) -> PreparedFile:
"""Prepare file content for upload (async version).
Converts various content types to a consistent BinaryIO interface for streaming uploads.
For Path inputs, opens the file handle in a thread pool to avoid blocking the event loop.
The actual file reading is streamed by httpx during the HTTP request.
Args:
content: The file content as bytes, a Path to a file, or a file-like object.
Can be None if no file is set.
name: Optional filename. If not provided and content is a Path,
the filename will be derived from the path.
Returns:
A PreparedFile containing the file object, filename, and whether it should be closed.
"""
if content is None:
return PreparedFile(file_object=None, filename=None, should_close=False)
if name is None and isinstance(content, Path):
name = content.name
filename = name or "uploaded_file"
if isinstance(content, bytes):
return PreparedFile(file_object=BytesIO(content), filename=filename, should_close=False)
if isinstance(content, Path):
# Open file in thread pool to avoid blocking the event loop
# Returns a sync file handle that httpx can stream from in chunks
file_obj = await anyio.to_thread.run_sync(content.open, "rb")
return PreparedFile(file_object=cast("BinaryIO", file_obj), filename=filename, should_close=True)
# At this point, content must be a BinaryIO (file-like object)
return PreparedFile(file_object=cast("BinaryIO", content), filename=filename, should_close=False)
@staticmethod
def prepare_upload_sync(content: bytes | Path | BinaryIO | None, name: str | None = None) -> PreparedFile:
"""Prepare file content for upload (sync version).
Converts various content types to a consistent BinaryIO interface for streaming uploads.
Args:
content: The file content as bytes, a Path to a file, or a file-like object.
Can be None if no file is set.
name: Optional filename. If not provided and content is a Path,
the filename will be derived from the path.
Returns:
A PreparedFile containing the file object, filename, and whether it should be closed.
"""
if content is None:
return PreparedFile(file_object=None, filename=None, should_close=False)
if name is None and isinstance(content, Path):
name = content.name
filename = name or "uploaded_file"
if isinstance(content, bytes):
return PreparedFile(file_object=BytesIO(content), filename=filename, should_close=False)
if isinstance(content, Path):
return PreparedFile(file_object=content.open("rb"), filename=filename, should_close=True)
# At this point, content must be a BinaryIO (file-like object)
return PreparedFile(file_object=cast("BinaryIO", content), filename=filename, should_close=False)
@staticmethod
def handle_error_response(exc: httpx.HTTPStatusError) -> None:
"""Handle HTTP error responses for file operations.
Args:
exc: The HTTP status error from httpx.
Raises:
AuthenticationError: If authentication fails (401/403).
NodeNotFoundError: If the file/node is not found (404).
httpx.HTTPStatusError: For other HTTP errors.
"""
if exc.response.status_code in {401, 403}:
response = exc.response.json()
errors = response.get("errors", [])
messages = [error.get("message") for error in errors]
raise AuthenticationError(" | ".join(messages)) from exc
if exc.response.status_code == 404:
response = exc.response.json()
detail = response.get("detail", "File not found")
raise NodeNotFoundError(node_type="FileObject", identifier=detail) from exc
raise exc
@staticmethod
def handle_response(resp: httpx.Response) -> bytes:
"""Handle the HTTP response and return file content as bytes.
Args:
resp: The HTTP response from httpx.
Returns:
The file content as bytes.
Raises:
AuthenticationError: If authentication fails.
NodeNotFoundError: If the file is not found.
"""
try:
resp.raise_for_status()
except httpx.HTTPStatusError as exc:
FileHandlerBase.handle_error_response(exc=exc)
return resp.content
class FileHandler(FileHandlerBase):
"""Async file handler for download operations.
Handles file downloads with support for streaming to disk
for memory-efficient handling of large files.
"""
def __init__(self, client: InfrahubClient) -> None:
"""Initialize the async file handler.
Args:
client: The async Infrahub client instance.
"""
self._client = client
def _build_url(self, node_id: str, branch: str | None) -> str:
"""Build the download URL for a file.
Args:
node_id: The ID of the FileObject node.
branch: Optional branch name.
Returns:
The complete URL for downloading the file.
"""
url = f"{self._client.address}/api/storage/files/{node_id}"
if branch:
url = f"{url}?branch={branch}"
return url
@overload
async def download(self, node_id: str, branch: str | None) -> bytes: ...
@overload
async def download(self, node_id: str, branch: str | None, dest: Path) -> int: ...
@overload
async def download(self, node_id: str, branch: str | None, dest: None) -> bytes: ...
async def download(self, node_id: str, branch: str | None, dest: Path | None = None) -> bytes | int:
"""Download file content from a FileObject node.
Args:
node_id: The ID of the FileObject node.
branch: Optional branch name. Uses client default if not provided.
dest: Optional destination path. If provided, streams to disk.
Returns:
If dest is None: The file content as bytes.
If dest is provided: The number of bytes written.
Raises:
ServerNotReachableError: If the server is not reachable.
AuthenticationError: If authentication fails.
NodeNotFoundError: If the node/file is not found.
"""
effective_branch = branch or self._client.default_branch
url = self._build_url(node_id=node_id, branch=effective_branch)
if dest is not None:
return await self._stream_to_file(url=url, dest=dest)
try:
resp = await self._client._get(url=url)
except ServerNotReachableError:
self._client.log.error(f"Unable to connect to {self._client.address}")
raise
return self.handle_response(resp=resp)
async def _stream_to_file(self, url: str, dest: Path) -> int:
"""Stream download directly to a file without loading into memory.
Args:
url: The URL to download from.
dest: The destination path to write to.
Returns:
The number of bytes written to the file.
Raises:
ServerNotReachableError: If the server is not reachable.
AuthenticationError: If authentication fails.
NodeNotFoundError: If the file is not found.
"""
try:
async with self._client._get_streaming(url=url) as resp:
try:
resp.raise_for_status()
except httpx.HTTPStatusError as exc:
# Need to read the response body for error details
await resp.aread()
self.handle_error_response(exc=exc)
bytes_written = 0
async with await anyio.Path(dest).open("wb") as f:
async for chunk in resp.aiter_bytes(chunk_size=65536):
await f.write(chunk)
bytes_written += len(chunk)
return bytes_written
except ServerNotReachableError:
self._client.log.error(f"Unable to connect to {self._client.address}")
raise
class FileHandlerSync(FileHandlerBase):
"""Sync file handler for download operations.
Handles file downloads with support for streaming to disk
for memory-efficient handling of large files.
"""
def __init__(self, client: InfrahubClientSync) -> None:
"""Initialize the sync file handler.
Args:
client: The sync Infrahub client instance.
"""
self._client = client
def _build_url(self, node_id: str, branch: str | None) -> str:
"""Build the download URL for a file.
Args:
node_id: The ID of the FileObject node.
branch: Optional branch name.
Returns:
The complete URL for downloading the file.
"""
url = f"{self._client.address}/api/storage/files/{node_id}"
if branch:
url = f"{url}?branch={branch}"
return url
@overload
def download(self, node_id: str, branch: str | None) -> bytes: ...
@overload
def download(self, node_id: str, branch: str | None, dest: Path) -> int: ...
@overload
def download(self, node_id: str, branch: str | None, dest: None) -> bytes: ...
def download(self, node_id: str, branch: str | None, dest: Path | None = None) -> bytes | int:
"""Download file content from a FileObject node.
Args:
node_id: The ID of the FileObject node.
branch: Optional branch name. Uses client default if not provided.
dest: Optional destination path. If provided, streams to disk.
Returns:
If dest is None: The file content as bytes.
If dest is provided: The number of bytes written.
Raises:
ServerNotReachableError: If the server is not reachable.
AuthenticationError: If authentication fails.
NodeNotFoundError: If the node/file is not found.
"""
effective_branch = branch or self._client.default_branch
url = self._build_url(node_id=node_id, branch=effective_branch)
if dest is not None:
return self._stream_to_file(url=url, dest=dest)
try:
resp = self._client._get(url=url)
except ServerNotReachableError:
self._client.log.error(f"Unable to connect to {self._client.address}")
raise
return self.handle_response(resp=resp)
def _stream_to_file(self, url: str, dest: Path) -> int:
"""Stream download directly to a file without loading into memory.
Args:
url: The URL to download from.
dest: The destination path to write to.
Returns:
The number of bytes written to the file.
Raises:
ServerNotReachableError: If the server is not reachable.
AuthenticationError: If authentication fails.
NodeNotFoundError: If the file is not found.
"""
try:
with self._client._get_streaming(url=url) as resp:
try:
resp.raise_for_status()
except httpx.HTTPStatusError as exc:
# Need to read the response body for error details
resp.read()
self.handle_error_response(exc=exc)
bytes_written = 0
with dest.open("wb") as f:
for chunk in resp.iter_bytes(chunk_size=65536):
f.write(chunk)
bytes_written += len(chunk)
return bytes_written
except ServerNotReachableError:
self._client.log.error(f"Unable to connect to {self._client.address}")
raise