From 682390dffad9824c1b3468e2cd5361119e05442f Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Thu, 6 Dec 2012 17:53:03 -0800 Subject: [PATCH] Add a GIF mime type, TODOs, add camdebug mime command. Change-Id: Ic8930d4250dec30ed8c9a056c1cecd70cc447f9d --- cmd/camdebug/camdebug.go | 14 +++++++++----- cmd/camdebug/mime.go | 36 ++++++++++++++++++++++++++++++++++++ cmd/camdebug/splits.go | 1 + pkg/magic/magic.go | 3 +++ 4 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 cmd/camdebug/mime.go diff --git a/cmd/camdebug/camdebug.go b/cmd/camdebug/camdebug.go index 3f196c905..3b2f94a53 100644 --- a/cmd/camdebug/camdebug.go +++ b/cmd/camdebug/camdebug.go @@ -18,18 +18,22 @@ package main import ( "flag" - "log" ) -var flagSplits = flag.Bool("splits", false, "show splits") +var ( + flagSplits = flag.Bool("splits", false, "show splits of provided filename") + flagMIME = flag.Bool("mime", false, "show MIME type of provided file") +) func main() { flag.Parse() - + if *flagMIME { + showMIME() + return + } if *flagSplits { showSplits() return } - - log.Fatalf("TODO: usage info") + flag.Usage() } diff --git a/cmd/camdebug/mime.go b/cmd/camdebug/mime.go new file mode 100644 index 000000000..c01b657c6 --- /dev/null +++ b/cmd/camdebug/mime.go @@ -0,0 +1,36 @@ +/* +Copyright 2012 Google Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You 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 main + +import ( + "flag" + "log" + "fmt" + "os" + + "camlistore.org/pkg/magic" +) + +func showMIME() { + file := flag.Arg(0) + f, err := os.Open(file) + if err != nil { + log.Fatal(err) + } + mime, _ := magic.MimeTypeFromReader(f) + fmt.Println(mime) +} diff --git a/cmd/camdebug/splits.go b/cmd/camdebug/splits.go index 94fcd7c08..5ac2e8993 100644 --- a/cmd/camdebug/splits.go +++ b/cmd/camdebug/splits.go @@ -94,4 +94,5 @@ func showSplits() { } } dumpSpans(spans, 0) + fmt.Printf("\n\nNOTE NOTE NOTE: the camdebug tool hasn't been updated to use the splitting policy from pkg/schema/filewriter.go.") } diff --git a/pkg/magic/magic.go b/pkg/magic/magic.go index 87d7a6dab..39eba91e9 100644 --- a/pkg/magic/magic.go +++ b/pkg/magic/magic.go @@ -29,12 +29,15 @@ type prefixEntry struct { } var prefixTable = []prefixEntry{ + {[]byte("GIF87a"), "image/gif"}, + {[]byte("GIF89a"), "image/gif"}, // TODO: Others? {[]byte("\xff\xd8\xff\xe2"), "image/jpeg"}, {[]byte("\xff\xd8\xff\xe1"), "image/jpeg"}, {[]byte("\xff\xd8\xff\xe0"), "image/jpeg"}, {[]byte("\xff\xd8\xff\xdb"), "image/jpeg"}, {[]byte{137, 'P', 'N', 'G', '\r', '\n', 26, 10}, "image/png"}, {[]byte("-----BEGIN PGP PUBLIC KEY BLOCK---"), "text/x-openpgp-public-key"}, + // TODO(bradfitz): popular audio & video formats at least } // Returns the emptry string if unknown.