Giter Club home page Giter Club logo

magicmime's Introduction

magicmime Build Status GoDoc

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

This uses the libmagic library from the file tool, so you might need to install the development package for libmagic. On Debian or Ubuntu: apt-get install libmagic-dev. On RHEL, CentOS or Fedora: yum install file-devel. On Mac OS X: brew install libmagic. If you don't have the required package, 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

Examples

See godoc examples.

Docs

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.

magicmime's People

Contributors

ahmetb avatar c4milo avatar cgilling avatar dvrkps avatar jmsdncn avatar kaleworsley avatar kolyshkin avatar presbrey avatar rakyll avatar titxrf avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

magicmime's Issues

Open must Close if magic_setflags or magic_load fails

Currently, Open sequence doesn't handle the case where magic_setflags and magic_load fails. We need to close magic and free the underlying resources.

Also, we must document that Close must be called once user is done with magic.

Compilation on M1 mac

Just to note this solution somewhere in case someone else runs into it:

I was unable to compile the golang tool pistol on an M1 mac because homebrew installs files to /opt/homebrew/include† and /usr/local/include simply doesn't exist.
† or whatever your brew --prefix is set to.

❯ go install github.com/doronbehar/pistol/cmd/pistol@latest
# github.com/doronbehar/magicmime
Users/jonaustin/opt/_go/pkg/mod/github.com/doronbehar/[email protected]/magicmime.go:28:11: fatal error: 'magic.h' file not found
 #include <magic.h>
          ^~~~~~~~~
1 error generated.

Was able to fix this by simply symlinking things:

sudo ln -s /opt/homebrew/include /usr/local

Add travis build

libmagic dependencies are probably not there, but worth giving it a try.

M1 Mac error: libmagic.1.dylib': found architecture 'arm64', required architecture 'x86_64'

Hi,
I am installing magicmime via pistol and encounter this error on Macbook Air M1.

libmagic 5.45 (current latest) installed via brew. Has anyone the same issue? Should I downgrade libmagic on my machine? Thanks.

`
go install github.com/doronbehar/pistol/cmd/pistol@latest  !1/14
go: downloading github.com/doronbehar/pistol v0.4.2
go: downloading github.com/doronbehar/magicmime v0.1.1-0.20211127135329-3de4ff29dc49
go: downloading github.com/adrg/xdg v0.4.0
go: downloading github.com/alexflint/go-arg v1.4.3
go: downloading github.com/alessio/shellescape v1.4.1
go: downloading github.com/sirupsen/logrus v1.9.0
go: downloading github.com/alecthomas/chroma/v2 v2.7.0
go: downloading github.com/dustin/go-humanize v1.0.1
go: downloading github.com/mholt/archiver/v3 v3.5.1
go: downloading github.com/nwaples/rardecode v1.1.3
go: downloading github.com/alexflint/go-scalar v1.1.0
go: downloading golang.org/x/sys v0.7.0
go: downloading github.com/andybalholm/brotli v1.0.5
go: downloading github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5
go: downloading github.com/golang/snappy v0.0.4
go: downloading github.com/klauspost/compress v1.16.4
go: downloading github.com/klauspost/pgzip v1.2.5
go: downloading github.com/pierrec/lz4/v4 v4.1.17
go: downloading github.com/ulikunitz/xz v0.5.11
go: downloading github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8
go: downloading github.com/dlclark/regexp2 v1.9.0

github.com/doronbehar/magicmime

ld: warning: ignoring file '/opt/homebrew/Cellar/libmagic/5.45/lib/libmagic.1.dylib': found architecture 'arm64', required architecture 'x86_64'
ld: Undefined symbols:
_magic_buffer, referenced from:
__cgo_31903eabb88b_Cfunc_magic_buffer in _x002.o
_magic_close, referenced from:
__cgo_31903eabb88b_Cfunc_magic_close in _x002.o
_magic_error, referenced from:
__cgo_31903eabb88b_Cfunc_magic_error in _x002.o
_magic_file, referenced from:
__cgo_31903eabb88b_Cfunc_magic_file in _x002.o
_magic_load, referenced from:
__cgo_31903eabb88b_Cfunc_magic_load in _x002.o
_magic_open, referenced from:
__cgo_31903eabb88b_Cfunc_magic_open in _x002.o
_magic_setflags, referenced from:
__cgo_31903eabb88b_Cfunc_magic_setflags in _x002.o
_magic_version, referenced from:
__cgo_31903eabb88b_Cfunc_magic_version in _x002.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
`

