mirror of
https://git.maid.zone/stuff/soundcloak.git
synced 2026-03-22 00:28:12 +05:00
export/import/reset prefs
This commit is contained in:
@@ -63,6 +63,10 @@ type PrefsForm struct {
|
||||
DefaultAutoplayMode string
|
||||
}
|
||||
|
||||
type Export struct {
|
||||
Preferences *cfg.Preferences `json:",omitempty"`
|
||||
}
|
||||
|
||||
func Load(r fiber.Router) {
|
||||
r.Get("/_/preferences", func(c *fiber.Ctx) error {
|
||||
p, err := Get(c)
|
||||
@@ -86,6 +90,10 @@ func Load(r fiber.Router) {
|
||||
return err
|
||||
}
|
||||
|
||||
if *old.AutoplayNextTrack {
|
||||
old.DefaultAutoplayMode = &p.DefaultAutoplayMode
|
||||
}
|
||||
|
||||
if *old.Player == "hls" {
|
||||
if cfg.ProxyStreams {
|
||||
if p.ProxyStreams == "on" {
|
||||
@@ -122,9 +130,6 @@ func Load(r fiber.Router) {
|
||||
old.ParseDescriptions = &cfg.False
|
||||
}
|
||||
|
||||
if *old.AutoplayNextTrack {
|
||||
old.DefaultAutoplayMode = &p.DefaultAutoplayMode
|
||||
}
|
||||
old.Player = &p.Player
|
||||
|
||||
data, err := json.Marshal(old)
|
||||
@@ -142,4 +147,68 @@ func Load(r fiber.Router) {
|
||||
|
||||
return c.Redirect("/_/preferences")
|
||||
})
|
||||
|
||||
r.Get("/_/preferences/reset", func(c *fiber.Ctx) error {
|
||||
// c.ClearCookie("prefs")
|
||||
c.Cookie(&fiber.Cookie{ // I've had some issues with c.ClearCookie() method, so using this workaround for now
|
||||
Name: "prefs",
|
||||
Value: "{}",
|
||||
Expires: time.Now().Add(400 * 24 * time.Hour),
|
||||
HTTPOnly: true,
|
||||
SameSite: "strict",
|
||||
})
|
||||
|
||||
return c.Redirect("/_/preferences")
|
||||
})
|
||||
|
||||
r.Get("/_/preferences/export", func(c *fiber.Ctx) error {
|
||||
p, err := Get(c)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.JSON(Export{Preferences: &p})
|
||||
})
|
||||
|
||||
r.Post("/_/preferences/import", func(c *fiber.Ctx) error {
|
||||
f, err := c.FormFile("prefs")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fd, err := f.Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer fd.Close()
|
||||
|
||||
dec := json.NewDecoder(fd)
|
||||
|
||||
var p Export
|
||||
err = dec.Decode(&p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if p.Preferences == nil {
|
||||
p.Preferences = &cfg.Preferences{}
|
||||
}
|
||||
|
||||
Defaults(p.Preferences)
|
||||
|
||||
data, err := json.Marshal(p.Preferences)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
c.Cookie(&fiber.Cookie{
|
||||
Name: "prefs",
|
||||
Value: string(data),
|
||||
Expires: time.Now().Add(400 * 24 * time.Hour),
|
||||
HTTPOnly: true,
|
||||
SameSite: "strict",
|
||||
})
|
||||
|
||||
return c.Redirect("/_/preferences")
|
||||
})
|
||||
}
|
||||
|
||||
@@ -32,7 +32,7 @@ templ sel(name string, options []option, selected string) {
|
||||
|
||||
templ Preferences(prefs cfg.Preferences) {
|
||||
<h1>Preferences</h1>
|
||||
<form method="post">
|
||||
<form method="post" autocomplete="off">
|
||||
<label>
|
||||
Parse descriptions:
|
||||
@checkbox("ParseDescriptions", *prefs.ParseDescriptions)
|
||||
@@ -89,4 +89,16 @@ templ Preferences(prefs cfg.Preferences) {
|
||||
<br/>
|
||||
<p>These preferences get saved in a cookie.</p>
|
||||
</form>
|
||||
|
||||
<h1>Management</h1>
|
||||
<h2>Preferences</h2>
|
||||
<div style="display: flex; gap: 1rem;">
|
||||
<a class="btn" href="/_/preferences/export" download="soundcloak_preferences.json">Export</a>
|
||||
<a class="btn" href="/_/preferences/reset">Reset</a>
|
||||
</div>
|
||||
<br>
|
||||
<form method="post" action="/_/preferences/import" autocomplete="off" style="display: grid; gap: 1rem;" enctype="multipart/form-data">
|
||||
<input class="btn" type="file" autocomplete="off" name="prefs" />
|
||||
<input type="submit" value="Import" class="btn"/>
|
||||
</form>
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user