Browse Source

init

feature/autoindex
gryffyn 11 months ago
commit
93c8757c18
Signed by: gryffyn GPG Key ID: 6948DD6514D02BEF
  1. 5
      .gitignore
  2. 5
      go.mod
  3. 22
      main.go
  4. 48
      parser/exif.go
  5. 65
      parser/regex.go

5
.gitignore

@ -0,0 +1,5 @@
# Created by .ignore support plugin (hsz.mobi)
test.jpg
go.sum
.idea/

5
go.mod

@ -0,0 +1,5 @@
module git.neveris.one/gryffyn/gfn-fRen
go 1.15
require github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd

22
main.go

@ -0,0 +1,22 @@
package main
import (
"log"
"os"
"git.neveris.one/gryffyn/gfn-fRen/parser"
)
func main() {
format := `%DateTimeOriginal%-gryffyn.jpg`
path := "test.jpg"
exifData := parser.Exif{}
exifData.Open(path)
_ = exifData.Parse()
err := os.Rename(path, parser.ParseFormat(format, exifData.Tags))
if err != nil {
log.Fatal(err)
}
}
// gfn-fRen -n '%DateTimeOriginal%-gryffyn.jpg' test.jpg

48
parser/exif.go

@ -0,0 +1,48 @@
package parser
import (
"io"
"log"
"os"
goexif "github.com/rwcarlsen/goexif/exif"
"github.com/rwcarlsen/goexif/tiff"
)
type Tags map[string]*tiff.Tag
type Exif struct {
File io.Reader
Tags Tags
}
type walker struct {
ed *goexif.Exif
Tags Tags
}
func (e *Exif) Open(path string) {
f, err := os.Open(path)
defer f.Close()
if err != nil {
log.Print(err)
}
e.File = f
}
// Implements goexif.Walker()
func (w *walker) Walk(name goexif.FieldName, tag *tiff.Tag) error {
w.Tags[string(name)] = tag
return nil
}
// Extracts the exif data from the file, then parses it into tags.
func (e *Exif) Parse() error {
var x *goexif.Exif
var err error
x, err = goexif.Decode(e.File)
walker := &walker{ed: x, Tags: make(Tags)}
err = x.Walk(walker)
e.Tags = walker.Tags
return err
}

65
parser/regex.go

@ -0,0 +1,65 @@
package parser
import (
"regexp"
"strings"
"time"
"github.com/rwcarlsen/goexif/tiff"
)
// gfn-fRen -f '%DateTimeOriginal%-gryffyn.jpg' test.jpg
var validFields = []string{"ImageWidth", "ImageLength", "BitsPerSample", "Compression", "PhotometricInterpretation",
"Orientation", "SamplesPerPixel", "PlanarConfiguration", "YCbCrSubSampling", "YCbCrPositioning", "XResolution",
"YResolution", "ResolutionUnit", "DateTime", "ImageDescription", "Make", "Model", "Software", "Artist",
"Copyright", "ExifIFDPointer", "GPSInfoIFDPointer", "InteroperabilityIFDPointer", "ExifVersion", "FlashpixVersion",
"ColorSpace", "ComponentsConfiguration", "CompressedBitsPerPixel", "PixelXDimension", "PixelYDimension",
"MakerNote", "UserComment", "RelatedSoundFile", "DateTimeOriginal", "DateTimeDigitized", "SubSecTime",
"SubSecTimeOriginal", "SubSecTimeDigitized", "ImageUniqueID", "ExposureTime", "FNumber", "ExposureProgram",
"SpectralSensitivity", "ISOSpeedRatings", "OECF", "ShutterSpeedValue", "ApertureValue", "BrightnessValue",
"ExposureBiasValue", "MaxApertureValue", "SubjectDistance", "MeteringMode", "LightSource", "Flash", "FocalLength",
"SubjectArea", "FlashEnergy", "SpatialFrequencyResponse", "FocalPlaneXResolution", "FocalPlaneYResolution",
"FocalPlaneResolutionUnit", "SubjectLocation", "ExposureIndex", "SensingMethod", "FileSource", "SceneType",
"CFAPattern", "CustomRendered", "ExposureMode", "WhiteBalance", "DigitalZoomRatio", "FocalLengthIn35mmFilm",
"SceneCaptureType", "GainControl", "Contrast", "Saturation", "Sharpness", "DeviceSettingDescription",
"SubjectDistanceRange"}
var dateFields = []string{"DateTime", "DateTimeOriginal", "DateTimeDigitized"}
func contains(arr []string, str string) bool {
for _, n := range arr {
if str == n {
return true
}
}
return false
}
func parseDate(t *tiff.Tag) string {
inLay := "2006:01:02 15:04:05"
outLay := "2006-01-02"
n, _ := time.Parse(inLay, t.String())
return n.Format(outLay)
}
func getTagValue(t *tiff.Tag) string {
return t.String()
}
func sanitizeString(fieldname string, t *tiff.Tag) string {
val := getTagValue(t)
if contains(dateFields, fieldname) {
return parseDate(t)
}
return val
}
func ParseFormat(str string, t Tags) string {
r := regexp.MustCompile(`%\w+%`)
p := r.ReplaceAllStringFunc(str,
func(s string) string {
raw := strings.Replace(s, "%", "", 2)
return sanitizeString(raw, t[raw])
})
return p
}
Loading…
Cancel
Save