Reorganized to not require GOPATH unless testing.

This commit is contained in:
Dustin Oprea 2018-06-16 09:49:14 -04:00
parent 7a8e5b005e
commit dfbe003c0e
4 changed files with 358 additions and 380 deletions

32
common_test.go Normal file
View File

@ -0,0 +1,32 @@
package exif
import (
"os"
"path"
"io/ioutil"
"github.com/dsoprea/go-logging"
)
var (
assetsPath = ""
testExifData = make([]byte, 0)
)
func init() {
goPath := os.Getenv("GOPATH")
if goPath == "" {
log.Panicf("GOPATH is empty")
}
assetsPath = path.Join(goPath, "src", "github.com", "dsoprea", "go-exif", "assets")
// Load test EXIF data.
filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
var err error
testExifData, err = ioutil.ReadFile(filepath)
log.PanicIf(err)
}

29
exif.go
View File

@ -1,13 +1,13 @@
package exif
import (
"os"
"errors"
"bytes"
"errors"
"fmt"
"os"
"io/ioutil"
"encoding/binary"
"io/ioutil"
"github.com/dsoprea/go-logging"
)
@ -32,22 +32,25 @@ var (
// EncodeDefaultByteOrder is the default byte-order for encoding operations.
EncodeDefaultByteOrder = binary.BigEndian
BigEndianBoBytes = [2]byte { 'M', 'M' }
LittleEndianBoBytes = [2]byte { 'I', 'I' }
// Default byte order for tests.
TestDefaultByteOrder = binary.BigEndian
ByteOrderLookup = map[[2]byte]binary.ByteOrder {
BigEndianBoBytes = [2]byte{'M', 'M'}
LittleEndianBoBytes = [2]byte{'I', 'I'}
ByteOrderLookup = map[[2]byte]binary.ByteOrder{
BigEndianBoBytes: binary.BigEndian,
LittleEndianBoBytes: binary.LittleEndian,
}
ByteOrderLookupR = map[binary.ByteOrder][2]byte {
ByteOrderLookupR = map[binary.ByteOrder][2]byte{
binary.BigEndian: BigEndianBoBytes,
binary.LittleEndian: LittleEndianBoBytes,
}
ExifFixedBytesLookup = map[binary.ByteOrder][2]byte {
binary.LittleEndian: [2]byte { 0x2a, 0x00 },
binary.BigEndian: [2]byte { 0x00, 0x2a },
ExifFixedBytesLookup = map[binary.ByteOrder][2]byte{
binary.LittleEndian: [2]byte{0x2a, 0x00},
binary.BigEndian: [2]byte{0x00, 0x2a},
}
)
@ -56,7 +59,6 @@ var (
ErrExifHeaderError = errors.New("exif header error")
)
// SearchAndExtractExif returns a slice from the beginning of the EXIF data the
// end of the file (it's not practical to try and calculate where the data
// actually ends).
@ -115,7 +117,6 @@ func SearchFileAndExtractExif(filepath string) (rawExif []byte, err error) {
return rawExif, nil
}
type ExifHeader struct {
ByteOrder binary.ByteOrder
FirstIfdOffset uint32
@ -141,7 +142,7 @@ func ParseExifHeader(data []byte) (eh ExifHeader, err error) {
// CIPA DC-008-2016; JEITA CP-3451D
// -> http://www.cipa.jp/std/documents/e/DC-008-Translation-2016-E.pdf
byteOrderBytes := [2]byte { data[0], data[1] }
byteOrderBytes := [2]byte{data[0], data[1]}
byteOrder, found := ByteOrderLookup[byteOrderBytes]
if found == false {
@ -149,7 +150,7 @@ func ParseExifHeader(data []byte) (eh ExifHeader, err error) {
log.Panic(ErrNoExif)
}
fixedBytes := [2]byte { data[2], data[3] }
fixedBytes := [2]byte{data[2], data[3]}
expectedFixedBytes := ExifFixedBytesLookup[byteOrder]
if fixedBytes != expectedFixedBytes {
exifLogger.Warningf(nil, "EXIF header fixed-bytes should be [%v] but are: [%v]", expectedFixedBytes, fixedBytes)

29
tags.go
View File

@ -1,12 +1,10 @@
package exif
import (
"os"
"path"
"fmt"
"gopkg.in/yaml.v2"
"github.com/dsoprea/go-logging"
"gopkg.in/yaml.v2"
)
const (
@ -32,11 +30,9 @@ const (
)
var (
tagDataFilepath = ""
// tagsWithoutAlignment is a tag-lookup for tags whose value size won't
// necessarily be a multiple of its tag-type.
tagsWithoutAlignment = map[uint16]struct{} {
tagsWithoutAlignment = map[uint16]struct{}{
// The thumbnail offset is stored as a long, but its data is a binary
// blob (not a slice of longs).
ThumbnailOffsetTagId: struct{}{},
@ -49,7 +45,6 @@ var (
tagsLogger = log.NewLogger("exif.tags")
)
// File structures.
type encodedTag struct {
@ -60,7 +55,6 @@ type encodedTag struct {
TypeName string `yaml:"type_name"`
}
// Indexing structures.
type IndexedTag struct {
@ -104,7 +98,6 @@ func (ti *TagIndex) load() (err error) {
return nil
}
// Read static data.
encodedIfds := make(map[string][]encodedTag)
@ -112,7 +105,6 @@ func (ti *TagIndex) load() (err error) {
err = yaml.Unmarshal([]byte(tagsYaml), encodedIfds)
log.PanicIf(err)
// Load structure.
tagsByIfd := make(map[string]map[uint16]*IndexedTag)
@ -125,10 +117,10 @@ func (ti *TagIndex) load() (err error) {
tagName := tagInfo.Name
tagTypeName := tagInfo.TypeName
// TODO(dustin): !! Non-standard types, but found in real data. Ignore for right now.
if tagTypeName == "SSHORT" || tagTypeName == "FLOAT" || tagTypeName == "DOUBLE" {
// TODO(dustin): !! Non-standard types, but found in real data. Ignore for right now.
if tagTypeName == "SSHORT" || tagTypeName == "FLOAT" || tagTypeName == "DOUBLE" {
continue
}
}
tagTypeId, found := TypeNamesR[tagTypeName]
if found == false {
@ -143,7 +135,6 @@ if tagTypeName == "SSHORT" || tagTypeName == "FLOAT" || tagTypeName == "DOUBLE"
Type: tagTypeId,
}
// Store by ID.
family, found := tagsByIfd[ifdName]
@ -158,7 +149,6 @@ if tagTypeName == "SSHORT" || tagTypeName == "FLOAT" || tagTypeName == "DOUBLE"
family[tagId] = tag
// Store by name.
familyR, found := tagsByIfdR[ifdName]
@ -228,7 +218,6 @@ func (ti *TagIndex) GetWithName(ii IfdIdentity, name string) (it *IndexedTag, er
return it, nil
}
// IfdTagWithId returns true if the given tag points to a child IFD block.
func IfdTagNameWithIdOrFail(parentIfdName string, tagId uint16) string {
name, found := IfdTagNameWithId(parentIfdName, tagId)
@ -319,13 +308,5 @@ func IfdId(parentIfdName, ifdName string) (ii IfdIdentity, id int) {
}
func init() {
goPath := os.Getenv("GOPATH")
if goPath == "" {
log.Panicf("GOPATH is empty")
}
assetsPath := path.Join(goPath, "src", "github.com", "dsoprea", "go-exif", "assets")
tagDataFilepath = path.Join(assetsPath, "tags.yaml")
tagIndex = NewTagIndex()
}

View File

@ -1,36 +0,0 @@
package exif
import (
"os"
"path"
"encoding/binary"
"io/ioutil"
"github.com/dsoprea/go-logging"
)
var (
TestDefaultByteOrder = binary.BigEndian
assetsPath = ""
testExifData = make([]byte, 0)
)
func init() {
goPath := os.Getenv("GOPATH")
if goPath == "" {
log.Panicf("GOPATH is empty")
}
assetsPath = path.Join(goPath, "src", "github.com", "dsoprea", "go-exif", "assets")
// Load test EXIF data.
filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
var err error
testExifData, err = ioutil.ReadFile(filepath)
log.PanicIf(err)
}