<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def insert_file(file, media_type):
 """Upsert the ``file`` and ``media_type`` into the files table. Returns the ``fileid`` and ``sha1`` of the upserted file. """

    resource_hash = get_file_sha1(file)
    with db_connect() as db_conn:
        with db_conn.cursor() as cursor:
            cursor.execute("SELECT fileid FROM files WHERE sha1 = %s",
                           (resource_hash,))
            try:
                fileid = cursor.fetchone()[0]
            except (IndexError, TypeError):
                cursor.execute("INSERT INTO files (file, media_type) "
                               "VALUES (%s, %s)"
                               "RETURNING fileid",
                               (psycopg2.Binary(file.read()), media_type,))
                fileid = cursor.fetchone()[0]
    return fileid, resource_hash