Initial Commit. Seems to work

This commit is contained in:
Brian Buller 2017-12-01 10:54:43 -06:00
commit 7873d4a2a3
2 changed files with 120 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
# Ignore the binary
jpg-metadata

118
main.go Normal file
View File

@ -0,0 +1,118 @@
package main
import (
"errors"
"flag"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/rwcarlsen/goexif/exif"
)
var inJson bool
var filename string
func init() {
const (
filenameUsage = "The File/Directory to scan"
inJsonDefault = false
inJsonUsage = "Output the result in JSON"
)
flag.StringVar(&filename, "filename", "", filenameUsage)
flag.BoolVar(&inJson, "json", inJsonDefault, inJsonUsage)
}
func main() {
flag.Parse()
fInfo, err := os.Lstat(filename)
if err != nil {
fmt.Println("Error reading passed file/directory")
os.Exit(1)
}
if fInfo.IsDir() {
ProcessDirectory(filename, inJson)
} else {
PrintJpegData(filename, inJson)
}
}
func ProcessDirectory(dirname string, inJson bool) {
if !inJson {
fmt.Println("Reading Directory:", dirname)
}
files, err := ioutil.ReadDir(dirname)
if err != nil {
panic(err)
}
if !strings.HasSuffix(dirname, "/") {
dirname += "/"
}
if inJson {
fmt.Print("[")
}
foundFirst := false
for _, file := range files {
if isJpeg(dirname+file.Name()) && inJson && foundFirst {
fmt.Print(",")
}
if PrintJpegData(dirname+file.Name(), inJson) == nil {
foundFirst = true
if !inJson {
fmt.Println("")
}
}
}
if inJson {
fmt.Print("]")
}
}
func isJpeg(filename string) bool {
nm := strings.ToUpper(filename)
return strings.HasSuffix(nm, ".JPG") || strings.HasSuffix(nm, ".JPEG")
}
func PrintJpegData(filename string, inJson bool) error {
if !isJpeg(filename) {
return errors.New("Not a JPEG")
}
exif, err := ReadExifData(filename)
if err != nil {
if !inJson {
fmt.Println("Error: " + err.Error())
return err
}
}
if inJson {
fmt.Print("{\"Filename\":\"" + filename + "\",\"Exif\":")
if exif == nil {
fmt.Print("{}")
} else {
json, err := exif.MarshalJSON()
if err != nil {
fmt.Print("{}")
}
fmt.Print(string(json))
}
fmt.Print("}")
} else {
fmt.Println("= " + filename + " =")
fmt.Println(exif)
}
return nil
}
func ReadExifData(filename string) (*exif.Exif, error) {
f, err := os.Open(filename)
if err != nil {
fmt.Println("Error:", err.Error())
return &exif.Exif{}, err
}
defer f.Close()
return exif.Decode(f)
}