Unverified Commit c529d4d2 authored by Agate's avatar Agate 💬
Browse files

Fix #1077: improved performance and error handling in fetch_attachment

parent 08ffc7ad
......@@ -77,7 +77,7 @@ def fetch_remote_attachment(attachment, filename=None, save=True):
attachment.last_fetch_date = timezone.now()
with tempfile.TemporaryFile() as tf:
with s.get(attachment.url, timeout=5, stream=True) as r:
for chunk in r.iter_content():
for chunk in r.iter_content(chunk_size=1024 * 100):
tf.write(chunk)
tf.seek(0)
if not filename:
......
import logging
import time
from django.conf import settings
......@@ -23,6 +24,9 @@ from . import throttling
from . import utils
logger = logging.getLogger(__name__)
class SkipFilterForGetObject:
def get_object(self, *args, **kwargs):
setattr(self.request, "_skip_filters", True)
......@@ -172,7 +176,11 @@ class AttachmentViewSet(
if size not in ["original", "medium_square_crop"]:
size = "original"
tasks.fetch_remote_attachment(instance)
try:
tasks.fetch_remote_attachment(instance)
except Exception:
logger.exception("Error while fetching attachment %s", instance.url)
return response.Response(status=500)
data = self.serializer_class(instance).data
redirect = response.Response(status=302)
redirect["Location"] = data["urls"][size]
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment