forked from pierrre/imageserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
101 lines (91 loc) · 2.58 KB
/
file.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Package file provides a imageserver.Server implementation that get the Image from a file.
package file
import (
"fmt"
"mime"
"os"
"path"
"path/filepath"
"strings"
"github.com/cognusion/imageserver"
imageserver_source "github.com/cognusion/imageserver/source"
)
// Server is a imageserver.Server implementation that get the Image from a file.
//
// It takes the "source" param and loads it from the Root directory.
// It expects a slash separated path.
type Server struct {
// Root is the directory where images are loaded from.
Root string
// Identify identifies the Image format.
// By default, it uses IdentifyMime().
Identify func(pth string, data []byte) (format string, err error)
}
// Get implements imageserver.Server.
func (srv *Server) Get(params imageserver.Params) (*imageserver.Image, error) {
pth, err := srv.getPath(params)
if err != nil {
return nil, err
}
data, err := loadFile(pth)
if err != nil {
return nil, err
}
format, err := srv.identify(pth, data)
if err != nil {
return nil, err
}
return &imageserver.Image{
Format: format,
Data: data,
}, nil
}
func (srv *Server) getPath(params imageserver.Params) (string, error) {
src, err := params.GetString(imageserver_source.Param)
if err != nil {
return "", err
}
// This trick comes from net/http.Dir.Open().
// It allows to "jail" the path inside the root.
return filepath.Join(srv.Root, filepath.FromSlash(path.Clean("/"+src))), nil
}
func loadFile(pth string) ([]byte, error) {
data, err := os.ReadFile(pth)
if err != nil {
return nil, newSourceError(fmt.Sprintf("error while reading file: %s: %s", pth, err.Error()))
}
return data, nil
}
func (srv *Server) identify(pth string, data []byte) (format string, err error) {
idf := srv.Identify
if idf == nil {
idf = IdentifyMime
}
format, err = idf(pth, data)
if err != nil {
return "", newSourceError(fmt.Sprintf("unable to identify image format: %s", err.Error()))
}
return format, nil
}
func newSourceError(msg string) error {
return &imageserver.ParamError{
Param: imageserver_source.Param,
Message: msg,
}
}
// IdentifyMime identifies the Image format with the "mime" package.
func IdentifyMime(pth string, data []byte) (format string, err error) {
ext := filepath.Ext(pth)
if ext == "" {
return "", fmt.Errorf("no file extension: %s", pth)
}
typ := mime.TypeByExtension(ext)
if typ == "" {
return "", fmt.Errorf("unkwnon file type for extension %s", ext)
}
const pref = "image/"
if !strings.HasPrefix(typ, pref) {
return "", fmt.Errorf("file type does not begin with \"%s\": %s", pref, typ)
}
return typ[len(pref):], nil
}