How can I detect a safe eject of mass storage device on STM32?
"Safe to eject" is strictly a host-side software change of state. It simply means that the host has finished writing any pending changes to the device and won't initiate any more.

There is no reason to provide any signaling to the device itself once this has happened, so there's no way to detect it there when you are emulating storage. You'll have to provide a side channel (i.e., a separate USB endpoint) and a custom host-side driver if you want to do this.