-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
detect.go
76 lines (64 loc) · 1.56 KB
/
detect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
// For more tutorials : https://learngoprogramming.com
// In-person training : https://www.linkedin.com/in/inancgumus/
// Follow me on twitter: https://twitter.com/inancgumus
package magic
import (
"bytes"
"fmt"
"io"
"os"
)
// Detect returns the files that have a valid header (file signature).
// A valid header is determined by the format.
func Detect(format string, filenames []string) (valids []string, err error) {
defer func() {
if rerr := recover(); rerr != nil {
err = fmt.Errorf("cannot detect: %v", rerr)
}
}()
return detect(format, filenames), nil
}
func detect(format string, filenames []string) (valids []string) {
header := headerOf(format)
buf := make([]byte, len(header))
for _, filename := range filenames {
if read(filename, buf) != nil {
continue
}
if bytes.Equal([]byte(header), buf) {
valids = append(valids, filename)
}
}
return
}
func headerOf(format string) string {
switch format {
case "png":
return "\x89PNG\r\n\x1a\n"
case "jpg":
return "\xff\xd8\xff"
}
// this should never occur
panic("unknown format: " + format)
}
// read reads len(buf) bytes to buf from a file
func read(filename string, buf []byte) error {
file, err := os.Open(filename)
if err != nil {
return err
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
return err
}
if fi.Size() <= int64(len(buf)) {
return fmt.Errorf("file size < len(buf)")
}
_, err = io.ReadFull(file, buf)
return err
}