Skip to content

ingesting base64 encoded content #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import time
from typing import Dict


DEFAULT_ENCODING = "utf-8"

KEY_CLIENT_ID = "client_identifier"
Expand Down Expand Up @@ -34,10 +33,12 @@ def __init__(
timestamp: int = None,
client_identifier: str = None,
):
if content is not None:
if content is None:
self._base_64_content = ""
elif not self.is_base64_encoded(content=content):
self._base_64_content = self._base64_encode_str(content=content)
else:
self._base_64_content = ""
self._base_64_content = content
if id is None:
self.id = hashlib.sha1(
self._base_64_content.encode(DEFAULT_ENCODING)
Expand All @@ -58,8 +59,8 @@ def dict(self) -> Dict:

return {
"id": self.id,
"content": self._base64_decode_content(),
"isBase64Encoded": False,
"content": self._base_64_content,
"isBase64Encoded": True,
"encoding": DEFAULT_ENCODING,
"created_time_epoch": self._unix_timestamp,
"metadata": self._metadata,
Expand All @@ -76,3 +77,13 @@ def _base64_decode_content(self, encoding: str = DEFAULT_ENCODING) -> str:
def _base64_encode_str(self, content: str, encoding: str = DEFAULT_ENCODING) -> str:
b = base64.b64encode(bytes(content, encoding))
return b.decode(encoding)

def is_base64_encoded(self, content: str, encoding: str = DEFAULT_ENCODING) -> bool:
try:
decoded_content = base64.b64decode(content).decode(encoding)
return (
base64.b64encode(decoded_content.encode(encoding)).decode(encoding)
== content
)
except Exception:
return False
2 changes: 1 addition & 1 deletion src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def get_handler(event, context, db: DB, is_web_browser: bool = False):
)
return {
"statusCode": 200,
"isBase64Encoded": False,
"isBase64Encoded": paste.is_base64_encoded(content=content),
"headers": {
"Content-Type": "text/html; charset=utf-8",
"Access-Control-Allow-Origin": "*",
Expand Down
Loading