What you can do, is initially save everything as File Document, but after that do some processing.
First check the file type (as you already seem to do), then in case of images, extract the contents of the document (for example by decoding it into Base64. After that write the contents to an image entity.
Another way you could try is using the ‘Clone’ function from CommunityCommons. The documentation states:
- Source: the original object to copy
- Target: the object to copy it into (should be of the same type, or a specialization)
- includeAssociations: whether to clone associations.
If associated objects need to be cloned as well, use deepClone, this function only copies the references, not the reffered objects. Target is not committed automatically.
Since Image is a specialization of FileDocument, this could work.