-
Notifications
You must be signed in to change notification settings - Fork 9
Description
When working on storing an excel file:
i.e.
https://cwms-data.usace.army.mil/cwms-data/blobs/24HOURDATA.XLSX?office=SWT
I was running into an issue where if I attempted to store raw bytes and have it encode this it would fail to determine the encoding.
I.e.
cwms-python/cwms/catalog/blobs.py
Line 93 in c930742
if "value" in data and not is_base64(data["value"]):
Attempts to check if it is already base64 encoded for backwards compat
then this part encodes it for a comparison
cwms-python/cwms/utils/checks.py
Lines 7 to 8 in c930742
decoded = base64.b64decode(s, validate=True) return base64.b64encode(decoded).decode("utf-8") == s
But if you encode something that is not encoded already it will result in a failed check.
I'm not sure how to better check the encoding
But perhaps at a minimum we could check if bytes vs string are sent in:
I.e.
value = data["value"]
if isinstance(value, bytes):
data["value"] = base64.b64encode(value).decode("utf-8")
elif isinstance(value, str):
data["value"] = base64.b64encode(value.encode("utf-8")).decode("utf-8")
else:
raise TypeError("Blob 'value' must be bytes or string")then within the string check to see if it's already a base64 encoded string (perhaps we can do this with headers instead of attempting a re-encode?)
Wanted to discuss options before I submitted a PR to fix!