Skip to content

Commit

Permalink
Fix social auth avatar save.
Browse files Browse the repository at this point in the history
  • Loading branch information
TyVik committed Jan 6, 2024
1 parent 0b5ef30 commit 9938b01
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions users/pipeline.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os
from io import FileIO
from io import BytesIO
from typing import Optional
from urllib.request import urlretrieve
from urllib.parse import unquote, urlparse

import requests
from django.conf import settings
from django.utils.crypto import get_random_string
from social_core.backends.facebook import FacebookOAuth2
from social_core.backends.google import GoogleOAuth2
from social_core.backends.vk import VKOAuth2
Expand All @@ -14,8 +15,9 @@
def user_details(strategy, response, backend, is_new, *args, user=None, **kwargs):
def save_image(user: User, url: Optional[str]) -> None:
if url:
result = urlretrieve(url)
user.image.save(os.path.basename(url), FileIO(result[0]))
result = requests.get(unquote(url), timeout=2.0)
ext = urlparse(url).path.split('.')[-1]
user.image.save(f'{get_random_string(16)}.{ext}', BytesIO(result.content))
user.save()

if user is not None and is_new:
Expand Down

0 comments on commit 9938b01

Please sign in to comment.