Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/generate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Generate
on:
push:
branches:
- master
schedule:
- cron: '0 21 * * *'
workflow_dispatch:
jobs:
generate:
name: generate
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version-file: go.mod
cache: true
cache-dependency-path: go.sum
- name: Run make generate
run: make generate
- name: Commit and push if changed
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
git diff --staged --quiet || (git commit -m "chore: Auto-generate latest signatures" && git push)
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
freedesktop.org.xml
363 changes: 339 additions & 24 deletions LICENSE

Large diffs are not rendered by default.

11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
golangcilint_version := "1.53.3"
golangcilint_version := "2.6.1"

default : lint test

.PHONY: lint
lint:
@echo "Linting..."
@(which golangci-lint && [[ "$$(golangci-lint --version | awk '{print $$4}')" == "$(golangcilint_version)" ]] ) || go install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(golangcilint_version)
@(which golangci-lint && [[ "$$(golangci-lint --version | awk '{print $$4}')" == "$(golangcilint_version)" ]] ) || go install github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v$(golangcilint_version)
@golangci-lint run

.PHONY: test
test:
go test -race ./...

.PHONY: generate
generate: freedesktop.org.xml
go generate

freedesktop.org.xml:
curl -o freedesktop.org.xml https://gitlab.freedesktop.org/xdg/shared-mime-info/-/raw/master/data/freedesktop.org.xml.in
51 changes: 30 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,44 @@
# magic

Toolkit for detecting and verifying file type using magic bytes in pure Go
Toolkit for detecting and verifying file type using magic bytes in pure Go.

Support for all file signatures listed [here](https://en.wikipedia.org/wiki/List_of_file_signatures).
Support for all file signatures supported by [FreeDesktop](https://gitlab.freedesktop.org/xdg/shared-mime-info/-/raw/master/data/freedesktop.org.xml.in) (and a few more.)

You only need to provide the first few hundred bytes of a given file to detect the file type, unless you want to detect `.iso` images, which require examination of the first 32774 bytes.
A MIME type, description, and a suggested file extension and icon name are provided for each lookup.

A description and a suggested file extension are provided where relevant, and MIME types will be added in future.
A binary is also included for ease of use.

## Example Usage
## Binary Usage

```go
package main
```sh
$ go install github.com/liamg/magic/cmd/latest@latest
$ magic /path/to/file
```

import "github.com/liamg/magic"
## Module Usage

func main() {
See the [docs](https://pkg.go.dev/github.com/liamg/magic) for full details.

data := []byte{0xa1, 0xb2, 0xc3, 0xd4, 0x00, 0x00, 0x00, 0x00}
```go
package main

fileType, err := magic.Lookup(data)
if err != nil {
if err == magic.ErrUnknown {
fmt.Println("File type is unknown")
os.Exit(1)
}else{
panic(err)
}
}
import (
"fmt"
"os"

fmt.Printf("File extension: %s\n", fileType.Extension)
fmt.Printf("File type description: %s\n", fileType.Description)
"github.com/liamg/magic"
)

func main() {
ft, err := magic.IdentifyPath(os.Args[1])
if err != nil {
fmt.Printf("\x1b[31mError identifying file: %s\x1b[0m\n", err)
os.Exit(1)
}

fmt.Printf("File %s\n", os.Args[1])
fmt.Printf("Description %s\n", ft.Description)
fmt.Printf("MIME %s\n", ft.MIME)
fmt.Printf("Icon %s\n", ft.Icon)
}
```
Loading