Skip to content

Commit 8c400a3

Browse files
Fix: evaluate sha256 for resource from fd (#279)
1 parent 2b6ed63 commit 8c400a3

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

karton/core/resource.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def __init__(
3939
bucket: Optional[str] = None,
4040
metadata: Optional[Dict[str, Any]] = None,
4141
sha256: Optional[str] = None,
42+
fd: Optional[IO[bytes]] = None,
4243
_uid: Optional[str] = None,
4344
_size: Optional[int] = None,
4445
_flags: Optional[List[str]] = None,
@@ -66,6 +67,16 @@ def __init__(
6667
for byte_block in iter(lambda: f.read(4096), b""):
6768
sha256_hash.update(byte_block)
6869
sha256 = sha256_hash.hexdigest()
70+
elif fd is not None:
71+
if calculate_hash:
72+
# we need to calculate the whole hash and return pos as it was
73+
sha256_hash = hashlib.sha256()
74+
last_position = fd.tell()
75+
fd.seek(0)
76+
for byte_block in iter(lambda: fd.read(4096), b""):
77+
sha256_hash.update(byte_block)
78+
sha256 = sha256_hash.hexdigest()
79+
fd.seek(last_position)
6980
elif content:
7081
if isinstance(content, str):
7182
self._content = content.encode()
@@ -190,6 +201,7 @@ def __init__(
190201
bucket=bucket,
191202
metadata=metadata,
192203
sha256=sha256,
204+
fd=fd,
193205
_uid=uid,
194206
_flags=_flags,
195207
)

0 commit comments

Comments
 (0)