L
L
Lexx Xell2020-07-02 13:53:51
Django
Lexx Xell, 2020-07-02 13:53:51

Why does an error occur when serializing a file name?

Website in Django.

Dependent packages:
django 3.0.5
djangorestframework 3.11.0

When uploading files with certain names, the serializer gives invalidity. For example, this name crashes the serialization of "2 Sub Urban ft. Rei Ami - Freak (Rus Cover Oxygen1um ft. Lil Key) ▶ Song In Russian - Oxygenium Translation Rus Cover.mp4" (I understand that the title is too "tricky", but nothing I can do it - users are trying to download this)

Serializer code

class ClipVideoFileSerializer(serializers.ModelSerializer):
"""Clip VideoFile Serializer"""

class Meta:
    model = Clip
    fields = "__all__"
    read_only_fields = ('error', 'converted', 'is_playing', 'clip_duration',)


View Code

class ClipView(APIView):
"""Clip View"""

parser_classes = (MultiPartParser, FormParser, JSONParser)

object_serializer = ClipSerializer
object_get_serializer = ClipGetSerializer
video_file_serializer = ClipVideoFileSerializer
json_object_name = "clip"

"""

Часть несвязанного с проблемой кода скрыта

"""

def put(self, request):
    if request.data.get(self.json_object_name):

        """ Часть несвязанного с проблемой кода скрыта """

        """ При аплоаде выполняется блок elif """

    elif request.data.get("video_file") is not None and request.data.get("id") is not None:
        instance = get_object_or_404(Clip.objects.all(), pk=request.data.get("id"))

        """ Часть несвязанного с проблемой кода скрыта """

        file_serializer = self.video_file_serializer(
            instance=instance,
            data=request.data,
            partial=True
        )
        if file_serializer.is_valid():

            """ Часть несвязанного с проблемой кода скрыта """

        else:
            data = request.data
            return self.get(request, context={"detail": "update error", "id": data.get("id"),
                                              "convertation_status": "error"})
    else:
        return self.get(request, context={"error": "bad request"})

"""

Часть несвязанного с проблемой кода скрыта

"""


Model
class Clip(models.Model):
    """Clip"""
    
    """ Часть несвязанного с проблемой кода скрыта """

    name = models.CharField("Clip name", max_length=150, default="NoName")
    video_file = models.FileField("Clip", upload_to="clips", null=True, blank=True)

    """ Часть несвязанного с проблемой кода скрыта """


If the files have simple names (for example "video_bla_bla.mp4"), then there are no problems with downloading.

I tried to change the name when saving using django-hashedfilenamestorage, but to no avail.

It is possible to change the name of the file being sent at the front, but this is a crutch, because on the back there will be a possibility of an error due to the name.

How can I solve this problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lexx Xell, 2020-07-03
@LexxXell

Check what error the serializer gives:
print(file_serializer.errors)
In a specific case, the error is in exceeding the length of the file name string:

{'video_file': [ErrorDetail(string='Ensure this filename has at most 100 characters (it has 120).', code='max_length')]}

The default length was 100 characters.
The solution is to specify a longer filename length:
video_file = models.FileField("Clip", upload_to=get_new_video_file_path, null=True, blank=True, max_length=255)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question