Skip to content

Instantly share code, notes, and snippets.

@rayrutjes
Created December 12, 2015 13:36
Show Gist options
  • Save rayrutjes/db9b9ea8e02255d62ce2 to your computer and use it in GitHub Desktop.
Save rayrutjes/db9b9ea8e02255d62ce2 to your computer and use it in GitHub Desktop.
golang detect content type of a file
file, err := os.Open(path)
if err != nil {
return err
}
defer file.Close()
// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)
_, err = file.Read(buffer)
if err != nil {
return err
}
// Reset the read pointer if necessary.
file.Seek(0, 0)
// Always returns a valid content-type and "application/octet-stream" if no others seemed to match.
contentType := http.DetectContentType(buffer)
@ardwiinoo
Copy link

Cool

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment