/
torrent.go
164 lines (146 loc) · 3.98 KB
/
torrent.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
158
159
160
161
162
163
164
package handlers
import (
"bytes"
"fmt"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/log"
"github.com/miru-project/bt-server/app"
"github.com/miru-project/bt-server/config"
models "github.com/miru-project/bt-server/model"
"github.com/miru-project/bt-server/util"
)
func TorrentStatus(c *fiber.Ctx) error {
return c.JSON(app.BTClient.Stats())
}
func ListTorrent(c *fiber.Ctx) error {
var result []models.TorrentResult
for hash, t := range app.Torrents {
result = append(result, models.TorrentResult{
InfoHash: hash,
Name: t.Name(),
})
}
return c.JSON(result)
}
func AddTorrent(c *fiber.Ctx) error {
body := c.Body()
mediaInfo, err := metainfo.Load(bytes.NewReader(body))
if err != nil {
return err
}
t, err := app.BTClient.AddTorrent(mediaInfo)
if err != nil {
return err
}
hex := t.InfoHash().HexString()
app.Torrents[hex] = t
files := []string{}
if len(t.Info().Files) == 0 {
files = append(files, t.Name())
} else {
for _, file := range t.Info().Files {
files = append(files, file.DisplayPath(t.Info()))
}
}
return c.JSON(models.TorrentDetailResult{
InfoHash: hex,
Detail: t.Info(),
Files: files,
})
}
func DeleteTorrent(c *fiber.Ctx) error {
infoHash := c.Params("infoHash")
t, ok := app.Torrents[infoHash]
if !ok {
return c.Status(http.StatusNotFound).SendString("torrent not found")
}
t.Drop()
if config.AUTO_DELETE_CACHE_FILE == "true" {
if err := os.RemoveAll(path.Join(config.DATA_DIR, t.Name())); err != nil {
return c.Status(http.StatusInternalServerError).SendString("Internal server error")
}
}
delete(app.Torrents, infoHash)
return c.SendStatus(http.StatusOK)
}
func GetTorrentData(c *fiber.Ctx) error {
params := c.AllParams()
infoHash := params["infoHash"]
filePath := params["*1"]
t, ok := app.Torrents[infoHash]
if !ok {
return c.Status(http.StatusNotFound).SendString("torrent not found")
}
if filePath == "" {
files := []string{}
if len(t.Info().Files) == 0 {
files = append(files, t.Name())
} else {
for _, file := range t.Info().Files {
files = append(files, file.DisplayPath(t.Info()))
}
}
return c.JSON(models.TorrentDetailResult{
InfoHash: infoHash,
Detail: t.Info(),
Files: files,
})
}
files := t.Files()
unescape, err := url.PathUnescape(filePath)
log.Info(unescape)
if err != nil {
log.Error(err.Error())
}
// 获取文件后缀
fileExtension := path.Ext(unescape)
if len(files) == 0 && unescape == t.Name() {
return serverTorrentData(c, fileExtension, t.NewReader(), t.Length())
}
for _, file := range files {
if file.DisplayPath() == unescape {
return serverTorrentData(c, fileExtension, file.NewReader(), file.Length())
}
}
return c.Status(http.StatusNotFound).SendString("file not found")
}
func serverTorrentData(c *fiber.Ctx, fileExtension string, reader torrent.Reader, fileSize int64) error {
// 获取文件后缀
mime, ok := util.IsMedia(fileExtension)
log.Info(mime, fileExtension)
if !ok {
c.Set("Content-Type", "application/octet-stream")
return c.SendStream(reader)
}
c.Set("Content-Type", mime)
reader.SetResponsive()
rangeHeader := c.Get("Range")
if rangeHeader != "" {
ranges := strings.Split(rangeHeader, "=")[1]
rangeParts := strings.Split(ranges, "-")
start, _ := strconv.ParseInt(rangeParts[0], 10, 64)
end := fileSize - 1
if rangeParts[1] != "" {
end, _ = strconv.ParseInt(rangeParts[1], 10, 64)
}
c.Status(http.StatusPartialContent)
c.Set("Content-Range", fmt.Sprintf("bytes %d-%d/%d", start, end, fileSize))
c.Set("Accept-Ranges", "bytes")
c.Set("Content-Length", strconv.FormatInt(end-start+1, 10))
log.Info(fmt.Sprintf("bytes %d-%d/%d", start, end, fileSize))
_, err := reader.Seek(start, 0)
if err != nil {
return c.Status(http.StatusInternalServerError).SendString("Internal server error")
}
return c.SendStream(reader)
}
return c.SendStream(reader)
}