diff options
author | Adam Mathes <adam@trenchant.org> | 2017-09-30 16:50:07 -0700 |
---|---|---|
committer | Adam Mathes <adam@trenchant.org> | 2017-09-30 16:50:07 -0700 |
commit | ffb1ad446af8b1951ff0721736acad79628a716d (patch) | |
tree | 5e8848ed6a1027485fe39a6826d4248935d3e102 /post/post.go | |
parent | c86206a673525cd8401750400517c5aa2875d8ec (diff) | |
download | snkt-ffb1ad446af8b1951ff0721736acad79628a716d.tar.gz snkt-ffb1ad446af8b1951ff0721736acad79628a716d.tar.bz2 snkt-ffb1ad446af8b1951ff0721736acad79628a716d.zip |
enable non-text posts, like images
Diffstat (limited to 'post/post.go')
-rw-r--r-- | post/post.go | 72 |
1 files changed, 66 insertions, 6 deletions
diff --git a/post/post.go b/post/post.go index 792635f..25c9883 100644 --- a/post/post.go +++ b/post/post.go @@ -10,13 +10,15 @@ import ( "adammathes.com/snkt/text" "adammathes.com/snkt/vlog" "github.com/russross/blackfriday" + "github.com/rwcarlsen/goexif/exif" "io/ioutil" "log" "os" "path" + "path/filepath" "strconv" "strings" - "time" + "time" ) var Template = "post" @@ -58,7 +60,9 @@ type Post struct { RssDate string FileInfo os.FileInfo - + Extension string + ContentType string + Site sitemeta } @@ -95,12 +99,55 @@ func (p *Post) Read(fi os.FileInfo) { p.FileInfo = fi p.SourceFile = p.FileInfo.Name() var err error - p.Raw, err = ioutil.ReadFile(path.Join(config.Config.TxtDir, fi.Name())) - if err != nil { - log.Println(err) + + + // this is an abominaion + + ext := filepath.Ext(fi.Name()) + // ext includes the '.' + if len(ext)>1 { + p.Extension = strings.ToLower(ext[1:]) + } + + switch p.Extension { + case "bmp", "gif", "jpg", "jpeg", "png", "tiff": + p.ContentType = "image" + p.Unparsed = "" + p.parseExif() + // parse as image + case "mp4", "mpeg": + p.ContentType = "video" + p.Unparsed = "" + // parse as image + case "mp3": + p.ContentType = "audio" + p.Unparsed = "" + // parse as image + default: + p.ContentType = "text" + p.Raw, err = ioutil.ReadFile(path.Join(config.Config.TxtDir, p.FileInfo.Name())) + if err != nil { + log.Println(err) + } + p.Unparsed = string(p.Raw) } - p.Unparsed = string(p.Raw) p.parse() + // end abomination +} + +func (p *Post) AbsoluteFilePath() string { + return path.Join(config.Config.TxtDir, p.FileInfo.Name()) +} + +/* +Try to extract metadata from EXIF +*/ +func (p *Post) parseExif() { + // TODO: exif parsing? + f,_ := os.Open(p.AbsoluteFilePath()) + x,_ := exif.Decode(f) + tm,_ := x.DateTime() + p.Time = tm } /* @@ -193,6 +240,13 @@ func (p *Post) ParseFmt(s string) string { } func (p *Post) parseDates() { + + // in the case of exif + if (p.Time != time.Time{} ) { + p.fillDates() + return + } + // // Dates // @@ -219,7 +273,13 @@ func (p *Post) parseDates() { p.Time = p.FileInfo.ModTime() } } + p.fillDates() +} +/* +Given p.Time is correct, create the other derived date fields +*/ +func (p *Post) fillDates() { p.Year, p.Month, p.Day = p.Time.Date() /* golang date format refresher 1 2 3 4 5 7 6 |