Skip to content

Commit 8c1a5d5

Browse files
feat: improve avatar filename generation with content type handling
- Refactored avatar download logic to determine file extension based on the content type from the response headers. - Removed redundant code for extension mapping, ensuring a cleaner implementation. - Enhanced error handling by returning None for unsupported content types.
1 parent e6dd488 commit 8c1a5d5

File tree

1 file changed

+14
-11
lines changed
  • apps/api/plane/authentication/adapter

1 file changed

+14
-11
lines changed

apps/api/plane/authentication/adapter/base.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,20 @@ def download_and_upload_avatar(self, avatar_url, user):
130130
if content_length and int(content_length) > max_size:
131131
return None
132132

133+
# Get content type and determine file extension
134+
content_type = response.headers.get("Content-Type", "image/jpeg")
135+
extension_map = {
136+
"image/jpeg": "jpg",
137+
"image/jpg": "jpg",
138+
"image/png": "png",
139+
"image/gif": "gif",
140+
"image/webp": "webp",
141+
}
142+
extension = extension_map.get(content_type)
143+
144+
if not extension:
145+
return None
146+
133147
# Download with size limit
134148
chunks = []
135149
total_size = 0
@@ -141,17 +155,6 @@ def download_and_upload_avatar(self, avatar_url, user):
141155
content = b"".join(chunks)
142156
file_size = len(content)
143157

144-
# Get content type and determine file extension
145-
content_type = response.headers.get("Content-Type", "image/jpeg")
146-
extension_map = {
147-
"image/jpeg": "jpg",
148-
"image/jpg": "jpg",
149-
"image/png": "png",
150-
"image/gif": "gif",
151-
"image/webp": "webp",
152-
}
153-
extension = extension_map.get(content_type, "jpg")
154-
155158
# Generate unique filename
156159
filename = f"{uuid.uuid4().hex}-user-avatar.{extension}"
157160

0 commit comments

Comments
 (0)