0

I have a POST-method which reads the body as a stream, if one was provided that is. I need to check if this stream represents an image or not. First thing that comes to mind is that I can read through the bytes and check if they conform to a certain image/file structure but that requires enough time to make the API 'feel' slow, is there another (read better) way of doing this?

Zita
  • 121
  • 1
  • 9
  • 1
    most image file types start with a [Magic Number](https://en.wikipedia.org/wiki/Magic_number_(programming)) - which is just a few bytes at the beginning. you should be able to check that without a significant performance impact – Franz Gleichmann Oct 07 '22 at 11:53
  • [This](https://stackoverflow.com/questions/670546/determine-if-file-is-an-image) has some ways of doing it, though it doesn't have some image types (e.g. webm) – DavidG Oct 07 '22 at 11:55
  • @FranzGleichmann Hmm okay, I was looping through the entire thing because JPEG for example has a ending sequence (0xFF, 0xD9) but I don't need to look for that then? All I have to check is the starting sequence for JPEG (0xFF, 0xD8) – Zita Oct 07 '22 at 12:05

0 Answers0