Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit c2bdf04

Browse files
authored
Discard an empty upload_name before persisting an uploaded file (#7905)
1 parent e154f7c commit c2bdf04

File tree

3 files changed

+9
-3
lines changed

3 files changed

+9
-3
lines changed

changelog.d/7905.bugfix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix a longstanding bug when storing a media file with an empty `upload_name`.

synapse/rest/media/v1/media_repository.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,23 +139,24 @@ def mark_recently_accessed(self, server_name, media_id):
139139
async def create_content(
140140
self,
141141
media_type: str,
142-
upload_name: str,
142+
upload_name: Optional[str],
143143
content: IO,
144144
content_length: int,
145145
auth_user: str,
146146
) -> str:
147147
"""Store uploaded content for a local user and return the mxc URL
148148
149149
Args:
150-
media_type: The content type of the file
151-
upload_name: The name of the file
150+
media_type: The content type of the file.
151+
upload_name: The name of the file, if provided.
152152
content: A file like object that is the content to store
153153
content_length: The length of the content
154154
auth_user: The user_id of the uploader
155155
156156
Returns:
157157
The mxc url of the stored content
158158
"""
159+
159160
media_id = random_string(24)
160161

161162
file_info = FileInfo(server_name=None, file_id=media_id)

synapse/rest/media/v1/upload_resource.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ async def _async_render_POST(self, request):
6363
msg="Invalid UTF-8 filename parameter: %r" % (upload_name), code=400
6464
)
6565

66+
# If the name is falsey (e.g. an empty byte string) ensure it is None.
67+
else:
68+
upload_name = None
69+
6670
headers = request.requestHeaders
6771

6872
if headers.hasHeader(b"Content-Type"):

0 commit comments

Comments
 (0)