Revert "Refactor module-detection to use go/build instead of static file"

This reverts commit c23f7a1066.

The package-path will be a build-path rather than a source-path, and any
assets being referred to will not be found.
dustin/master
Dustin Oprea 2020-06-03 20:18:17 -04:00
parent eb6d1c16a5
commit 7a76510c2b
4 changed files with 63 additions and 27 deletions

0
v2/.MODULE_ROOT Normal file
View File

View File

@ -1,10 +1,9 @@
package exifcommon
import (
"os"
"path"
"go/build"
"encoding/binary"
"io/ioutil"
@ -24,32 +23,63 @@ var (
TestDefaultByteOrder = binary.BigEndian
)
// GetModuleRootPath returns our source-path when running from source during
// tests.
func GetModuleRootPath() string {
p, err := build.Default.Import(
"github.com/dsoprea/go-exif/v2",
build.Default.GOPATH,
build.FindOnly)
if moduleRootPath != "" {
return moduleRootPath
}
moduleRootPath := os.Getenv("EXIF_MODULE_ROOT_PATH")
if moduleRootPath != "" {
return moduleRootPath
}
currentWd, err := os.Getwd()
log.PanicIf(err)
packagePath := p.Dir
return packagePath
currentPath := currentWd
visited := make([]string, 0)
for {
tryStampFilepath := path.Join(currentPath, ".MODULE_ROOT")
_, err := os.Stat(tryStampFilepath)
if err != nil && os.IsNotExist(err) != true {
log.Panic(err)
} else if err == nil {
break
}
visited = append(visited, tryStampFilepath)
currentPath = path.Dir(currentPath)
if currentPath == "/" {
log.Panicf("could not find module-root: %v", visited)
}
}
return currentPath
}
func getTestAssetsPath() string {
moduleRootPath := GetModuleRootPath()
assetsPath := path.Join(moduleRootPath, "assets")
if assetsPath == "" {
moduleRootPath := GetModuleRootPath()
assetsPath = path.Join(moduleRootPath, "assets")
}
return assetsPath
}
func getTestImageFilepath() string {
return path.Join(assetsPath, "NDM_8901.jpg")
if testImageFilepath == "" {
assetsPath := getTestAssetsPath()
testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg")
}
return testImageFilepath
}
func getTestExifData() []byte {
assetsPath := getTestAssetsPath()
filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
var err error
@ -59,7 +89,3 @@ func getTestExifData() []byte {
return testExifData
}
func init() {
assetsPath = getTestAssetsPath()
}

View File

@ -173,7 +173,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
if len(data) < ExifSignatureLength {
if len(data) < 8 {
exifLogger.Warningf(nil, "Not enough data for EXIF header: (%d)", len(data))
return eh, ErrNoExif
}

View File

@ -13,7 +13,10 @@ import (
)
var (
assetsPath = ""
assetsPath = ""
testImageFilepath = ""
testGpsImageFilepath = ""
testExifData = make([]byte, 0)
)
@ -157,18 +160,25 @@ func validateExifSimpleTestIb(exifData []byte, t *testing.T) {
}
func getTestAssetsPath() string {
moduleRootPath := exifcommon.GetModuleRootPath()
assetsPath := path.Join(moduleRootPath, "assets")
if assetsPath == "" {
moduleRootPath := exifcommon.GetModuleRootPath()
assetsPath = path.Join(moduleRootPath, "assets")
}
return assetsPath
}
func getTestImageFilepath() string {
testImageFilepath := path.Join(assetsPath, "NDM_8901.jpg")
if testImageFilepath == "" {
assetsPath := getTestAssetsPath()
testImageFilepath = path.Join(assetsPath, "NDM_8901.jpg")
}
return testImageFilepath
}
func getTestExifData() []byte {
assetsPath := getTestAssetsPath()
filepath := path.Join(assetsPath, "NDM_8901.jpg.exif")
var err error
@ -180,10 +190,10 @@ func getTestExifData() []byte {
}
func getTestGpsImageFilepath() string {
testGpsImageFilepath := path.Join(assetsPath, "gps.jpg")
if testGpsImageFilepath == "" {
assetsPath := getTestAssetsPath()
testGpsImageFilepath = path.Join(assetsPath, "gps.jpg")
}
return testGpsImageFilepath
}
func init() {
assetsPath = getTestAssetsPath()
}