Exemplo n.º 1
0
    def as_img(self):
        """Convert the provided buffer to another format.

        Args:
            obj (File): The File/ContentFile object.

        Exceptions:
            Exception: Cowardly catch blanket exceptions here, log it, and return None.

        Returns:
            BytesIO: The BytesIO stream containing the converted File data.
            None: If there is an exception, the method returns None.

        """
        root = environ.Path(__file__) - 2  # Set the base directory to two levels.
        file_path = root('assets') + '/' + self.image

        # download it if file is remote
        if settings.AWS_STORAGE_BUCKET_NAME and settings.AWS_STORAGE_BUCKET_NAME in self.image:
            file_path = f'cache/{self.pk}.png'
            if not path.exists(file_path):
                safe_url = self.image.replace(' ', '%20')
                filedata = urllib.request.urlopen(safe_url)
                datatowrite = filedata.read()
                with open(file_path, 'wb') as f:
                    f.write(datatowrite)

        # serve file
        with open(file_path, 'rb') as f:
            obj = File(f)
            from avatar.utils import svg_to_png
            return svg_to_png(obj.read(), scale=3, width=333, height=384, index=self.pk, prefer='inkscape')
        return None
Exemplo n.º 2
0
    def as_img(self):
        """Convert the provided buffer to another format.

        Args:
            obj (File): The File/ContentFile object.

        Exceptions:
            Exception: Cowardly catch blanket exceptions here, log it, and return None.

        Returns:
            BytesIO: The BytesIO stream containing the converted File data.
            None: If there is an exception, the method returns None.

        """
        root = environ.Path(
            __file__) - 2  # Set the base directory to two levels.
        file_path = root('assets') + '/' + self.image
        with open(file_path, 'rb') as f:
            obj = File(f)
            from avatar.utils import svg_to_png
            return svg_to_png(obj.read(),
                              scale=3,
                              width=333,
                              height=384,
                              index=self.pk)
        return None