-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_update_playlist.go
82 lines (65 loc) 路 1.78 KB
/
get_update_playlist.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package rest
import (
"fmt"
"github.com/boggydigital/kvas"
"github.com/boggydigital/pathways"
"github.com/boggydigital/yet/data"
"github.com/boggydigital/yet/paths"
"net/http"
"net/url"
)
func GetUpdatePlaylist(w http.ResponseWriter, r *http.Request) {
// GET /update_playlist?list
playlistId := r.URL.Query().Get("list")
if playlistId == "" {
http.Redirect(w, r, "/list", http.StatusPermanentRedirect)
return
}
metadataDir, err := pathways.GetAbsDir(paths.Metadata)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
properties := []string{
data.PlaylistWatchlistProperty,
data.PlaylistDownloadQueueProperty,
data.PlaylistSingleFormatDownloadProperty,
}
plRdx, err := kvas.NewReduxWriter(metadataDir, properties...)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
for _, p := range properties {
if err := updatePlaylistProperty(playlistId, p, r.URL, plRdx); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
}
http.Redirect(w, r, "/playlist?list="+playlistId, http.StatusTemporaryRedirect)
}
func updatePlaylistProperty(playlistId string, property string, u *url.URL, rdx kvas.WriteableRedux) error {
flagStr := ""
switch property {
case data.PlaylistWatchlistProperty:
flagStr = "refresh"
case data.PlaylistDownloadQueueProperty:
flagStr = "download"
case data.PlaylistSingleFormatDownloadProperty:
flagStr = "single-format"
default:
return fmt.Errorf("unsupported property %s", property)
}
flag := u.Query().Has(flagStr)
var err error
if flag {
if !rdx.HasKey(property, playlistId) {
err = rdx.AddValues(property, playlistId, data.TrueValue)
}
} else {
if rdx.HasKey(property, playlistId) {
err = rdx.CutKeys(property, playlistId)
}
}
return err
}