When #2 is done, it's important that people don't end up with URLs without the appropriate extension. To solve the most common situations where someone would upload binary data without an extension, inspect the first few bytes to detect if the file is a PNG or a JPEG.