Would need to test it, but I think the binary file contains information about the type of file. If you convert to a string (part of the community commons market place module) and then look for it (using substring and contains logic). I will try to look into this tomorrow and update the answer (if required because no-one else gave an answer).
You can check out these Market place modules:
Mime Type checker (recently added)
File Type checker (has not been updated since 2019)
You would then need to create some logic to update the filename with the correct extension