Created
December 12, 2015 13:36
-
-
Save rayrutjes/db9b9ea8e02255d62ce2 to your computer and use it in GitHub Desktop.
golang detect content type of a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
mogaika
commented
Mar 29, 2016
@mogaika This is no longer needed. DetectContentType
handles it itself.
@0xbkt Handles what? err != nil { return err }
will drop detection for files with size < 512 bytes
Yes, @mogaika is right. I also found another problem: Text files with less than 512 bytes are detected as application/octet-stream
. The problem seems to be that buffer
is filled with zero values up top a length of 512 bytes. If you slice the array to the size actually returned from Read()
before passing it to http.DetectContentType
it correctly returns text/plain; charset=utf8
:
func Get(seeker io.ReadSeeker) (string, error) {
// At most the first 512 bytes of data are used:
// https://golang.org/src/net/http/sniff.go?s=646:688#L11
buff := make([]byte, 512)
_, err := seeker.Seek(0, io.SeekStart)
if err != nil {
return "", err
}
bytesRead, err := seeker.Read(buff)
if err != nil && err != io.EOF {
return "", err
}
// Slice to remove fill-up zero values which cause a wrong content type detection in the next step
buff = buff[:bytesRead]
return http.DetectContentType(buff), nil
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment