Check image extensions before saving
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
""" functionality outline for a book data connector """
|
||||
from abc import ABC, abstractmethod
|
||||
import imghdr
|
||||
import logging
|
||||
|
||||
from django.core.files.base import ContentFile
|
||||
from django.db import transaction
|
||||
import requests
|
||||
from requests.exceptions import RequestException
|
||||
@ -291,9 +293,17 @@ def get_image(url, timeout=10):
|
||||
except RequestException as err:
|
||||
logger.exception(err)
|
||||
return None
|
||||
|
||||
if not resp.ok:
|
||||
return None
|
||||
return resp
|
||||
|
||||
image_content = ContentFile(resp.content)
|
||||
extension = imghdr.what(None, image_content.read())
|
||||
if not extension:
|
||||
logger.exception("File requested was not an image: %s", url)
|
||||
return None
|
||||
|
||||
return image_content, extension
|
||||
|
||||
|
||||
class Mapping:
|
||||
|
Reference in New Issue
Block a user