Support uploading of images from event creation
This commit is contained in:
@@ -9,7 +9,42 @@ from datetime import timedelta
|
||||
import logging
|
||||
from django.conf import settings
|
||||
from django.template.loader import render_to_string
|
||||
from django.core.files.base import ContentFile
|
||||
import base64
|
||||
import uuid
|
||||
from sikweb.settings import FRONTEND_URL, URL, EMAIL_API_KEY, EMAIL_API_SECRET, DEFAULT_EMAIL_FROM, DEFAULT_EMAIL_FROM_ADDR, ENABLE_AUTOMATIC_EMAILS
|
||||
import imghdr
|
||||
|
||||
|
||||
def get_file_extension(file_name, decoded_file):
|
||||
extension = imghdr.what(file_name, decoded_file)
|
||||
extension = "jpg" if extension == "jpeg" else extension
|
||||
return extension
|
||||
|
||||
|
||||
def decode_base64_file(data):
|
||||
# Check if this is a base64 string
|
||||
if isinstance(data, str):
|
||||
# Check if the base64 string is in the "data:" format
|
||||
if 'data:' in data and ';base64,' in data:
|
||||
# Break out the header from the base64 content
|
||||
header, data = data.split(';base64,')
|
||||
|
||||
# Try to decode the file. Return validation error if it fails.
|
||||
try:
|
||||
decoded_file = base64.b64decode(data)
|
||||
except TypeError:
|
||||
TypeError('invalid_image')
|
||||
|
||||
# Generate file name:
|
||||
file_name = str(uuid.uuid4())
|
||||
|
||||
# Get the file name extension:
|
||||
file_extension = get_file_extension(file_name, decoded_file)
|
||||
|
||||
complete_file_name = "%s.%s" % (file_name, file_extension, )
|
||||
|
||||
return ContentFile(decoded_file, name=complete_file_name)
|
||||
|
||||
|
||||
def month_from_now():
|
||||
|
||||
Reference in New Issue
Block a user