-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.go
59 lines (47 loc) · 1.51 KB
/
local.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
package fetchers
import (
"bytes"
"io"
"io/ioutil"
"path"
"strings"
"github.com/erans/thumbla/utils"
"github.com/labstack/echo/v4"
)
// LocalFetcher fetches content from http/https sources
type LocalFetcher struct {
Name string
FetcherType string
Path string
}
// Fetch returns content from the local machine
func (fetcher *LocalFetcher) Fetch(c echo.Context, url string) (io.Reader, string, error) {
filename := strings.Replace(url, "local://", "", -1)
fileFullPath := path.Join(fetcher.Path, filename)
c.Logger().Debugf("File to load: %s", fileFullPath)
var buf []byte
var err error
if buf, err = ioutil.ReadFile(fileFullPath); err != nil {
return nil, "", err
}
c.Logger().Debugf("url=%s GetMimeTypeByFileExt=%s", url, utils.GetMimeTypeByFileExt(url))
return bytes.NewReader(buf), utils.GetMimeTypeByFileExt(url), nil
}
// GetName returns the name assigned to this fetcher that can be used in the 'paths' section
func (fetcher *LocalFetcher) GetName() string {
return fetcher.Name
}
// GetFetcherType returns the type of this fetcher to be used in the 'type' properties when defining fetchers
func (fetcher *LocalFetcher) GetFetcherType() string {
return fetcher.FetcherType
}
// NewLocalFetcher creates a new fetcher that support http/https
func NewLocalFetcher(cfg map[string]interface{}) *LocalFetcher {
var name, _ = cfg["name"]
var path, _ = cfg["path"]
return &LocalFetcher{
Name: utils.SafeCastToString(name),
FetcherType: "local",
Path: utils.SafeCastToString(path),
}
}