-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjsonHandler.go
More file actions
146 lines (122 loc) · 3.62 KB
/
jsonHandler.go
File metadata and controls
146 lines (122 loc) · 3.62 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package iconifygo
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"strings"
)
type Icon struct {
Body string `json:"body"`
Left int `json:"left,omitempty"`
Top int `json:"top,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
HFlip bool `json:"hFlip,omitempty"`
VFlip bool `json:"vFlip,omitempty"`
Rotate int `json:"rotate,omitempty"`
}
type Alias struct {
Parent string `json:"parent"`
HFlip bool `json:"hFlip,omitempty"`
VFlip bool `json:"vFlip,omitempty"`
Rotate int `json:"rotate,omitempty"`
}
type IconSetResponse struct {
Prefix string `json:"prefix"`
LastModified uint `json:"lastModified,omitempty"`
Aliases map[string]Alias `json:"aliases,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
Icons map[string]Icon `json:"icons"`
NotFound []string `json:"not_found,omitempty"`
}
func loadIconSet(fileName, iconDir string) (IconSetResponse, error) {
var iconSet IconSetResponse
fullPath := filepath.Join(iconDir, fileName)
data, err := os.ReadFile(fullPath)
if err != nil {
return iconSet, err
}
if err := json.Unmarshal(data, &iconSet); err != nil {
return iconSet, err
}
return iconSet, nil
}
func parseIconSet(iconSet IconSetResponse, iconNames []string) (result IconSetResponse) {
var icons = make(map[string]Icon, len(iconNames))
var notFound []string
var aliases = make(map[string]Alias, len(iconNames))
for _, iconName := range iconNames {
icon, alias, err := getIconFromSet(&iconSet, iconName)
if err != nil {
notFound = append(notFound, iconName)
continue
}
if alias.Parent != "" {
aliases[iconName] = alias
icons[alias.Parent] = icon
continue
}
icons[iconName] = icon
}
result.Icons = icons
result.NotFound = notFound
result.Aliases = aliases
result.Prefix = iconSet.Prefix
result.LastModified = iconSet.LastModified
if iconSet.Width > 0 {
result.Width = iconSet.Width
}
if iconSet.Height > 0 {
result.Height = iconSet.Height
}
return result
}
func getIconFromSet(iconSet *IconSetResponse, iconName string) (Icon, Alias, error) {
if _, ok := iconSet.Icons[iconName]; ok {
return iconSet.Icons[iconName], Alias{}, nil
} else {
if _, ok := iconSet.Aliases[iconName]; ok {
alias := iconSet.Aliases[iconName]
if _, ok := iconSet.Icons[iconSet.Aliases[iconName].Parent]; ok {
icon := iconSet.Icons[iconSet.Aliases[iconName].Parent]
return icon, alias, nil
}
return Icon{}, Alias{}, fmt.Errorf("Parent not found for alias %s", iconName)
}
return Icon{}, Alias{}, fmt.Errorf("Icon %s not found", iconName)
}
}
func (s *IconifyServer) handleJSON(w http.ResponseWriter, r *http.Request) {
dirParts := strings.Split(r.URL.Path, "/")
file := strings.Split(r.URL.Path, "/")[len(dirParts)-1]
iconSet, err := loadIconSet(file, s.IconsetPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
http.Error(w, "File not found", http.StatusNotFound)
return
}
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
iconNames := strings.Split(r.URL.Query().Get("icons"), ",")
if len(iconNames) == 1 && iconNames[0] == "" {
http.Error(w, "No icons specified", http.StatusBadRequest)
return
}
result := parseIconSet(iconSet, iconNames)
response, err := json.Marshal(&result)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, err = w.Write(response)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}