コード例 #1
0
    def convert_base64_to_file(self):
        # Get hash of content for cache key
        hashed_content = hashlib.md5()
        hashed_content.update(self.encoding.encode('utf-8'))
        key = "ENCODED: {} (base64 encoded)".format(hashed_content.hexdigest())

        if not config.UPDATE and FILECACHE.get(key):
            return FILECACHE.get(key).decode('utf-8')

        config.LOGGER.info("\tConverting base64 to file")

        extension = get_base64_encoding(self.encoding).group(1)
        assert extension in [
            file_formats.PNG, file_formats.JPG, file_formats.JPEG
        ], "Base64 files must be images in jpg or png format"

        tempf = tempfile.NamedTemporaryFile(suffix=".{}".format(extension),
                                            delete=False)
        tempf.close()
        write_base64_to_file(self.encoding, tempf.name)
        filename = "{}.{}".format(get_hash(tempf.name), file_formats.PNG)

        copy_file_to_storage(filename, tempf.name)
        os.unlink(tempf.name)
        FILECACHE.set(key, bytes(filename, "utf-8"))
        return filename
コード例 #2
0
def create_content_thumbnail(thumbnail_string, file_format_id=file_formats.PNG, preset_id=None):
    thumbnail_data = ast.literal_eval(thumbnail_string)
    if thumbnail_data.get('base64'):
        with tempfile.NamedTemporaryFile(suffix=".{}".format(file_format_id), delete=False) as tempf:
            tempf.close()
            write_base64_to_file(thumbnail_data['base64'], tempf.name)
            with open(tempf.name, 'rb') as tf:
                return create_file_from_contents(tf.read(), ext=file_format_id, preset_id=preset_id)
コード例 #3
0
 def add_picture(self, encoding, left=0, top=0, width=2, height=2):
     filepath = self.get_write_to_path(ext="png")
     write_base64_to_file(encoding, filepath)
     return self.slide.shapes.add_picture(filepath,
                                          Inches(left),
                                          Inches(top),
                                          width=Inches(width),
                                          height=Inches(height))
コード例 #4
0
def create_thumbnail_from_base64(encoding, file_format_id=file_formats.PNG, preset_id=None, uploaded_by=None):
    """
        Takes encoding and makes it into a file object
        Args:
            encoding (str): base64 to make into an image file
            file_format_id (str): what the extension should be
            preset_id (str): what the preset should be
            uploaded_by (<User>): who uploaded the image
        Returns <File> object with the file_on_disk being the image file generated from the encoding
    """
    fd, path = tempfile.mkstemp()
    try:
        write_base64_to_file(encoding, path)
        with open(path, 'rb') as tf:
            return create_file_from_contents(tf.read(), ext=file_format_id, preset_id=preset_id, uploaded_by=uploaded_by)
    finally:
        os.close(fd)
コード例 #5
0
def create_thumbnail_from_base64(encoding, file_format_id=file_formats.PNG, preset_id=None, uploaded_by=None):
    """
        Takes encoding and makes it into a file object
        Args:
            encoding (str): base64 to make into an image file
            file_format_id (str): what the extension should be
            preset_id (str): what the preset should be
            uploaded_by (<User>): who uploaded the image
        Returns <File> object with the file_on_disk being the image file generated from the encoding
    """
    fd, path = tempfile.mkstemp()
    try:
        write_base64_to_file(encoding, path)
        with open(path, 'rb') as tf:
            return create_file_from_contents(tf.read(), ext=file_format_id, preset_id=preset_id, uploaded_by=uploaded_by)
    finally:
        os.close(fd)
コード例 #6
0
ファイル: import_tools.py プロジェクト: toccotedd/studio
def write_to_thumbnail_file(raw_thumbnail):
    """ write_to_thumbnail_file: Convert base64 thumbnail to file
        Args:
            raw_thumbnail (str): base64 encoded thumbnail
        Returns: thumbnail filename
    """
    if raw_thumbnail and isinstance(raw_thumbnail, basestring) and raw_thumbnail != "" and 'static' not in raw_thumbnail:
        with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tempf:
            try:
                tempf.close()
                write_base64_to_file(raw_thumbnail, tempf.name)
                with open(tempf.name, 'rb') as tf:
                    fobj = create_file_from_contents(tf.read(), ext="png", preset_id=format_presets.CHANNEL_THUMBNAIL)
                    return str(fobj)
            finally:
                tempf.close()
                os.unlink(tempf.name)
コード例 #7
0
 def get_thumbnail_from_encoding(self, encoding):
     filepath = self.get_write_to_path(ext="png")
     write_base64_to_file(encoding, filepath)
     return filepath
コード例 #8
0
 def add_picture(self, encoding, left=0, top=0, width=2, height=2):
     filepath = self.get_write_to_path(ext="png")
     write_base64_to_file(encoding, filepath)
     return self.slide.shapes.add_picture(filepath, Inches(left), Inches(top), width=Inches(width), height=Inches(height))
コード例 #9
0
 def get_thumbnail_from_encoding(self, encoding):
     filepath = self.get_write_to_path(ext="png")
     write_base64_to_file(encoding, filepath)
     return filepath