The filename (and extension) is a property of the directory, not of the file itself. Therefore there is no way to know the filename from it's CID.
Also, ipfs is content agnostic so, unless your file actually has been added using specific CID-codec, ipfs just sees "unixfs" nodes.
The only thing you can do is to try to sniff the first block of the file and figure out the type from there. The gateway endpoint does this to set
Content-Type headers I think, but probably reads the full file first. You can see the blocks in a file with the
refs command. The first one shouold be the first block.
block get can get it and you can do
file block.bin to figure out types, or use some library (just found https://github.com/h2non/filetype after googling).