Skip to content

Commit e0e0ed3

Browse files
committed
Ensure we delete media if we reject due to spam check
Fixes up #17239
1 parent 466f344 commit e0e0ed3

File tree

1 file changed

+27
-32
lines changed

1 file changed

+27
-32
lines changed

synapse/media/media_storage.py

+27-32
Original file line numberDiff line numberDiff line change
@@ -137,42 +137,37 @@ async def store_into_file(
137137
dirname = os.path.dirname(fname)
138138
os.makedirs(dirname, exist_ok=True)
139139

140-
main_media_repo_write_trace_scope = start_active_span(
141-
"writing to main media repo"
142-
)
143-
main_media_repo_write_trace_scope.__enter__()
144-
145-
with main_media_repo_write_trace_scope:
146-
try:
140+
try:
141+
with start_active_span("writing to main media repo"):
147142
with open(fname, "wb") as f:
148143
yield f, fname
149144

150-
except Exception as e:
151-
try:
152-
os.remove(fname)
153-
except Exception:
154-
pass
155-
156-
raise e from None
157-
158-
with start_active_span("writing to other storage providers"):
159-
spam_check = (
160-
await self._spam_checker_module_callbacks.check_media_file_for_spam(
161-
ReadableFileWrapper(self.clock, fname), file_info
145+
with start_active_span("writing to other storage providers"):
146+
spam_check = (
147+
await self._spam_checker_module_callbacks.check_media_file_for_spam(
148+
ReadableFileWrapper(self.clock, fname), file_info
149+
)
162150
)
163-
)
164-
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
165-
logger.info("Blocking media due to spam checker")
166-
# Note that we'll delete the stored media, due to the
167-
# try/except below. The media also won't be stored in
168-
# the DB.
169-
# We currently ignore any additional field returned by
170-
# the spam-check API.
171-
raise SpamMediaException(errcode=spam_check[0])
172-
173-
for provider in self.storage_providers:
174-
with start_active_span(str(provider)):
175-
await provider.store_file(path, file_info)
151+
if spam_check != self._spam_checker_module_callbacks.NOT_SPAM:
152+
logger.info("Blocking media due to spam checker")
153+
# Note that we'll delete the stored media, due to the
154+
# try/except below. The media also won't be stored in
155+
# the DB.
156+
# We currently ignore any additional field returned by
157+
# the spam-check API.
158+
raise SpamMediaException(errcode=spam_check[0])
159+
160+
for provider in self.storage_providers:
161+
with start_active_span(str(provider)):
162+
await provider.store_file(path, file_info)
163+
164+
except Exception as e:
165+
try:
166+
os.remove(fname)
167+
except Exception:
168+
pass
169+
170+
raise e from None
176171

177172
async def fetch_media(self, file_info: FileInfo) -> Optional[Responder]:
178173
"""Attempts to fetch media described by file_info from the local cache

0 commit comments

Comments
 (0)