add data to torrent

This commit is contained in:
YouROK
2021-01-19 16:12:39 +03:00
parent 8644e040a3
commit d15e0fd375
7 changed files with 20 additions and 4 deletions

View File

@@ -15,6 +15,7 @@ type TorrentDB struct {
Name string `json:"name"`
Title string `json:"title,omitempty"`
Poster string `json:"poster,omitempty"`
Data string `json:"data,omitempty"`
Timestamp int64 `json:"timestamp,omitempty"`
Size int64 `json:"size,omitempty"`

View File

@@ -20,7 +20,7 @@ func InitApiHelper(bt *BTServer) {
bts = bt
}
func AddTorrent(spec *torrent.TorrentSpec, title, poster string) (*Torrent, error) {
func AddTorrent(spec *torrent.TorrentSpec, title, poster string, data string) (*Torrent, error) {
torr, err := NewTorrent(spec, bts)
if err != nil {
log.TLogln("error add torrent:", err)
@@ -41,6 +41,12 @@ func AddTorrent(spec *torrent.TorrentSpec, title, poster string) (*Torrent, erro
torr.Poster = torDB.Poster
}
}
if torr.Data == "" {
torr.Data = data
if torr.Data == "" && torDB != nil {
torr.Data = torDB.Data
}
}
return torr, nil
}

View File

@@ -14,6 +14,7 @@ func AddTorrentDB(torr *Torrent) {
t.TorrentSpec = torr.TorrentSpec
t.Name = torr.Name()
t.Title = torr.Title
t.Data = torr.Data
if t.Title == "" {
t.Title = t.Name
}
@@ -36,6 +37,7 @@ func GetTorrentDB(hash metainfo.Hash) *Torrent {
torr.Poster = db.Poster
torr.Timestamp = db.Timestamp
torr.Size = db.Size
torr.Data = db.Data
torr.Stat = state.TorrentInDB
return torr
}
@@ -57,6 +59,7 @@ func ListTorrentsDB() map[metainfo.Hash]*Torrent {
torr.Poster = db.Poster
torr.Timestamp = db.Timestamp
torr.Size = db.Size
torr.Data = db.Data
torr.Stat = state.TorrentInDB
ret[torr.TorrentSpec.InfoHash] = torr
}

View File

@@ -24,6 +24,7 @@ import (
type Torrent struct {
Title string
Poster string
Data string
*torrent.TorrentSpec
Stat state.TorrentStat

View File

@@ -59,7 +59,7 @@ func stream(c *gin.Context) {
return
}
tor, err := torr.AddTorrent(spec, title, poster)
tor, err := torr.AddTorrent(spec, title, poster, "")
if err != nil {
c.AbortWithError(http.StatusInternalServerError, err)
return

View File

@@ -19,6 +19,7 @@ type torrReqJS struct {
Hash string `json:"hash,omitempty"`
Title string `json:"title,omitempty"`
Poster string `json:"poster,omitempty"`
Data string `json:"data,omitempty"`
SaveToDB bool `json:"save_to_db,omitempty"`
}
@@ -68,7 +69,7 @@ func addTorrent(req torrReqJS, c *gin.Context) {
return
}
tor, err := torr.AddTorrent(torrSpec, req.Title, req.Poster)
tor, err := torr.AddTorrent(torrSpec, req.Title, req.Poster, req.Data)
if err != nil {
log.TLogln("error add torrent:", err)
c.AbortWithError(http.StatusInternalServerError, err)

View File

@@ -28,6 +28,10 @@ func torrentUpload(c *gin.Context) {
if len(form.Value["poster"]) > 0 {
poster = form.Value["poster"][0]
}
data := ""
if len(form.Value["data"]) > 0 {
data = form.Value["data"][0]
}
for name, file := range form.File {
log.TLogln("add torrent file", name)
@@ -45,7 +49,7 @@ func torrentUpload(c *gin.Context) {
continue
}
tor, err := torr.AddTorrent(spec, title, poster)
tor, err := torr.AddTorrent(spec, title, poster, data)
if err != nil {
log.TLogln("error upload torrent:", err)
continue