internal/format/format.go

61 lines · 1083 bytes

 1// Package format describes the file types the gallery can show.
 2package format
 3
 4import (
 5	"io"
 6	"path/filepath"
 7	"strings"
 8	"time"
 9)
10
11type Kind int
12
13const (
14	KindImage Kind = iota
15)
16
17// Meta holds display metadata. Width and Height are after EXIF orientation.
18type Meta struct {
19	Width, Height int
20	Taken         time.Time
21	Camera, Lens  string
22	Aperture      string
23	Shutter       string
24	ISO           int
25}
26
27type Format interface {
28	Name() string
29	Match(path string) bool
30	Metadata(path string) (Meta, error)
31	Resize(src string, width int, dst io.Writer) error
32	Kind() Kind
33}
34
35// Registry resolves a path to the first registered Format that matches it.
36type Registry struct {
37	formats []Format
38}
39
40func (r *Registry) Register(f Format) {
41	r.formats = append(r.formats, f)
42}
43
44func (r *Registry) Lookup(path string) Format {
45	for _, f := range r.formats {
46		if f.Match(path) {
47			return f
48		}
49	}
50	return nil
51}
52
53func hasExt(path string, exts []string) bool {
54	ext := strings.ToLower(filepath.Ext(path))
55	for _, e := range exts {
56		if ext == e {
57			return true
58		}
59	}
60	return false
61}