Attaching additional files during upload

I am working on a module for Omeka S, but I’ve run into a bit of a problem.

The module is hooking into the media.ingest_file.pre with the intention of converting PDF’s into an image or several images. I have no problem uploading a PDF and converting it to an image and then creating a new media object and attaching it to the item.

The issue I’m running into is that once my new media item is attached and the code exits the event listener, the item is being saved and it’s basically ‘overwriting’ the media item that I just attached.

Any suggestions on when to do this conversion and attach the media?