Add icon to sources

This commit is contained in:
nemunaire 2020-04-27 19:32:46 +02:00
parent 7beb01db5c
commit 563d84eeda
8 changed files with 101 additions and 6 deletions

View File

@ -1,7 +1,10 @@
package api
import (
"bytes"
"errors"
"io"
"net/http"
"reflect"
"strings"
@ -40,12 +43,30 @@ func getSourceSpecs(_ *config.Options, p httprouter.Params, body io.Reader) Resp
}
}
func getSourceSpecImg(ssid string) Response {
if cnt, ok := sources.Icons[strings.TrimSuffix(ssid, ".png")]; ok {
return &FileResponse{
contentType: "image/png",
content: bytes.NewBuffer(cnt),
}
} else {
return APIErrorResponse{
status: http.StatusNotFound,
err: errors.New("Icon not found."),
}
}
}
func getSourceSpec(_ *config.Options, p httprouter.Params, body io.Reader) Response {
ssid := string(p.ByName("ssid"))
if len(ssid) > 1 {
ssid = ssid[1:]
}
if strings.HasSuffix(ssid, ".png") {
return getSourceSpecImg(ssid)
}
src, err := sources.FindSource(ssid)
if err != nil {
return APIErrorResponse{

View File

@ -1,3 +1,4 @@
package main
//go:generate go run generators/gen_database_migration.go
//go:generate go run generators/gen_sources_icon.go

View File

@ -0,0 +1,59 @@
package main
// +build ignore
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"text/template"
)
const tpl = `// Code generated by go generate. DO NOT EDIT.
// sources:
{{ range $idx, $path := .Sources }}// {{ $path }}
{{ end }}
package sources // import "happydns.org/sources"
var Icons = map[string][]byte{
{{ range $file, $content := .Map }} {{printf "%q" $file}}: []byte({{printf "%q" $content}}),
{{ end }}}
`
var bundleTpl = template.Must(template.New("").Parse(tpl))
type valTpl struct {
Sources []string
Map map[string][]byte
}
func main() {
srcFiles, err := filepath.Glob("sources/*/*.png")
if err != nil {
panic(err)
}
d := valTpl{
Sources: []string{},
Map: map[string][]byte{},
}
for _, srcFile := range srcFiles {
data, err := ioutil.ReadFile(srcFile)
if err != nil {
panic(err)
}
d.Sources = append(d.Sources, srcFile)
d.Map["git.happydns.org/happydns/"+strings.TrimSuffix(srcFile, ".png")] = data
}
f, err := os.Create("sources/icons.go")
if err != nil {
panic(err)
}
defer f.Close()
bundleTpl.Execute(f, d)
}

View File

@ -8,8 +8,8 @@
<div class="d-flex flex-row justify-content-around flex-wrap align-self-center" v-if="!loading && mySources.length > 0">
<div type="button" @click="selectExistingSource(src)" class="p-3 source" v-for="(src, index) in mySources" v-bind:key="index">
<img :src="sources[src['_srctype']].icon" :alt="sources[src['_srctype']].name">
Utiliser {{ src.comment }}
<img :src="'/api/source_specs/' + src._srctype + '.png'" :alt="sources[src['_srctype']].name">
{{ src._comment }}
</div>
</div>
@ -17,9 +17,9 @@
<div class="d-flex flex-row justify-content-around flex-wrap align-self-center" v-if="!loading">
<div type="button" @click="selectNewSource(index)" class="p-3 source" v-for="(src, index) in sources" v-bind:key="index">
<img :src="src.icon" :alt="src.icon">
Utiliser {{ src.name }}<br>
<p class="text-muted">
<img :src="'/api/source_specs/' + index + '.png'" :alt="src.name">
{{ src.name }}<br>
<p class="text-muted" style="position: absolute;font-size: 80%;margin-top: 10.5em;width: 20%">
{{ src.description }}
</p>
</div>
@ -29,6 +29,9 @@
<div v-if="step === 1">
<b-row>
<b-col lg="4" md="5">
<div class="text-center mb-3">
<img :src="'/api/source_specs/' + source_specs_selected + '.png'" :alt="sources[source_specs_selected].name" style="max-width: 100%; max-height: 10em">
</div>
<h3>
{{ sources[source_specs_selected].name }}
</h3>

View File

@ -9,6 +9,9 @@
<b-row>
<b-col lg="4" md="5">
<div class="text-center mb-3">
<img :src="'/api/source_specs/' + source_specs_selected + '.png'" :alt="sources[source_specs_selected].name" style="max-width: 100%; max-height: 10em">
</div>
<h3>
{{ sources[source_specs_selected].name }}
</h3>

BIN
sources/ddns/DDNSServer.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 20 KiB

9
sources/icons.go Normal file

File diff suppressed because one or more lines are too long

View File

@ -4,6 +4,5 @@ import ()
type SourceInfos struct {
Name string `json:"name"`
Icon string `json:"icon"`
Description string `json:"description"`
}