-
Notifications
You must be signed in to change notification settings - Fork 11
/
methods_labels.go
128 lines (103 loc) · 2.75 KB
/
methods_labels.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package storm
import (
"fmt"
deluge "github.com/gdm85/go-libdeluge"
"github.com/gorilla/mux"
"net/http"
)
// getClientV1 gets the underlying version 1 client from a DelugeClient interface.
func getClientV1(conn deluge.DelugeClient) (*deluge.Client, error) {
switch client := conn.(type) {
case *deluge.Client:
return client, nil
case *deluge.ClientV2:
return &client.Client, nil
default:
return nil, fmt.Errorf("failed to obtain version 1 Deluge client")
}
}
// labelPluginClient constructs an instance of a deluge.LabelPlugin client from the input DelugeClient connection.
func labelPluginClient(conn deluge.DelugeClient) (*deluge.LabelPlugin, error) {
client, err := getClientV1(conn)
if err != nil {
return nil, err
}
return &deluge.LabelPlugin{
Client: client,
}, nil
}
// httpLabels gets the current labels
func httpLabels(conn deluge.DelugeClient, r *http.Request) (interface{}, error) {
plugin, err := labelPluginClient(conn)
if err != nil {
return nil, err
}
return plugin.GetLabels()
}
// httpCreateLabel creates a new label
func httpCreateLabel(conn deluge.DelugeClient, r *http.Request) (interface{}, error) {
vars := mux.Vars(r)
plugin, err := labelPluginClient(conn)
if err != nil {
return nil, err
}
err = plugin.AddLabel(vars["id"])
if err != nil {
return nil, err
}
return nil, nil
}
// httpCreateLabel deletes an existing label
func httpDeleteLabel(conn deluge.DelugeClient, r *http.Request) (interface{}, error) {
vars := mux.Vars(r)
plugin, err := labelPluginClient(conn)
if err != nil {
return nil, err
}
err = plugin.RemoveLabel(vars["id"])
if err != nil {
return nil, err
}
return nil, nil
}
// httpTorrentsLabels gets labels associated with all torrents matching the filter.
// ?id[] One or more torrent IDs
// ?state Torrents of this state
//
// Returns a mapping of torrent hash to torrent labels
func httpTorrentsLabels(conn deluge.DelugeClient, r *http.Request) (interface{}, error) {
ids, err := torrentIDs(r.URL.Query(), 0)
if err != nil {
return nil, err
}
state := (deluge.TorrentState)(r.URL.Query().Get("state"))
plugin, err := labelPluginClient(conn)
if err != nil {
return nil, err
}
labels, err := plugin.GetTorrentsLabels(state, ids)
if err != nil {
return nil, err
}
return labels, nil
}
type SetTorrentLabelRequest struct {
Label string
}
// httpSetTorrentLabel sets the label for a given torrent hash
func httpSetTorrentLabel(id string, conn deluge.DelugeClient, r *http.Request) (interface{}, error) {
var req SetTorrentLabelRequest
err := Read(r, &req)
if err != nil {
return nil, err
}
plugin, err := labelPluginClient(conn)
if err != nil {
return nil, err
}
err = plugin.SetTorrentLabel(id, req.Label)
if err != nil {
return nil, err
}
return nil, nil
}