From 88d4b0b50ca445a798229a1850b22c882edb7aae Mon Sep 17 00:00:00 2001 From: Nikita Manovich Date: Wed, 6 Feb 2019 00:22:28 +0300 Subject: [PATCH] Fixed problems with renaming of files. --- cvat/apps/engine/models.py | 13 ++++++++++++- cvat/apps/engine/serializers.py | 7 +++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/cvat/apps/engine/models.py b/cvat/apps/engine/models.py index a22999221263..6a29b3a52406 100644 --- a/cvat/apps/engine/models.py +++ b/cvat/apps/engine/models.py @@ -84,6 +84,17 @@ def get_task_dirname(self): def __str__(self): return self.name +# Redefined a couple of operation for FileSystemStorage to avoid renaming +# or other side effects. +class MyFileSystemStorage(FileSystemStorage): + def get_valid_name(self, name): + return name + + def get_available_name(self, name, max_length=None): + if self.exists(name) or (max_length and len(name) > max_length): + raise IOError('`{}` file already exists or its name is too long'.format(name)) + return name + def upload_path_handler(instance, filename): return os.path.join(instance.task.get_upload_dirname(), filename) @@ -91,7 +102,7 @@ def upload_path_handler(instance, filename): class ClientFile(models.Model): task = models.ForeignKey(Task, on_delete=models.CASCADE) file = models.FileField(upload_to=upload_path_handler, - storage=FileSystemStorage()) + storage=MyFileSystemStorage()) class Meta: default_permissions = () diff --git a/cvat/apps/engine/serializers.py b/cvat/apps/engine/serializers.py index 37cf6bd160ba..9a3549274ef7 100644 --- a/cvat/apps/engine/serializers.py +++ b/cvat/apps/engine/serializers.py @@ -60,6 +60,13 @@ class Meta: model = ClientFile fields = ('file', ) + def to_internal_value(self, data): + return {'file': data} + + def to_representation(self, instance): + upload_dir = instance.task.get_upload_dirname() + return instance.file.path[len(upload_dir) + 1:] + class ServerFileSerializer(serializers.ModelSerializer): class Meta: model = ServerFile