This commit is contained in:
Yami Odymel 2024-02-02 16:45:12 +08:00
parent 4350359f90
commit 12a27d11d5
No known key found for this signature in database
GPG Key ID: 68E469836934DB36
4 changed files with 55 additions and 0 deletions

View File

@ -1,7 +1,9 @@
package chaturbate
import (
"encoding/json"
"errors"
"os"
"github.com/google/uuid"
"github.com/urfave/cli/v2"
@ -161,3 +163,45 @@ func (m *Manager) StopListenUpdate(id string) error {
close(v)
return nil
}
// SaveChannels
func (m *Manager) SaveChannels() error {
configs := make([]*Config, 0)
for _, v := range m.Channels {
configs = append(configs, &Config{
Username: v.Username,
Framerate: v.Framerate,
Resolution: v.Resolution,
ResolutionFallback: v.ResolutionFallback,
FilenamePattern: v.filenamePattern,
SplitDuration: v.SplitDuration,
SplitFilesize: v.SplitFilesize,
})
}
b, err := json.MarshalIndent(configs, "", " ")
if err != nil {
return err
}
return os.WriteFile("chaturbate_channels.json", b, 0777)
}
// LoadChannels
func (m *Manager) LoadChannels() error {
b, err := os.ReadFile("chaturbate_channels.json")
if err != nil {
if os.IsNotExist(err) {
return nil
}
return err
}
var configs []*Config
if err := json.Unmarshal(b, &configs); err != nil {
return err
}
for _, v := range configs {
if err := m.CreateChannel(v); err != nil {
return err
}
}
return nil
}

View File

@ -60,5 +60,9 @@ func (h *CreateChannelHandler) Handle(c *gin.Context) {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if err := h.chaturbate.SaveChannels(); err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, &CreateChannelResponse{})
}

View File

@ -46,5 +46,9 @@ func (h *DeleteChannelHandler) Handle(c *gin.Context) {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
if err := h.chaturbate.SaveChannels(); err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return
}
c.JSON(http.StatusOK, &DeleteChannelResponse{})
}

View File

@ -132,6 +132,9 @@ func startWeb(c *cli.Context) error {
//r.Use(cors.Default())
m := chaturbate.NewManager(c)
if err := m.LoadChannels(); err != nil {
return err
}
fe, err := fs.Sub(FS, "handler/view")
if err != nil {