import re
from . import conf
from rest_framework import serializers


def image_validator(image):
    if not image.name.endswith(conf.allowed_formats_for_image):
        raise serializers.ValidationError('Image format is not supported')
    if image.size > (1024 * conf.maximum_image_size):
        raise serializers.ValidationError('Image size is too large')
    return image



def file_validator(data):
    file = data['file']
    if data['file_type'] == 'image':
        if not file.name.endswith(conf.allowed_formats_for_image):
            raise serializers.ValidationError('Image format is not supported')
        if file.size > (1024 * conf.maximum_image_size):
            raise serializers.ValidationError('Image size is too large')
    elif data['file_type'] == 'audio':
        if not file.name.endswith(conf.allowed_formats_for_audio):
            raise serializers.ValidationError('Audio format is not supported')
        if file.size > (1024 * conf.maximum_audio_size):
            raise serializers.ValidationError('Audio size is too large')
    elif data['file_type'] == 'video':
        if not file.name.endswith(conf.allowed_formats_for_video):
            raise serializers.ValidationError('Video format is not supported')
        if file.size > (1024 * conf.maximum_video_size):
            raise serializers.ValidationError('Video size is too large')
    else:
        raise serializers.ValidationError('Enter a valid file_type')