forked from karlssonerik/go-service-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_files.go
72 lines (57 loc) · 1.42 KB
/
static_files.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
package parser
import (
"io/ioutil"
"os"
"strings"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/SKF/go-service-doc/core"
"github.com/SKF/go-service-doc/utils"
)
func (p *Parser) findStaticFiles() {
zap.L().Info("search for static files")
files, err := ioutil.ReadDir(p.sourceDir + "/static")
if os.IsNotExist(err) {
return
}
if err != nil {
p.err = errors.Wrap(err, "ioutil.ReadDir failed")
return
}
for _, f := range files {
if f.IsDir() {
continue
}
var (
file core.File
fileExtension string
)
switch {
case strings.HasSuffix(f.Name(), ".svg"):
file.ContentType = "image/svg+xml"
fileExtension = ".svg"
case strings.HasSuffix(f.Name(), ".png"):
file.ContentType = "image/png"
fileExtension = ".png"
case strings.HasSuffix(f.Name(), ".ico"):
file.ContentType = "image/ico"
fileExtension = ".ico"
default:
continue
}
file.Path = p.outputDir + "/static/" + f.Name()
file.Href = p.basepath + "/static/" + utils.ConvertToKebabCase(f.Name())
if f.Name() == "favicon.ico" {
p.faviconHref = file.Href
}
file.Name = strings.ReplaceAll(f.Name(), fileExtension, "")
file.Name = utils.ConvertToCamelCase(file.Name)
filepath := p.sourceDir + "/static/" + f.Name()
file.Content, err = ioutil.ReadFile(filepath)
if err != nil {
p.err = errors.Wrap(err, "ioutil.ReadFile failed")
return
}
p.staticFiles = append(p.staticFiles, file)
}
}