Skip to content

Commit deaa3f7

Browse files
Refactor PasteDataAware to use encoded_content and plain_content properties. Introduce utility functions for Base64 encoding and decoding.
1 parent 85903c0 commit deaa3f7

File tree

4 files changed

+68
-38
lines changed

4 files changed

+68
-38
lines changed

backend/app/models.py

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import base64
21
import time
32
from typing import Optional
43

54
from pydantic import BaseModel, Field
65

6+
from app.utils import from_base64, is_base64, to_base64
7+
78

89
class PasteInputModel(BaseModel):
910
"""
@@ -38,59 +39,39 @@ class PasteDataAware:
3839
content: str
3940
client_id: str
4041
created_at: int
41-
content_encoding: str = Field(
42-
default="utf-8",
43-
description="Encoding used for the content. Default is 'utf-8'.",
44-
)
4542

4643
def __init__(
4744
self,
4845
content: str,
4946
client_id: str,
5047
created_at: Optional[int] = None,
5148
paste_id: str = None,
52-
content_encoding: str = "utf-8",
5349
):
5450
self.paste_id = paste_id
55-
self._content = (
56-
content
57-
if self._is_base64(content)
58-
else base64.b64encode(content.encode(content_encoding)).decode(
59-
content_encoding
60-
)
61-
)
51+
self._content = content if is_base64(content) else to_base64(content)
6252
self.client_id = client_id
6353
self.created_at = int(time.time()) if created_at is None else created_at
64-
self.content_encoding = content_encoding
6554

66-
def _is_base64(self, s: str) -> bool:
55+
@property
56+
def encoded_content(self) -> str:
6757
"""
68-
Check if a string is valid Base64.
69-
70-
Args:
71-
s (str): The string to check.
58+
Get the decoded content of the paste.
7259
7360
Returns:
74-
bool: True if the string is valid Base64, False otherwise.
61+
str: The decoded content of the paste.
7562
"""
76-
try:
77-
# Decode and re-encode to verify Base64 validity
78-
return base64.b64encode(base64.b64decode(s)).decode("utf-8") == s
79-
except Exception:
80-
return False
63+
return self._content if is_base64(self._content) else to_base64(self._content)
8164

82-
def plain_content(self):
65+
@property
66+
def plain_content(self) -> str:
8367
"""
84-
Returns the content of the paste in plain text format.
68+
Get the plain content of the paste.
8569
70+
Returns:
71+
str: The plain content of the paste.
8672
"""
87-
return base64.b64decode(self._content.encode(self.content_encoding)).decode(
88-
self.content_encoding
73+
return (
74+
self._content
75+
if not is_base64(self._content)
76+
else from_base64(self._content)
8977
)
90-
91-
def base64_content(self):
92-
"""
93-
Returns the content of the paste in base64 format.
94-
95-
"""
96-
return self._content

backend/app/routes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async def get_paste(id: str, request: Request, db_client=Depends(get_db_client))
3737
is_web_browser = "Mozilla" in user_agent or "AppleWebKit" in user_agent
3838

3939
paste_model = PasteModel(
40-
content=paste.plain_content(),
40+
content=paste.plain_content,
4141
paste_id=paste.paste_id,
4242
workspace=paste.client_id,
4343
)

backend/app/services/db_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ async def insert_paste(self, paste: PasteDataAware) -> str:
9797
RETURNING id, paste_id;
9898
"""
9999
result = await conn.fetchrow(
100-
insert_query, paste.base64_content(), paste.client_id, paste.created_at
100+
insert_query, paste.encoded_content, paste.client_id, paste.created_at
101101
)
102102

103103
return result["paste_id"]

backend/app/utils.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
DEFAULT_ENCODING = "utf-8"
2+
import base64
3+
4+
DEFAULT_ENCODING = "utf-8"
5+
6+
7+
def is_base64(s: str, encoding: str = DEFAULT_ENCODING) -> bool:
8+
"""
9+
Check if a string is valid Base64.
10+
11+
Args:
12+
s (str): The string to check.
13+
14+
Returns:
15+
bool: True if the string is valid Base64, False otherwise.
16+
"""
17+
try:
18+
# Decode and re-encode to verify Base64 validity
19+
return base64.b64encode(base64.b64decode(s)).decode(encoding=encoding) == s
20+
except Exception:
21+
return False
22+
23+
24+
def to_base64(s: str, encoding: str = DEFAULT_ENCODING) -> str:
25+
"""
26+
Convert a string to Base64.
27+
28+
Args:
29+
s (str): The string to convert.
30+
encoding (str): The encoding to use. Defaults to 'utf-8'.
31+
32+
Returns:
33+
str: The Base64 encoded string.
34+
"""
35+
return base64.b64encode(s.encode(encoding=encoding)).decode(encoding=encoding)
36+
37+
38+
def from_base64(s: str, encoding: str = DEFAULT_ENCODING) -> str:
39+
"""
40+
Convert a Base64 string to its original form.
41+
42+
Args:
43+
s (str): The Base64 string to convert.
44+
encoding (str): The encoding to use. Defaults to 'utf-8'.
45+
46+
Returns:
47+
str: The decoded string.
48+
"""
49+
return base64.b64decode(s.encode(encoding=encoding)).decode(encoding=encoding)

0 commit comments

Comments
 (0)