0

I'm working on a chat application where I need to handle and store various types of files such as images, videos, audios, PDFs, and documents using Django and Django REST framework. I have a WebSocket consumer with events like connect, disconnect, and receive. In the receive event, I'm trying to handle video files. Here's a simplified version of my code:

    # consumers.py
    async def video(self, event):
        stream = File(BytesIO(event['file']), name=event['file_name'])
        data = JSONParser().parse(stream)
        event['file'] = data
        await self.send(text_data=json.dumps(event))
# serializers.py
from drf_extra_fields.fields import Base64FileField, 
Base64ImageField
import filetype

class MyBase64FileField(Base64FileField):

    ALLOWED_TYPES = ['pdf', 'docx','doc','mp3','mp4','mov']

    def get_file_extension(self, filename, decoded_file):
        extension = filetype.guess_extension(decoded_file)
        return extension

    def to_internal_value(self, data):
        if isinstance(data, str):
            return super().to_internal_value(data)
        return data

class MessageSerializer(serializers.ModelSerializer):
    image = Base64ImageField(required=False)
    file = MyBase64FileField(required=False)
    class Meta:
        model = ChannelMessages
        fields = '__all__'

I'm facing issues with sending video files over the WebSocket. What changes do I need to make in my code to handle video files properly and send them over the WebSocket? Additionally, I'm looking for a solution that allows me to handle and send other file types like images, audios, PDFs, and documents as well.

0