-
Notifications
You must be signed in to change notification settings - Fork 148
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] On-demand filelist fetching #1719
base: dev
Are you sure you want to change the base?
Changes from 51 commits
22e1c7e
f226f1a
89fd690
58c6d09
a740a23
68da680
1b1fd28
8e3d53c
c3cb627
557047c
11567bf
ef30870
1219e69
2087c27
ceb02d1
ed8cf8c
b6b1414
1d8a324
d3480ad
479eb02
49ead03
77e602b
8f28efc
c1658bc
e703dea
ef49289
18000fb
bd8a8b7
9f7ee65
ba503c8
1d90dcf
9ffb5f1
cb8604d
08b6984
c943839
1c82814
95daf79
4bd364e
95cc7fd
43b16ed
88a4966
52050b2
0cf75e3
01865e8
239b953
c7ae1cf
b05f3b8
eb29745
81ef4b9
ec3858e
1f2e85c
050d00d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package torrentController | ||
|
||
import ( | ||
"html/template" | ||
"encoding/hex" | ||
"net/http" | ||
"strings" | ||
"strconv" | ||
|
||
"github.com/NyaaPantsu/nyaa/models/torrents" | ||
"github.com/NyaaPantsu/nyaa/models" | ||
"github.com/NyaaPantsu/nyaa/templates" | ||
"github.com/NyaaPantsu/nyaa/utils/format" | ||
"github.com/NyaaPantsu/nyaa/utils/filelist" | ||
"github.com/Stephen304/goscrape" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
func GetFilesHandler(c *gin.Context) { | ||
id, _ := strconv.ParseInt(c.Param("id"), 10, 32) | ||
torrent, err := torrents.FindByID(uint(id)) | ||
|
||
if err != nil { | ||
c.Status(http.StatusNotFound) | ||
return | ||
} | ||
|
||
|
||
if len(torrent.FileList) == 0 { | ||
var blankScrape models.Scrape | ||
ScrapeFiles(format.InfoHashToMagnet(strings.TrimSpace(torrent.Hash), torrent.Name, GetTorrentTrackers(torrent)...), torrent, blankScrape, true) | ||
} | ||
|
||
folder := filelist.FileListToFolder(torrent.FileList, "root") | ||
templates.TorrentFileList(c, torrent.ToJSON(), folder) | ||
} | ||
|
||
// ScrapeFiles : Scrape torrent files | ||
func ScrapeFiles(magnet string, torrent *models.Torrent, currentStats models.Scrape, statsExists bool) (error, []FileJSON) { | ||
if client == nil { | ||
err := initClient() | ||
if err != nil { | ||
return err, []FileJSON{} | ||
} | ||
} | ||
|
||
t, _ := client.AddMagnet(magnet) | ||
<-t.GotInfo() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might also be better to make it in a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That itself is not a problem, since the torrent page load is not affected by this. The filelist fetching is generally done with JS, if the user doesn't have JS then a /files/:torrentid page is loaded but the user will known full well that if that page takes time to load, it's because it's loading the filelist |
||
|
||
infoHash := t.InfoHash() | ||
dst := make([]byte, hex.EncodedLen(len(t.InfoHash()))) | ||
hex.Encode(dst, infoHash[:]) | ||
|
||
var UDP []string | ||
|
||
for _, tracker := range t.Metainfo().AnnounceList[0] { | ||
if strings.HasPrefix(tracker, "udp") { | ||
UDP = append(UDP, tracker) | ||
} | ||
} | ||
var results goscrape.Result | ||
if len(UDP) != 0 { | ||
udpscrape := goscrape.NewBulk(UDP) | ||
results = udpscrape.ScrapeBulk([]string{torrent.Hash})[0] | ||
} | ||
t.Drop() | ||
return nil, UpdateTorrentStats(torrent, results, currentStats, t.Files(), statsExists) | ||
} | ||
|
||
// FileJSON for file model in json, | ||
type FileJSON struct { | ||
Path string `json:"path"` | ||
Filesize template.HTML `json:"filesize"` | ||
} | ||
|
||
func fileSize(filesize int64) template.HTML { | ||
return template.HTML(format.FileSize(filesize)) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's only for the filelist, you should detect before if you have the torrent file and then if you have it already, load the files from it (since you won't have to wait to connect to peers to have them). You can check how I access to a torrent file information in the upload process here with
tfile
the content of the torrent filenyaa/utils/validator/torrent/functions.go
Line 195 in 860f9c8
In the opposite, if you don't have it, you should save it after the scrapping so you save ressource power