mirror of
https://github.com/1f349/violet.git
synced 2024-11-23 20:01:35 +00:00
Simplify favicon outputting code
This commit is contained in:
parent
92fa325fd5
commit
a63dd95201
@ -4,6 +4,7 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/MrMelon54/png2ico"
|
"github.com/MrMelon54/png2ico"
|
||||||
"image/png"
|
"image/png"
|
||||||
@ -18,6 +19,27 @@ type FaviconList struct {
|
|||||||
Svg *FaviconImage
|
Svg *FaviconImage
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ErrInvalidFaviconExtension = errors.New("invalid favicon extension")
|
||||||
|
|
||||||
|
// ProduceForExt outputs the bytes for the ico/png/svg icon and the HTTP
|
||||||
|
// Content-Type header to output.
|
||||||
|
func (l *FaviconList) ProduceForExt(ext string) (raw []byte, contentType string, err error) {
|
||||||
|
switch ext {
|
||||||
|
case ".ico":
|
||||||
|
contentType = "image/x-icon"
|
||||||
|
raw, err = l.ProduceIco()
|
||||||
|
case ".png":
|
||||||
|
contentType = "image/png"
|
||||||
|
raw, err = l.ProducePng()
|
||||||
|
case ".svg":
|
||||||
|
contentType = "image/svg+xml"
|
||||||
|
raw, err = l.ProduceSvg()
|
||||||
|
default:
|
||||||
|
err = ErrInvalidFaviconExtension
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// ProduceIco outputs the bytes of the ico icon or an error
|
// ProduceIco outputs the bytes of the ico icon or an error
|
||||||
func (l *FaviconList) ProduceIco() ([]byte, error) {
|
func (l *FaviconList) ProduceIco() ([]byte, error) {
|
||||||
if l.Ico == nil {
|
if l.Ico == nil {
|
||||||
|
@ -18,9 +18,10 @@ var snakeOilProv = genSnakeOilProv()
|
|||||||
|
|
||||||
type fakeDomains struct{}
|
type fakeDomains struct{}
|
||||||
|
|
||||||
func (f *fakeDomains) IsValid(host string) bool { return host == "example.com" }
|
func (f *fakeDomains) IsValid(host string) bool { return host == "example.com" }
|
||||||
func (f *fakeDomains) Put(domain string, active bool) {}
|
func (f *fakeDomains) Put(string, bool) {}
|
||||||
func (f *fakeDomains) Delete(domain string) {}
|
func (f *fakeDomains) Delete(string) {}
|
||||||
|
func (f *fakeDomains) Compile() {}
|
||||||
|
|
||||||
func genSnakeOilProv() mjwt.Signer {
|
func genSnakeOilProv() mjwt.Signer {
|
||||||
key, err := rsa.GenerateKey(rand.Reader, 1024)
|
key, err := rsa.GenerateKey(rand.Reader, 1024)
|
||||||
|
@ -10,6 +10,7 @@ import (
|
|||||||
"log"
|
"log"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"path"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -69,33 +70,14 @@ func setupFaviconMiddleware(fav *favicons.Favicons, next http.Handler) http.Hand
|
|||||||
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
return http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
|
||||||
if req.Header.Get("X-Violet-Raw-Favicon") != "1" {
|
if req.Header.Get("X-Violet-Raw-Favicon") != "1" {
|
||||||
switch req.URL.Path {
|
switch req.URL.Path {
|
||||||
case "/favicon.svg":
|
case "/favicon.svg", "/favicon.png", "/favicon.ico":
|
||||||
icons := fav.GetIcons(req.Host)
|
icons := fav.GetIcons(req.Host)
|
||||||
raw, err := icons.ProduceSvg()
|
raw, contentType, err := icons.ProduceForExt(path.Ext(req.URL.Path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.RespondVioletError(rw, http.StatusTeapot, "No SVG icon available")
|
utils.RespondVioletError(rw, http.StatusTeapot, "No icon available")
|
||||||
return
|
|
||||||
}
|
|
||||||
rw.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = rw.Write(raw)
|
|
||||||
return
|
|
||||||
case "/favicon.png":
|
|
||||||
icons := fav.GetIcons(req.Host)
|
|
||||||
raw, err := icons.ProducePng()
|
|
||||||
if err != nil {
|
|
||||||
utils.RespondVioletError(rw, http.StatusTeapot, "No PNG icon available")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
rw.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = rw.Write(raw)
|
|
||||||
return
|
|
||||||
case "/favicon.ico":
|
|
||||||
icons := fav.GetIcons(req.Host)
|
|
||||||
raw, err := icons.ProduceIco()
|
|
||||||
if err != nil {
|
|
||||||
utils.RespondVioletError(rw, http.StatusTeapot, "No ICO icon available")
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
rw.Header().Set("Content-Type", contentType)
|
||||||
rw.WriteHeader(http.StatusOK)
|
rw.WriteHeader(http.StatusOK)
|
||||||
_, _ = rw.Write(raw)
|
_, _ = rw.Write(raw)
|
||||||
return
|
return
|
||||||
|
@ -16,7 +16,7 @@ func TestRespondHttpStatus(t *testing.T) {
|
|||||||
assert.Equal(t, "418 I'm a teapot", res.Status)
|
assert.Equal(t, "418 I'm a teapot", res.Status)
|
||||||
a, err := io.ReadAll(res.Body)
|
a, err := io.ReadAll(res.Body)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "418 I'm a teapot\n\n", string(a))
|
assert.Equal(t, "418 I'm a teapot\n", string(a))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestRespondVioletError(t *testing.T) {
|
func TestRespondVioletError(t *testing.T) {
|
||||||
@ -27,6 +27,6 @@ func TestRespondVioletError(t *testing.T) {
|
|||||||
assert.Equal(t, "418 I'm a teapot", res.Status)
|
assert.Equal(t, "418 I'm a teapot", res.Status)
|
||||||
a, err := io.ReadAll(res.Body)
|
a, err := io.ReadAll(res.Body)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, "418 I'm a teapot\n\n", string(a))
|
assert.Equal(t, "418 I'm a teapot\n", string(a))
|
||||||
assert.Equal(t, "Hidden Error Message", res.Header.Get("X-Violet-Error"))
|
assert.Equal(t, "Hidden Error Message", res.Header.Get("X-Violet-Error"))
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user