|
|
@ -9,18 +9,23 @@ import ( |
|
|
|
"path" |
|
|
|
"path" |
|
|
|
"path/filepath" |
|
|
|
"path/filepath" |
|
|
|
"regexp" |
|
|
|
"regexp" |
|
|
|
|
|
|
|
"sort" |
|
|
|
"strconv" |
|
|
|
"strconv" |
|
|
|
|
|
|
|
"time" |
|
|
|
|
|
|
|
|
|
|
|
// for processing images
|
|
|
|
// for processing images
|
|
|
|
_ "image/gif" |
|
|
|
_ "image/gif" |
|
|
|
_ "image/png" |
|
|
|
_ "image/png" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/nfnt/resize" |
|
|
|
"github.com/nfnt/resize" |
|
|
|
|
|
|
|
"github.com/tajtiattila/metadata" |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
type MediaSource struct { |
|
|
|
type MediaSource struct { |
|
|
|
StorageDir string |
|
|
|
StorageDir string |
|
|
|
ThumbDir string |
|
|
|
ThumbDir string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set Set |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) LocationOrig(m Media) string { |
|
|
|
func (ms *MediaSource) LocationOrig(m Media) string { |
|
|
@ -49,6 +54,8 @@ func (ms *MediaSource) ThumbFilename(m Media, size image.Rectangle) string { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) ReceiveNewMedia(name string, r io.Reader) error { |
|
|
|
func (ms *MediaSource) ReceiveNewMedia(name string, r io.Reader) error { |
|
|
|
|
|
|
|
ms.set = nil |
|
|
|
|
|
|
|
|
|
|
|
dest := path.Join(ms.StorageDir, name) |
|
|
|
dest := path.Join(ms.StorageDir, name) |
|
|
|
f, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE, 0644) |
|
|
|
f, err := os.OpenFile(dest, os.O_WRONLY|os.O_CREATE, 0644) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
@ -67,6 +74,47 @@ type Media struct { |
|
|
|
Type string |
|
|
|
Type string |
|
|
|
Name string |
|
|
|
Name string |
|
|
|
Size image.Rectangle |
|
|
|
Size image.Rectangle |
|
|
|
|
|
|
|
FullName string |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
metadata *metadata.Metadata |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Media) Date() time.Time { |
|
|
|
|
|
|
|
m.getMetadata() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if m.metadata != nil { |
|
|
|
|
|
|
|
raw := m.metadata.Get(metadata.DateTimeOriginal) |
|
|
|
|
|
|
|
if raw != "" { |
|
|
|
|
|
|
|
d, err := time.Parse("2006-01-02T15:04:05", raw) |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
|
|
|
|
return d |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fi, err := os.Stat(m.FullName) |
|
|
|
|
|
|
|
if err == nil { |
|
|
|
|
|
|
|
return fi.ModTime() |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return time.Time{} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (m *Media) getMetadata() error { |
|
|
|
|
|
|
|
if m.metadata != nil { |
|
|
|
|
|
|
|
return nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
f, err := os.Open(m.FullName) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
md, err := metadata.Parse(f) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return err |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
m.metadata = md |
|
|
|
|
|
|
|
return nil |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) Size(name string) (image.Rectangle, error) { |
|
|
|
func (ms *MediaSource) Size(name string) (image.Rectangle, error) { |
|
|
@ -136,11 +184,13 @@ func (ms *MediaSource) HasThumb(m Media, size image.Rectangle) bool { |
|
|
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) ByName(name string) *Media { |
|
|
|
func (ms *MediaSource) ByName(name string) *Media { |
|
|
|
size, _ := ms.Size(path.Join(ms.StorageDir, name)) |
|
|
|
size, _ := ms.Size(path.Join(ms.StorageDir, name)) |
|
|
|
return &Media{ |
|
|
|
m := Media{ |
|
|
|
Type: "image", |
|
|
|
Type: "image", |
|
|
|
Name: name, |
|
|
|
Name: name, |
|
|
|
Size: size, |
|
|
|
Size: size, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
m.FullName = ms.LocationOrig(m) |
|
|
|
|
|
|
|
return &m |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) Thumb(m Media, size image.Rectangle) (string, error) { |
|
|
|
func (ms *MediaSource) Thumb(m Media, size image.Rectangle) (string, error) { |
|
|
@ -187,6 +237,7 @@ func (ms *MediaSource) ThumbImage(img image.Image, m Media, size image.Rectangle |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (ms *MediaSource) Walk() ([]*Media, error) { |
|
|
|
func (ms *MediaSource) Walk() ([]*Media, error) { |
|
|
|
|
|
|
|
if ms.set == nil { |
|
|
|
var media []*Media |
|
|
|
var media []*Media |
|
|
|
|
|
|
|
|
|
|
|
err := filepath.Walk(ms.StorageDir, func(name string, fi os.FileInfo, err error) error { |
|
|
|
err := filepath.Walk(ms.StorageDir, func(name string, fi os.FileInfo, err error) error { |
|
|
@ -200,9 +251,25 @@ func (ms *MediaSource) Walk() ([]*Media, error) { |
|
|
|
return nil |
|
|
|
return nil |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err != nil { |
|
|
|
return media, err |
|
|
|
return media, err |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ms.set = media |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return ms.set, nil |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Set []*Media |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s Set) ByDate() Set { |
|
|
|
|
|
|
|
sort.Slice(s, func(i, j int) bool { |
|
|
|
|
|
|
|
return s[i].Date().After(s[j].Date()) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
return s |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var ( |
|
|
|
var ( |
|
|
|
sizeString = regexp.MustCompile(`([0-9]*)(x)?([0-9]*)`) |
|
|
|
sizeString = regexp.MustCompile(`([0-9]*)(x)?([0-9]*)`) |
|
|
|
) |
|
|
|
) |
|
|
|