diff --git a/build.pl b/build.pl index 1e0cf3122..8919168d7 100755 --- a/build.pl +++ b/build.pl @@ -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 diff --git a/lib/go/camli/magic/magic.go b/lib/go/camli/magic/magic.go new file mode 100644 index 000000000..3758f81d9 --- /dev/null +++ b/lib/go/camli/magic/magic.go @@ -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 "" +} diff --git a/lib/go/camli/mysqlindexer/receive.go b/lib/go/camli/mysqlindexer/receive.go index 6081c1bbb..54996a4ba 100644 --- a/lib/go/camli/mysqlindexer/receive.go +++ b/lib/go/camli/mysqlindexer/receive.go @@ -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 {