Skip to content

Instantly share code, notes, and snippets.

@danp
Created September 4, 2014 13:31
Show Gist options
  • Save danp/ee8c5aa7dacd33b60f56 to your computer and use it in GitHub Desktop.
Save danp/ee8c5aa7dacd33b60f56 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"crypto/sha1"
"fmt"
"image"
"io"
"os"
"os/exec"
"time"
_ "image/jpeg"
_ "image/png"
"github.com/rwcarlsen/goexif/exif"
)
func process(fn string) {
f, err := os.Open(fn)
if err != nil {
fmt.Fprintf(os.Stderr, "file=%q action=open err=%q\n", fn, err)
return
}
defer f.Close()
stat, err := f.Stat()
if err != nil {
fmt.Fprintf(os.Stderr, "file=%q action=stat err=%q\n", fn, err)
return
}
img, format, err := image.Decode(f)
if err != nil {
fmt.Fprintf(os.Stderr, "file=%q action=image_decode err=%q\n", fn, err)
return
}
if _, err := f.Seek(0, 0); err != nil {
fmt.Fprintf(os.Stderr, "file=%q action=rewind err=%q\n", fn, err)
return
}
exif, err := exif.Decode(f)
if err != nil {
fmt.Fprintf(os.Stderr, "file=%q image_format=%q action=exif_decode err=%q\n", fn, format, err)
}
var exifTime time.Time
if exif != nil {
dateTimeTag, err := exif.Get("DateTime")
if err != nil {
fmt.Fprintf(os.Stderr, "file=%q image_format=%q action=exif_get_datetime err=%q\n", fn, format, err)
} else {
exifTime, _ = time.ParseInLocation("2006:01:02 15:04:05", dateTimeTag.StringVal(), time.Local)
}
}
bounds := img.Bounds()
var canonTime time.Time
if exifTime.IsZero() {
canonTime = stat.ModTime()
} else {
canonTime = exifTime
}
if _, err := f.Seek(0, 0); err != nil {
fmt.Fprintf(os.Stderr, "file=%q action=rewind err=%q\n", fn, err)
return
}
shaHash := sha1.New()
if _, err := io.Copy(shaHash, f); err != nil {
fmt.Fprintf(os.Stderr, "file=%q image_format=%q action=shasum err=%q\n", fn, format, err)
return
}
dir := fmt.Sprintf("final/%s", canonTime.Format("2006/01/02"))
path := fmt.Sprintf("%s/%s_%x.%s", dir, canonTime.Format("2006-01-02-15-04-05"), shaHash.Sum(nil), format)
fmt.Printf("file=%q mtime=%q image_format=%q resolution=%v exif_time=%q canon_time=%q path=%q\n", fn, stat.ModTime(), format, bounds.Max, exifTime, canonTime, path)
os.MkdirAll(dir, 0755)
cpCmd := exec.Command("cp", "-p", fn, path)
if err := cpCmd.Run(); err != nil {
fmt.Fprintf(os.Stderr, "file=%q image_format=%q path=%q action=cp err=%q\n", fn, format, path, err)
}
}
func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fn := scanner.Text()
process(fn)
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment