Move magic sniffing to its own library, out of MySQL stuff.

This commit is contained in:
Brad Fitzpatrick 2011-03-19 00:48:33 -07:00
parent 184afbc2a3
commit 3bc3aa1390
3 changed files with 51 additions and 21 deletions

View File

@ -329,6 +329,7 @@ TARGET: lib/go/camli/blobserver/localdisk
TARGET: lib/go/camli/client
TARGET: lib/go/camli/httputil
TARGET: lib/go/camli/jsonsign
TARGET: lib/go/camli/magic
TARGET: lib/go/camli/misc/httprange
TARGET: lib/go/camli/mysqlindexer
- ext:github.com/Philio/GoMySQL

View File

@ -0,0 +1,44 @@
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
nYou may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package magic
import (
"bytes"
)
type prefixEntry struct {
prefix []byte
mtype string
}
var prefixTable = []prefixEntry{
{[]byte("\xff\xd8\xff\xe1"), "image/jpeg"},
{[]byte("\xff\xd8\xff\xe0"), "image/jpeg"},
{[]byte{137, 'P', 'N', 'G', '\r', '\n', 26, 10}, "image/png"},
}
// Returns the emptry string if unknown.
func MimeType(hdr []byte) string {
hlen := len(hdr)
for _, pte := range prefixTable {
plen := len(pte.prefix)
if hlen > plen && bytes.Equal(hdr[:plen], pte.prefix) {
return pte.mtype
}
}
return ""
}

View File

@ -19,9 +19,9 @@ package mysqlindexer
import (
"camli/blobref"
"camli/blobserver"
"camli/magic"
"camli/schema"
"bytes"
"io"
"json"
"log"
@ -55,17 +55,6 @@ func (sn *blobSniffer) IsTruncated() bool {
return sn.written > maxSniffSize
}
type prefixEntry struct {
prefix []byte
mtype string
}
var prefixTable = []prefixEntry{
{[]byte("\xff\xd8\xff\xe1"), "image/jpeg"},
{[]byte("\xff\xd8\xff\xe0"), "image/jpeg"},
{[]byte{137, 'P', 'N', 'G', '\r', '\n', 26, 10}, "image/png"},
}
// returns content type (string) or nil if unknown
func (sn *blobSniffer) MimeType() interface{} {
if sn.mimeType != nil {
@ -75,21 +64,17 @@ func (sn *blobSniffer) MimeType() interface{} {
}
func (sn *blobSniffer) Parse() {
hlen := len(sn.header)
for _, pte := range prefixTable {
plen := len(pte.prefix)
if hlen > plen && bytes.Equal(sn.header[:plen], pte.prefix) {
sn.mimeType = &pte.mtype
}
}
// Try to parse it as JSON
// TODO: move this into the magic library? Is the magic library Camli-specific
// or to be upstreamed elsewhere?
if sn.bufferIsCamliJson() {
str := "application/json; camliType=" + sn.camli.Type
sn.mimeType = &str
}
return
if mime := magic.MimeType(sn.header); mime != "" {
sn.mimeType = &mime
}
}
func (sn *blobSniffer) bufferIsCamliJson() bool {