Error in `./app': free(): invalid next size

*** Error in `./app': free(): invalid next size (fast): 0x00007f79400b5140 ***

======= Backtrace: =========
/lib64/libc.so.6(+0x7c503)[0x7f7971af5503]
/usr/lib64/libmagic.so.1(file_vprintf+0x6c)[0x7f797247330c]
/usr/lib64/libmagic.so.1(file_printf+0x87)[0x7f79724733e7]
/usr/lib64/libmagic.so.1(file_trycdf+0x4c7)[0x7f7972477157]
/usr/lib64/libmagic.so.1(file_buffer+0x1c6)[0x7f7972473726]
/usr/lib64/libmagic.so.1(magic_buffer+0x31)[0x7f7972467b61]
./app(_cgo_f7c060554b06_Cfunc_magic_buffer+0x1f)[0x8093df]
./app[0x459c60]

I'm using magicmime with multiple goroutines but with a mutex around magicmime.TypeByBuffer(blob)

fatal error: 'magic.h' file not found

When I try to download this package using go get github.com/rakyll/magicmime I get an error saying

# github.com/rakyll/magicmime
../../rakyll/magicmime/magicmime.go:28:11: fatal error: 'magic.h' file not found
 #include <magic.h>
          ^
1 error generated.

What could be the possible reason for this error?

crashes in production but not development

here is code, on the TypeByBuffer call I get the panic:

       err := magicmime.Open(magicmime.MAGIC_MIME_TYPE)
       mime, err := magicmime.TypeByBuffer(buffer)
       if err != nil {
               return err
       }
       magicmime.Close()
fatal error: unexpected signal during runtime execution
Jun 05 07:36:23 : /usr/lib/golang/src/runtime/panic.go:503 +0x8e
Jun 05 07:36:23 : runtime.sigpanic()
Jun 05 07:36:23 : /usr/lib/golang/src/runtime/sigpanic_unix.go:14 +0x5e
Jun 05 07:36:23 : goroutine 6581214 [syscall, locked to thread]:
Jun 05 07:36:23 : runtime.cgocall_errno(0x401610, 0xc2080e1698, 0xc200000000)
Jun 05 07:36:23 : /usr/lib/golang/src/runtime/cgocall.go:130 +0xf5 fp=0xc2080e16
Jun 05 07:36:23 : github.com/rakyll/magicmime._Cfunc_magic_buffer(0x7fece80008c0
Jun 05 07:36:23 : github.com/rakyll/magicmime/_obj/_cgo_gotypes.go:76 +0x44 fp=0
Jun 05 07:36:23 : github.com/rakyll/magicmime.TypeByBuffer(0xc2085fda00, 0x200,
Jun 05 07:36:23 : /home/aa/go/src/github.com/rakyll/magicmime/magicmime.go:151

Libmagic can not share handles across threads safely

I'm getting a crash in libmagic when using this library with multiple goroutimes. I suspect the reason is because this library keeps a single global handle that is used for all libmagic calls. A quick google search tells me that libmagic handles cannot be shared across threads.

Perhaps a better way to deal with this would be to wrap the libmagic handle as an opaque object that is passed back to the caller from Open(). Close() would also need a receiver or take an opaque handle as an argument.

Error: build constraints exclude all Go files in /home/user/go/bin/pkg/mod/github.com/rakyll/[email protected]

I am attempting to install Pistol, and I am running into an error involving magic mime.

doronbehar/pistol#76

The error states:

imports github.com/rakyll/magicmime: build constraints exclude all Go files in /home/user/go/bin/pkg/mod/github.com/rakyll/[email protected]

This is a new one for both me and the developer of pistol, and after doing some digging, an issue was discovered in the golang repository that mentions a potential solution to the issue by ensuring package main is used to denote the main script of the package.

golang/go#24068 (comment)

I attempted the suggested resolution, which created another error, because there is no main function in the source code.

So I am inquiring about a workaround that would allow me to use magicmime to install pistol.

Version tags

Hello @rakyll !

Is it possible to cut (semantic) version tags for magicmime? it would be easier to track it with dep.

Thank you for you work on the project.

The example in example_test.go has memeroy leak

The example in example_test.go has memeroy leak.

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)
    }
   defer mm.Close()
    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)
}

Take special attention for defer mm.Close()

cannot open; magic mime db is already open

Hi

I get cannot open; magic mime db is already open after my vscode crashed during tests and it obviously didn't close the db. However I cannot get it working again. If I ignore the error that it's already open, when I run magicmime.Close() I get invalid memory address or nil pointer dereference.

EDIT: this is still broken after rebooting

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.