2019-05-16 23:21:20 +03:00
|
|
|
// Copyright © 2018 Inanc Gumus
|
|
|
|
// Learn Go Programming Course
|
|
|
|
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
//
|
2019-10-30 19:34:44 +03:00
|
|
|
// For more tutorials : https://learngoprogramming.com
|
|
|
|
// In-person training : https://www.linkedin.com/in/inancgumus/
|
|
|
|
// Follow me on twitter: https://twitter.com/inancgumus
|
2019-05-16 23:21:20 +03:00
|
|
|
|
|
|
|
package magic
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-08-06 01:33:55 +03:00
|
|
|
"errors"
|
2019-05-16 23:21:20 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
2019-05-17 11:14:54 +03:00
|
|
|
// Detect returns the files that have a valid header (file signature).
|
|
|
|
// A valid header is determined by the format.
|
2019-05-16 23:21:20 +03:00
|
|
|
func Detect(format string, filenames []string) (valids []string, err error) {
|
|
|
|
header := headerOf(format)
|
2019-07-21 05:14:07 +03:00
|
|
|
if header == "unknown" {
|
|
|
|
err = fmt.Errorf("unknown format: %s", format)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-05-16 23:21:20 +03:00
|
|
|
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
|
2019-07-21 05:14:07 +03:00
|
|
|
// panic("unknown format: " + format)
|
|
|
|
return "unknown"
|
2019-05-16 23:21:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)) {
|
2019-08-06 01:33:55 +03:00
|
|
|
return errors.New("file size < len(buf)")
|
2019-05-16 23:21:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.ReadFull(file, buf)
|
|
|
|
return err
|
|
|
|
}
|