Skip to content

Commit

Permalink
add checksum timestamp changes
Browse files Browse the repository at this point in the history
  • Loading branch information
brynnz22 committed Nov 8, 2023
1 parent 4561d85 commit 2af9b30
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
5 changes: 4 additions & 1 deletion nmdc_runtime/api/core/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ def hash_from_str(s: str, algo="sha256") -> str:
return getattr(hashlib, algo)(s.encode("utf-8")).hexdigest()


def sha256hash_from_file(file_path: str):
def sha256hash_from_file(file_path: str, timestamp: str):
# https://stackoverflow.com/a/55542529
h = hashlib.sha256()

timestamp_bytes = timestamp.encode('utf-8')
h.update(timestamp_bytes)

with open(file_path, "rb") as file:
while True:
# Reading is buffered, so we can read smaller chunks.
Expand Down
1 change: 1 addition & 0 deletions nmdc_runtime/api/endpoints/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ def persist_content_and_get_drs_object(
),
"access_methods": [{"access_id": drs_id}],
},
timestamp= datetime.now(tz=ZoneInfo('America/Los_Angeles')).isoformat(timespec='minutes')
)
)
self_uri = f"drs://{HOSTNAME_EXTERNAL}/{drs_id}"
Expand Down
4 changes: 2 additions & 2 deletions nmdc_runtime/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def put_object(filepath, url, mime_type=None):
return requests.put(url, data=f, headers={"Content-Type": mime_type})


def drs_metadata_for(filepath, base=None):
def drs_metadata_for(filepath, base=None, timestamp=None):
"""given file path, get drs metadata
required: size, created_time, and at least one checksum.
Expand All @@ -96,7 +96,7 @@ def drs_metadata_for(filepath, base=None):
)
if "checksums" not in base:
base["checksums"] = [
{"type": "sha256", "checksum": sha256hash_from_file(filepath)}
{"type": "sha256", "checksum": sha256hash_from_file(filepath, timestamp)}
]
if "mime_type" not in base:
base["mime_type"] = mimetypes.guess_type(filepath)[0]
Expand Down

0 comments on commit 2af9b30

Please sign in to comment.