github.com/olivere/camlistore@v0.0.0-20140121221811-1b7ac2da0199/pkg/magic/magic.go (about)

     1  /*
     2  Copyright 2011 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  nYou may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package magic implements MIME type sniffing of data based on the
    18  // well-known "magic" number prefixes in the file.
    19  package magic
    20  
    21  import (
    22  	"bytes"
    23  	"io"
    24  	"net/http"
    25  	"strings"
    26  )
    27  
    28  type prefixEntry struct {
    29  	prefix []byte
    30  	mtype  string
    31  }
    32  
    33  var prefixTable = []prefixEntry{
    34  	{[]byte("GIF87a"), "image/gif"},
    35  	{[]byte("GIF89a"), "image/gif"}, // TODO: Others?
    36  	{[]byte("\xff\xd8\xff\xe2"), "image/jpeg"},
    37  	{[]byte("\xff\xd8\xff\xe1"), "image/jpeg"},
    38  	{[]byte("\xff\xd8\xff\xe0"), "image/jpeg"},
    39  	{[]byte("\xff\xd8\xff\xdb"), "image/jpeg"},
    40  	{[]byte("\x49\x49\x2a\x00\x10\x00\x00\x00\x43\x52\x02"), "image/cr2"},
    41  	{[]byte{137, 'P', 'N', 'G', '\r', '\n', 26, 10}, "image/png"},
    42  	{[]byte("-----BEGIN PGP PUBLIC KEY BLOCK---"), "text/x-openpgp-public-key"},
    43  	{[]byte{'I', 'D', '3'}, "audio/mpeg"},
    44  	// TODO(bradfitz): popular audio & video formats at least
    45  }
    46  
    47  // MIMEType returns the MIME type from the data in the provided header
    48  // of the data.
    49  // It returns the empty string if the MIME type can't be determined.
    50  func MIMEType(hdr []byte) string {
    51  	hlen := len(hdr)
    52  	for _, pte := range prefixTable {
    53  		plen := len(pte.prefix)
    54  		if hlen > plen && bytes.Equal(hdr[:plen], pte.prefix) {
    55  			return pte.mtype
    56  		}
    57  	}
    58  	t := http.DetectContentType(hdr)
    59  	t = strings.Replace(t, "; charset=utf-8", "", 1)
    60  	if t != "application/octet-stream" && t != "text/plain" {
    61  		return t
    62  	}
    63  	return ""
    64  }
    65  
    66  // MIMETypeFromReader takes a reader, sniffs the beginning of it,
    67  // and returns the mime (if sniffed, else "") and a new reader
    68  // that's the concatenation of the bytes sniffed and the remaining
    69  // reader.
    70  func MIMETypeFromReader(r io.Reader) (mime string, reader io.Reader) {
    71  	var buf bytes.Buffer
    72  	io.CopyN(&buf, r, 1024)
    73  	mime = MIMEType(buf.Bytes())
    74  	return mime, io.MultiReader(&buf, r)
    75  }
    76  
    77  // MIMETypeFromReader takes a ReaderAt, sniffs the beginning of it,
    78  // and returns the MIME type if sniffed, else the empty string.
    79  func MIMETypeFromReaderAt(ra io.ReaderAt) (mime string) {
    80  	var buf [1024]byte
    81  	n, _ := ra.ReadAt(buf[:], 0)
    82  	return MIMEType(buf[:n])
    83  }