Merge branch '1077-hanging-podcasts' into 'develop'

Fix #1077: improved performance and error handling in fetch_attachment

Closes #1077

See merge request funkwhale/funkwhale!1099
This commit is contained in:
Agate 2020-04-21 14:54:55 +02:00
commit 3300634297
2 changed files with 10 additions and 2 deletions

View File

@ -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:

View File

@ -1,3 +1,4 @@
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]