mirror of
https://github.com/AynaLivePlayer/AynaLivePlayer.git
synced 2026-03-19 02:19:45 +08:00
fix bug/web template init
This commit is contained in:
@@ -3,6 +3,7 @@ package gui
|
||||
import (
|
||||
"AynaLivePlayer/player"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fyne.io/fyne/v2"
|
||||
"fyne.io/fyne/v2/canvas"
|
||||
"fyne.io/fyne/v2/container"
|
||||
@@ -81,7 +82,12 @@ func newFixedSplitContainer(horizontal bool, leading, trailing fyne.CanvasObject
|
||||
|
||||
func newImageFromPlayerPicture(picture player.Picture) (*canvas.Image, error) {
|
||||
if picture.Data != nil {
|
||||
return canvas.NewImageFromReader(bytes.NewReader(picture.Data), "cover"), nil
|
||||
img := canvas.NewImageFromReader(bytes.NewReader(picture.Data), "cover")
|
||||
// return an error when img is nil
|
||||
if img == nil {
|
||||
return nil, errors.New("fail to read image")
|
||||
}
|
||||
return img, nil
|
||||
} else {
|
||||
uri, err := storage.ParseURI(picture.Url)
|
||||
if err != nil || uri == nil {
|
||||
|
||||
75
plugin/webinfo/template.go
Normal file
75
plugin/webinfo/template.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package webinfo
|
||||
|
||||
import (
|
||||
"AynaLivePlayer/util"
|
||||
"encoding/json"
|
||||
"io/ioutil"
|
||||
)
|
||||
|
||||
const WebTemplateStorePath = "./webtemplates.json"
|
||||
|
||||
type WebTemplate struct {
|
||||
Name string
|
||||
Template string
|
||||
}
|
||||
|
||||
type TemplateStore struct {
|
||||
Templates map[string]*WebTemplate
|
||||
}
|
||||
|
||||
func newTemplateStore(filename string) *TemplateStore {
|
||||
s := &TemplateStore{Templates: map[string]*WebTemplate{}}
|
||||
var templates []WebTemplate
|
||||
file, err := ioutil.ReadFile(filename)
|
||||
if err == nil {
|
||||
_ = json.Unmarshal(file, &templates)
|
||||
}
|
||||
for _, tmpl := range templates {
|
||||
s.Templates[tmpl.Name] = &tmpl
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *TemplateStore) Save(filename string) {
|
||||
templates := make([]WebTemplate, 0)
|
||||
for _, tmp := range s.Templates {
|
||||
templates = append(templates, *tmp)
|
||||
}
|
||||
unescape, err := util.MarshalIndentUnescape(templates, "", " ")
|
||||
if err != nil {
|
||||
lg.Warnf("save web templates to %s failed: %s", filename, err)
|
||||
return
|
||||
}
|
||||
if err := ioutil.WriteFile(filename, []byte(unescape), 0666); err != nil {
|
||||
lg.Warnf("save web templates to %s failed: %s", filename, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TemplateStore) Get(name string) *WebTemplate {
|
||||
if t, ok := s.Templates[name]; ok {
|
||||
return t
|
||||
}
|
||||
t := &WebTemplate{Name: name, Template: "<p>Empty</p>"}
|
||||
s.Templates[name] = t
|
||||
return t
|
||||
}
|
||||
|
||||
func (s *TemplateStore) Modify(name string, content string) {
|
||||
if _, ok := s.Templates[name]; ok {
|
||||
s.Templates[name].Template = content
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TemplateStore) List() []string {
|
||||
names := make([]string, 0)
|
||||
for name, _ := range s.Templates {
|
||||
names = append(names, name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
func (s *TemplateStore) Delete(name string) {
|
||||
delete(s.Templates, name)
|
||||
}
|
||||
21
plugin/webinfo/template_test.go
Normal file
21
plugin/webinfo/template_test.go
Normal file
@@ -0,0 +1,21 @@
|
||||
package webinfo
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTemplateStore_Create(t *testing.T) {
|
||||
s := newTemplateStore(WebTemplateStorePath)
|
||||
s.Get("A")
|
||||
s.Get("B")
|
||||
s.Modify("A", "123123")
|
||||
s.Save(WebTemplateStorePath)
|
||||
}
|
||||
|
||||
func TestTemplateStore_Load(t *testing.T) {
|
||||
s := newTemplateStore(WebTemplateStorePath)
|
||||
for name, tmpl := range s.Templates {
|
||||
fmt.Println(name, tmpl.Template)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user