Skip to content

Commit 85903c0

Browse files
Ajout de la gestion de l'encodage du contenu dans le modèle PasteDataAware et mise à jour des routes pour utiliser le contenu en texte brut. Suppression de la fonction de hachage inutilisée.
1 parent 461393b commit 85903c0

File tree

4 files changed

+49
-14
lines changed

4 files changed

+49
-14
lines changed

backend/app/models.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import base64
12
import time
23
from typing import Optional
34

45
from pydantic import BaseModel, Field
5-
from app.utils import hash_value
66

77

88
class PasteInputModel(BaseModel):
@@ -38,15 +38,59 @@ class PasteDataAware:
3838
content: str
3939
client_id: str
4040
created_at: int
41+
content_encoding: str = Field(
42+
default="utf-8",
43+
description="Encoding used for the content. Default is 'utf-8'.",
44+
)
4145

4246
def __init__(
4347
self,
4448
content: str,
4549
client_id: str,
4650
created_at: Optional[int] = None,
4751
paste_id: str = None,
52+
content_encoding: str = "utf-8",
4853
):
49-
self.paste_id = hash_value(value=content) if paste_id is None else paste_id
50-
self.content = content
54+
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+
)
5162
self.client_id = client_id
5263
self.created_at = int(time.time()) if created_at is None else created_at
64+
self.content_encoding = content_encoding
65+
66+
def _is_base64(self, s: str) -> bool:
67+
"""
68+
Check if a string is valid Base64.
69+
70+
Args:
71+
s (str): The string to check.
72+
73+
Returns:
74+
bool: True if the string is valid Base64, False otherwise.
75+
"""
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
81+
82+
def plain_content(self):
83+
"""
84+
Returns the content of the paste in plain text format.
85+
86+
"""
87+
return base64.b64decode(self._content.encode(self.content_encoding)).decode(
88+
self.content_encoding
89+
)
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.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.base64_content(), paste.client_id, paste.created_at
101101
)
102102

103103
return result["paste_id"]

backend/app/utils.py

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)