~jheckt/PhotographyWebsite

7f4ac5cba45003db4d43ffadf494827afd3adc5b — turnipsoup 1 year, 3 months ago 4298ff2 + fcfafe4 main
Merge branch 'exif-preload'
2 files changed, 58 insertions(+), 15 deletions(-)

M main.go
M photos.go
M main.go => main.go +6 -2
@@ 11,19 11,23 @@ import (
const PHOTOPATH = "photos"
const AVERAGEPATH = "averages"

// Preloading the EXIF data for the images
var ExData = exifPreload()

func init() {
	// Output to stdout instead of the default stderr
	log.SetOutput(os.Stdout)

	// Only log the warning severity or above.
	log.SetLevel(log.DebugLevel)

	log.Info("Starting up...")

}

// Main
func main() {

	log.Info("Starting up...")

	// Serve the home page
	http.HandleFunc("/", serveHome)


M photos.go => photos.go +52 -13
@@ 27,6 27,11 @@ type Photo struct {
	AverageLoc  string        // Where the averages palette is stored for the <img> tag
}

// Holds all of the EXIF data loaded during preload
type ExifData struct {
	Photos map[string]map[string]interface{}
}

// Gets the filename of the passed string by removing the dot and everything after it
func getJustFilename(filename string) string {
	return strings.Split(filename, ".")[0]


@@ 38,6 43,7 @@ func getExifData(collection string, filename string) []map[string]interface{} {
	var objmap []map[string]interface{}

	// Execute the exiftool command against the filename, and collect the output
	log.Info(fmt.Sprintf("Getting EXIF Data from %s/%s/%s", PHOTOPATH, collection, filename))
	cmd, err := exec.Command("exiftool", "-json", fmt.Sprintf("%s/%s/%s.png", PHOTOPATH, collection, filename)).Output()

	if len(cmd) < 1 {


@@ 82,7 88,7 @@ func generatePhotoPage(collection string, filename string) (*template.Template, 
	photo.Desc = template.HTML(getPhotoDescription(photo))

	// Get exif data
	exif := getExifData(collection, filename)
	exif := ExData.Photos[photo.Name]

	// Check if we even returned EXIF data and if we did try to get the description of the lens
	if len(exif) < 1 {


@@ 91,24 97,24 @@ func generatePhotoPage(collection string, filename string) (*template.Template, 
		return t, photo
	} else {

		if exif[0]["LensID"] != nil {
			photo.Details = exif[0]["LensID"].(string)
		if exif["LensID"] != nil {
			photo.Details = exif["LensID"].(string)
		}
		if exif[0]["Model"] != nil {
			photo.CameraModel = exif[0]["Model"].(string)
		if exif["Model"] != nil {
			photo.CameraModel = exif["Model"].(string)
		}
		if exif[0]["DateTimeOriginal"] != nil {
			photo.PhotoDate = exif[0]["DateTimeOriginal"].(string)
		if exif["DateTimeOriginal"] != nil {
			photo.PhotoDate = exif["DateTimeOriginal"].(string)
		}

		if exif[0]["ExposureTime"] != nil {
			photo.PhotoTech = fmt.Sprintf("%s %s ", photo.PhotoTech, exif[0]["ExposureTime"].(string))
		if exif["ExposureTime"] != nil {
			photo.PhotoTech = fmt.Sprintf("%s %s ", photo.PhotoTech, exif["ExposureTime"].(string))
		}
		if exif[0]["FNumber"] != nil {
			photo.PhotoTech = fmt.Sprintf("%s f/%.1f ", photo.PhotoTech, exif[0]["FNumber"].(float64))
		if exif["FNumber"] != nil {
			photo.PhotoTech = fmt.Sprintf("%s f/%.1f ", photo.PhotoTech, exif["FNumber"].(float64))
		}
		if exif[0]["ISO"] != nil {
			photo.PhotoTech = fmt.Sprintf("%s %.0f ", photo.PhotoTech, exif[0]["ISO"].(float64))
		if exif["ISO"] != nil {
			photo.PhotoTech = fmt.Sprintf("%s %.0f ", photo.PhotoTech, exif["ISO"].(float64))
		}
	}



@@ 180,3 186,36 @@ func getPhotoDescription(p Photo) string {

	return buf.String()
}

// Preload all EXIF data
func exifPreload() ExifData {
	log.Info("Starting EXIF Preload")
	Photos := make(map[string]map[string]interface{})
	var ED = ExifData{Photos}

	photoDirs := loadPhotoDirectories()
	log.Info("Found Photo Directories ", photoDirs)

	for i := range photoDirs {
		photoFiles := loadCollectionFilesByType(photoDirs[i], "png", false)

		for pf := range photoFiles {
			cleanFilename := getJustFilename(photoFiles[pf])
			var photoExif = getExifData(photoDirs[i], cleanFilename)

			// Merge all maps in the slice into a single map
			mergedExif := make(map[string]interface{})
			for j := range photoExif {
				for k, v := range photoExif[j] {
					mergedExif[k] = v
				}
			}

			ED.Photos[cleanFilename] = mergedExif

		}
	}

	return ED

}