-
Notifications
You must be signed in to change notification settings - Fork 11
/
methods_view.go
96 lines (79 loc) · 1.89 KB
/
methods_view.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
package storm
import (
"crypto/sha1"
"encoding/hex"
"encoding/json"
deluge "github.com/gdm85/go-libdeluge"
"net/http"
"sort"
)
type ViewTorrent struct {
Hash string
Label string
*deluge.TorrentStatus
}
type ViewUpdate struct {
Torrents []*ViewTorrent
Session *deluge.SessionStatus
DiskFree int64
}
type ViewUpdateResponse struct {
ViewUpdate
ETag string
}
func httpViewUpdate(conn deluge.DelugeClient, r *http.Request) (interface{}, error) {
var (
q = r.URL.Query()
ids = q["id"]
state = deluge.TorrentState(q.Get("state"))
)
torrents, err := conn.TorrentsStatus(state, ids)
if err != nil {
return nil, err
}
var torrentHashes = make([]string, 0, len(torrents))
for k := range torrents {
torrentHashes = append(torrentHashes, k)
}
sort.Strings(torrentHashes)
var torrentLabels = make(map[string]string)
plugin, err := labelPluginClient(conn)
if err == nil {
labels, err := plugin.GetTorrentsLabels(state, ids)
if err == nil {
torrentLabels = labels
}
}
var responseTorrents = make([]*ViewTorrent, 0, len(torrents))
for _, k := range torrentHashes {
responseTorrents = append(responseTorrents, &ViewTorrent{
Hash: k,
Label: torrentLabels[k],
TorrentStatus: torrents[k],
})
}
session, err := conn.GetSessionStatus()
if err != nil {
return nil, err
}
diskFree, err := conn.GetFreeSpace(q.Get("path"))
update := ViewUpdate{
Torrents: responseTorrents,
Session: session,
DiskFree: diskFree,
}
// ETag calculation
var h = sha1.New()
_ = json.NewEncoder(h).Encode(&update)
responseETag := hex.EncodeToString(h.Sum(nil))
if requestETag := r.Header.Get("ETag"); requestETag != "" && requestETag == responseETag {
return nil, &Error{
Code: http.StatusNotModified,
Message: "View not modified since last request",
}
}
return &ViewUpdateResponse{
ViewUpdate: update,
ETag: responseETag,
}, nil
}