Пример #1
0
def create_user_dir(sender, instance, created, **kwargs):
    """
    Create corresponding File instance and directory in hdfs
    when a user is created
    """
    if created:
        root_dir = File.objects.filter(parent=None)[0]
        user_dir = root_dir.parent_node.filter(name="user")[0]
        home_dir = File.objects.create(owner=instance, name=instance.username, parent=user_dir)
        response = make_dir(instance.username, home_dir.get_hdfs_path())
        print(response)
Пример #2
0
def create_user_dir(sender, instance, created, **kwargs):
    """
    Create corresponding File instance and directory in hdfs
    when a user is created
    """
    if created:
        root_dir = File.objects.filter(parent=None)[0]
        user_dir = root_dir.parent_node.filter(name='user')[0]
        home_dir = File.objects.create(owner=instance,
                                       name=instance.username,
                                       parent=user_dir)
        response = make_dir(instance.username, home_dir.get_hdfs_path())
        print(response)
Пример #3
0
    def perform_create(self, serializer):
        """
        Upload file to hdfs or create directory in hdfs
        """
        size = 0
        data = serializer.validated_data

        if data["name"] == "":
            raise serializers.ValidationError({
                'name': "can not be empty"
            })

        if data['file_type'] == 'FILE':
            t_file = self.request.FILES['file_uploaded']
            
            if t_file is None:
                raise serializers.ValidationError({
                    'file_uploaded': "please upload a file"
                })

            size = self.request.FILES['file_uploaded'].size

        instance = serializer.save(owner=self.request.user, modified=timezone.now(), size=size)

        username = self.request.user.username

        hdfs_path = instance.get_hdfs_path()
        if data['file_type'] == 'FILE':
            local_file = instance.file_uploaded.path
            response = upload_file(username, local_file, hdfs_path)
            os.remove(local_file)

            if response != 'Upload success':
                print("upload file fail!")
                instance.delete()
                raise serializers.ValidationError({
                    'hdfs_path': response
                })

            serializer.save(file_uploaded=None)
        elif data['file_type'] == 'DIR':
            response = make_dir(username, hdfs_path)
            if response != 'Create directory success':
                instance.delete()
                print("create directory fail!")
                raise serializers.ValidationError({
                    'hdfs_path': response
                })
Пример #4
0
    def perform_create(self, serializer):
        """
        Upload file to hdfs or create directory in hdfs
        """
        size = 0
        data = serializer.validated_data

        if data["name"] == "":
            raise serializers.ValidationError({'name': "can not be empty"})

        if data['file_type'] == 'FILE':
            t_file = self.request.FILES['file_uploaded']

            if t_file is None:
                raise serializers.ValidationError(
                    {'file_uploaded': "please upload a file"})

            size = self.request.FILES['file_uploaded'].size

        instance = serializer.save(owner=self.request.user,
                                   modified=timezone.now(),
                                   size=size)

        username = self.request.user.username

        hdfs_path = instance.get_hdfs_path()
        if data['file_type'] == 'FILE':
            local_file = instance.file_uploaded.path
            response = upload_file(username, local_file, hdfs_path)
            os.remove(local_file)

            if response != 'Upload success':
                print("upload file fail!")
                instance.delete()
                raise serializers.ValidationError({'hdfs_path': response})

            serializer.save(file_uploaded=None)
        elif data['file_type'] == 'DIR':
            response = make_dir(username, hdfs_path)
            if response != 'Create directory success':
                instance.delete()
                print("create directory fail!")
                raise serializers.ValidationError({'hdfs_path': response})