fix add torr file from dir

This commit is contained in:
yourok
2023-11-13 18:58:26 +03:00
parent 90c5679cb8
commit 55d7ba2707

View File

@@ -3,6 +3,8 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"github.com/anacrolix/torrent"
"github.com/anacrolix/torrent/metainfo"
"net" "net"
"os" "os"
"path/filepath" "path/filepath"
@@ -18,7 +20,6 @@ import (
"server/settings" "server/settings"
"server/torr" "server/torr"
"server/version" "server/version"
"server/web/api/utils"
) )
type args struct { type args struct {
@@ -131,7 +132,7 @@ func watchTDir(dir string) {
for _, file := range files { for _, file := range files {
filename := filepath.Join(path, file.Name()) filename := filepath.Join(path, file.Name())
if strings.ToLower(filepath.Ext(file.Name())) == ".torrent" { if strings.ToLower(filepath.Ext(file.Name())) == ".torrent" {
sp, err := utils.ParseLink("file://" + filename) sp, err := openFile(filename)
if err == nil { if err == nil {
tor, err := torr.AddTorrent(sp, "", "", "") tor, err := torr.AddTorrent(sp, "", "", "")
if err == nil { if err == nil {
@@ -160,3 +161,23 @@ func watchTDir(dir string) {
time.Sleep(time.Second * 5) time.Sleep(time.Second * 5)
} }
} }
func openFile(path string) (*torrent.TorrentSpec, error) {
minfo, err := metainfo.LoadFromFile(path)
if err != nil {
return nil, err
}
info, err := minfo.UnmarshalInfo()
if err != nil {
return nil, err
}
mag := minfo.Magnet(info.Name, minfo.HashInfoBytes())
// mag := minfo.Magnet(nil, &info)
return &torrent.TorrentSpec{
InfoBytes: minfo.InfoBytes,
Trackers: [][]string{mag.Trackers},
DisplayName: info.Name,
InfoHash: minfo.HashInfoBytes(),
}, nil
}