Check image extensions before saving
This commit is contained in:
parent
1dfe4d0f52
commit
754e24812b
3 changed files with 16 additions and 12 deletions
|
@ -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:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue