forked from pgaskin/BookBrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formats.go
44 lines (36 loc) · 893 Bytes
/
formats.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package formats
import (
"image"
"path/filepath"
"strings"
"github.com/geek1011/BookBrowser/booklist"
"github.com/pkg/errors"
)
var formats = map[string]func(filename string) (BookInfo, error){}
type BookInfo interface {
Book() *booklist.Book
HasCover() bool
GetCover() (image.Image, error)
}
func Register(ext string, load func(filename string) (BookInfo, error)) {
ext = strings.ToLower(ext)
if _, ok := formats[ext]; ok {
panic("attempted to register existing format " + ext)
}
formats[ext] = load
}
func Load(filename string) (BookInfo, error) {
ext := strings.Replace(filepath.Ext(filename), ".", "", 1)
load, ok := formats[strings.ToLower(ext)]
if !ok {
return nil, errors.Errorf("could not load format %s", ext)
}
return load(filename)
}
func GetExts() []string {
exts := []string{}
for ext := range formats {
exts = append(exts, ext)
}
return exts
}