magicmime

package
v0.0.0-...-2be0f72 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2017 License: MIT Imports: 3 Imported by: 0

README

magicmime

magicmime is a Go package which allows you to discover a file's mimetype by looking for magic numbers in its content. It could be used as a supplementary for Go's mime package which only interprets the file extension to detect mimetypes. Internally, it implements libmagic(3) bindings.

Tested on Linux and Mac OS X, should be working on BSD. You could be able to build and make it working with Cygwin on Windows.

Prerequisites

You might need to install devel packages for libmagic. On Debian, Ubuntu and CentOS, get libmagic-dev package from your package manager. On Mac OS X get libmagic via Homebrew: brew install libmagic. If you don't have the required dev packages, compilation will be terminated by an error saying magic.h cannot be found.

Usage

In order to start, go get this repository:

go get github.com/rakyll/magicmime
Example
package main

import (
	"fmt"

	"github.com/rakyll/magicmime"
)

func main() {
	mm, err := magicmime.New(magicmime.MAGIC_MIME_TYPE | magicmime.MAGIC_SYMLINK | magicmime.MAGIC_ERROR)
	if err != nil {
		panic(err)
	}

	filepath := "/bin/ls"

	mimetype, err := mm.TypeByFile(filepath)
	if err != nil {
		fmt.Printf("Something went wrong: %s", err)
		return
	}

	fmt.Printf("%s -> %s\n", filepath, mimetype)
}

API

https://godoc.org/github.com/rakyll/magicmime

License

Copyright 2013 Google Inc. All Rights Reserved.

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.

Documentation

Overview

Package magicmime detects mimetypes using libmagic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Magic

type Magic struct {
	// contains filtered or unexported fields
}

func New

func New(flags MagicFlag) (*Magic, error)

func (*Magic) Close

func (m *Magic) Close()

func (*Magic) TypeByBuffer

func (m *Magic) TypeByBuffer(blob []byte) (string, error)

TypeByBuffer looks up for a blob's mimetype by its contents. It uses a magic number database which is described in magic(5).

func (*Magic) TypeByFile

func (m *Magic) TypeByFile(filePath string) (string, error)

TypeByFile looks up for a file's mimetype by its content. It uses a magic number database which is described in magic(5).

type MagicFlag

type MagicFlag int
const (
	// No special handling
	MAGIC_NONE MagicFlag = C.MAGIC_NONE

	// Prints debugging messages to stderr
	MAGIC_DEBUG MagicFlag = C.MAGIC_DEBUG

	// If the file queried is a symlink, follow it.
	MAGIC_SYMLINK MagicFlag = C.MAGIC_SYMLINK

	// If the file is compressed, unpack it and look at the contents.
	MAGIC_COMPRESS MagicFlag = C.MAGIC_COMPRESS

	// If the file is a block or character special device, then open the device
	// and try to look in its contents.
	MAGIC_DEVICES MagicFlag = C.MAGIC_DEVICES

	// Return a MIME type string, instead of a textual description.
	MAGIC_MIME_TYPE MagicFlag = C.MAGIC_MIME_TYPE

	// Return a MIME encoding, instead of a textual description.
	MAGIC_MIME_ENCODING MagicFlag = C.MAGIC_MIME_ENCODING

	// A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
	MAGIC_MIME MagicFlag = C.MAGIC_MIME

	// Return all matches, not just the first.
	MAGIC_CONTINUE MagicFlag = C.MAGIC_CONTINUE

	// Check the magic database for consistency and print warnings to stderr.
	MAGIC_CHECK MagicFlag = C.MAGIC_CHECK

	// On systems that support utime(2) or utimes(2), attempt to preserve the
	// access time of files analyzed.
	MAGIC_PRESERVE_ATIME MagicFlag = C.MAGIC_PRESERVE_ATIME

	// Don't translate unprintable characters to a \ooo octal representation.
	MAGIC_RAW MagicFlag = C.MAGIC_RAW

	// Treat operating system errors while trying to open files and follow
	// symlinks as real errors, instead of printing them in the magic buffer
	MAGIC_ERROR MagicFlag = C.MAGIC_ERROR

	// Return the Apple creator and type.
	MAGIC_APPLE MagicFlag = C.MAGIC_APPLE

	// Don't check for EMX application type (only on EMX).
	MAGIC_NO_CHECK_APPTYPE MagicFlag = C.MAGIC_NO_CHECK_APPTYPE

	// Don't get extra information on MS Composite Document Files.
	MAGIC_NO_CHECK_CDF MagicFlag = C.MAGIC_NO_CHECK_CDF

	// Don't look inside compressed files.
	MAGIC_NO_CHECK_COMPRESS MagicFlag = C.MAGIC_NO_CHECK_COMPRESS

	// Don't print ELF details.
	MAGIC_NO_CHECK_ELF MagicFlag = C.MAGIC_NO_CHECK_ELF

	// Don't check text encodings.
	MAGIC_NO_CHECK_ENCODING MagicFlag = C.MAGIC_NO_CHECK_ENCODING

	// Don't consult magic files.
	MAGIC_NO_CHECK_SOFT MagicFlag = C.MAGIC_NO_CHECK_SOFT

	// Don't examine tar files.
	MAGIC_NO_CHECK_TAR MagicFlag = C.MAGIC_NO_CHECK_TAR

	// Don't check for various types of text files.
	MAGIC_NO_CHECK_TEXT MagicFlag = C.MAGIC_NO_CHECK_TEXT

	// Don't look for known tokens inside ascii files.
	MAGIC_NO_CHECK_TOKENS MagicFlag = C.MAGIC_NO_CHECK_TOKENS
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL