Skip to content

Commit e873085

Browse files
Merge pull request #100 from socraticDevBlog/20250427-storecontentbase64
Ajout de la gestion de l'encodage du contenu dans le modèle PasteData…
2 parents 461393b + deaa3f7 commit e873085

File tree

4 files changed

+76
-11
lines changed

4 files changed

+76
-11
lines changed

backend/app/models.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
from typing import Optional
33

44
from pydantic import BaseModel, Field
5-
from app.utils import hash_value
5+
6+
from app.utils import from_base64, is_base64, to_base64
67

78

89
class PasteInputModel(BaseModel):
@@ -46,7 +47,31 @@ def __init__(
4647
created_at: Optional[int] = None,
4748
paste_id: str = None,
4849
):
49-
self.paste_id = hash_value(value=content) if paste_id is None else paste_id
50-
self.content = content
50+
self.paste_id = paste_id
51+
self._content = content if is_base64(content) else to_base64(content)
5152
self.client_id = client_id
5253
self.created_at = int(time.time()) if created_at is None else created_at
54+
55+
@property
56+
def encoded_content(self) -> str:
57+
"""
58+
Get the decoded content of the paste.
59+
60+
Returns:
61+
str: The decoded content of the paste.
62+
"""
63+
return self._content if is_base64(self._content) else to_base64(self._content)
64+
65+
@property
66+
def plain_content(self) -> str:
67+
"""
68+
Get the plain content of the paste.
69+
70+
Returns:
71+
str: The plain content of the paste.
72+
"""
73+
return (
74+
self._content
75+
if not is_base64(self._content)
76+
else from_base64(self._content)
77+
)

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.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.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: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
1-
import hashlib
1+
DEFAULT_ENCODING = "utf-8"
2+
import base64
23

4+
DEFAULT_ENCODING = "utf-8"
35

4-
def hash_value(value: str, encoding: str = "utf-8") -> str:
56

6-
value_bytes = str(value).encode(encoding=encoding)
7-
hash_object = hashlib.md5()
8-
hash_object.update(value_bytes)
9-
return hash_object.hexdigest()
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)