Example #1
0
def create_files(cursor, contentnode, indent=0, download_url=None):
    """ create_files: Get license
        Args:
            cursor (sqlite3.Connection): connection to export database
            contentnode (models.ContentNode): node file references
            indent (int): How far to indent print statements
        Returns: None
    """
    # Parse database for files referencing content node and make file models
    sql_command = 'SELECT checksum, extension, file_size, contentnode_id, '\
        'lang_id, preset FROM {table} WHERE contentnode_id=\'{id}\';'\
        .format(table=FILE_TABLE, id=contentnode.node_id)

    query = cursor.execute(sql_command).fetchall()
    for checksum, extension, file_size, contentnode_id, lang_id, preset in query:
        filename = "{}.{}".format(checksum, extension)
        print("{indent} * FILE {filename}...".format(indent="   |" * indent,
                                                     filename=filename))

        try:
            filepath = models.generate_object_storage_name(checksum, filename)

            # Download file first
            if download_url and not default_storage.exists(filepath):
                buffer = StringIO()
                response = requests.get('{}/content/storage/{}/{}/{}'.format(
                    download_url, filename[0], filename[1], filename))
                for chunk in response:
                    buffer.write(chunk)
                create_file_from_contents(buffer.getvalue(),
                                          ext=extension,
                                          node=contentnode,
                                          preset_id=preset or "")
                buffer.close()
            else:
                # Save values to new or existing file object
                file_obj = models.File(
                    file_format_id=extension,
                    file_size=file_size,
                    contentnode=contentnode,
                    language_id=lang_id,
                    preset_id=preset or "",
                )
                file_obj.file_on_disk.name = filepath
                file_obj.save()

        except IOError as e:
            logging.warning("\b FAILED (check logs for more details)")
            sys.stderr.write(
                "Restoration Process Error: Failed to save file object {}: {}".
                format(filename, os.strerror(e.errno)))
            continue
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)
Example #3
0
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)