-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.go
157 lines (139 loc) · 4.02 KB
/
functions.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/json"
"net/http"
"net/url"
"strings"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
)
func safenDisplayPath(displayPath string) string {
fileNameArray := strings.Split(displayPath, "/")
return strings.Join(fileNameArray, " ")
}
func appendFilePlaylist(scheme string, host string, infohash string, name string) string {
playList := "#EXTINF:-1," + safenDisplayPath(name) + "\n"
playList += scheme + "://" + host + "/api/stream?infohash=" + infohash + "&file=" + url.QueryEscape(name) + "\n"
return playList
}
func nameCheck(str string, substr string) bool {
splittedSubStr := strings.Split(substr, " ")
for _, curWord := range splittedSubStr {
if !strings.Contains(str, curWord) {
return false
}
}
return true
}
func getTorrentFile(files []*torrent.File, filename string, exactName bool) *torrent.File {
var tFile *torrent.File = nil
for _, file := range files {
if exactName && file.DisplayPath() == filename {
tFile = file
}
if !exactName && filename != "" && nameCheck(strings.ToLower(file.DisplayPath()), strings.ToLower(filename)) {
tFile = file
}
if tFile != nil {
break
}
}
return tFile
}
func makePlayStreamURL(infohash string, filename string, isStream bool) string {
endPoint := "play"
if isStream {
endPoint = "stream"
}
URL := "/api/" + endPoint + "?infohash=" + infohash
if filename != "" {
URL += "&file=" + url.QueryEscape(filename)
}
return URL
}
func httpJSONError(w http.ResponseWriter, error string, code int) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(code)
if json.NewEncoder(w).Encode(errorRes{
Error: error,
}) != nil {
http.Error(w, error, code)
}
}
func parseRequestBody(w http.ResponseWriter, r *http.Request, v any) error {
err := json.NewDecoder(r.Body).Decode(v)
if err != nil {
httpJSONError(w, "Request JSON body decode error", http.StatusInternalServerError)
}
return err
}
func makeJSONResponse(w http.ResponseWriter, v any) {
w.Header().Set("Content-Type", "application/json")
err := json.NewEncoder(w).Encode(v)
if err != nil {
httpJSONError(w, "Response JSON body encode error", http.StatusInternalServerError)
}
}
func getInfo(t *torrent.Torrent) {
if t != nil {
<-t.GotInfo()
}
}
func initMagnet(w http.ResponseWriter, magnet string, alldn []string, alltr []string) *torrent.Torrent {
var t *torrent.Torrent = nil
var err error
magnetString := magnet
for _, dn := range alldn {
magnetString += "&dn=" + url.QueryEscape(dn)
}
for _, tr := range alltr {
magnetString += "&tr=" + url.QueryEscape(tr)
}
t, err = torrentCli.AddMagnet(magnetString)
if err != nil {
httpJSONError(w, "Torrent add error", http.StatusInternalServerError)
}
getInfo(t)
return t
}
func getTorrent(w http.ResponseWriter, infoHash string) *torrent.Torrent {
var t *torrent.Torrent = nil
var tOk bool
if len(infoHash) != 40 {
httpJSONError(w, "InfoHash not valid", http.StatusInternalServerError)
return t
}
t, tOk = torrentCli.Torrent(metainfo.NewHashFromHex(infoHash))
if !tOk {
httpJSONError(w, "Torrent not found", http.StatusNotFound)
}
getInfo(t)
return t
}
func parseTorrentStats(t *torrent.Torrent) torrentStatsRes {
var tsRes torrentStatsRes
tsRes.InfoHash = t.InfoHash().String()
tsRes.Name = t.Name()
tsRes.TotalPeers = t.Stats().TotalPeers
tsRes.ActivePeers = t.Stats().ActivePeers
tsRes.HalfOpenPeers = t.Stats().HalfOpenPeers
tsRes.PendingPeers = t.Stats().PendingPeers
if t.Info() == nil {
return tsRes
}
for _, tFile := range t.Files() {
tsRes.Files.OnTorrent = append(tsRes.Files.OnTorrent, torrentStatsFilesOnTorrent{
FileName: tFile.DisplayPath(),
FileSizeBytes: int(tFile.Length()),
})
if tFile.BytesCompleted() != 0 {
tsRes.Files.OnDisk = append(tsRes.Files.OnDisk, torrentStatsFilesOnDisk{
FileName: tFile.DisplayPath(),
StreamURL: makePlayStreamURL(t.InfoHash().String(), tFile.DisplayPath(), true),
BytesDownloaded: int(tFile.BytesCompleted()),
FileSizeBytes: int(tFile.Length()),
})
}
}
return tsRes
}