Fix #1077: improved performance and error handling in fetch_attachment
This commit is contained in:
parent
08ffc7adc5
commit
c529d4d260
|
@ -77,7 +77,7 @@ def fetch_remote_attachment(attachment, filename=None, save=True):
|
||||||
attachment.last_fetch_date = timezone.now()
|
attachment.last_fetch_date = timezone.now()
|
||||||
with tempfile.TemporaryFile() as tf:
|
with tempfile.TemporaryFile() as tf:
|
||||||
with s.get(attachment.url, timeout=5, stream=True) as r:
|
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.write(chunk)
|
||||||
tf.seek(0)
|
tf.seek(0)
|
||||||
if not filename:
|
if not filename:
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import logging
|
||||||
import time
|
import time
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
@ -23,6 +24,9 @@ from . import throttling
|
||||||
from . import utils
|
from . import utils
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
class SkipFilterForGetObject:
|
class SkipFilterForGetObject:
|
||||||
def get_object(self, *args, **kwargs):
|
def get_object(self, *args, **kwargs):
|
||||||
setattr(self.request, "_skip_filters", True)
|
setattr(self.request, "_skip_filters", True)
|
||||||
|
@ -172,7 +176,11 @@ class AttachmentViewSet(
|
||||||
if size not in ["original", "medium_square_crop"]:
|
if size not in ["original", "medium_square_crop"]:
|
||||||
size = "original"
|
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
|
data = self.serializer_class(instance).data
|
||||||
redirect = response.Response(status=302)
|
redirect = response.Response(status=302)
|
||||||
redirect["Location"] = data["urls"][size]
|
redirect["Location"] = data["urls"][size]
|
||||||
|
|
Loading…
Reference in New Issue