From 55d7ba2707c94bf45b6516fc3be2387e7fc52a36 Mon Sep 17 00:00:00 2001 From: yourok <8yourok8@mail.ru> Date: Mon, 13 Nov 2023 18:58:26 +0300 Subject: [PATCH] fix add torr file from dir --- server/cmd/main.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/server/cmd/main.go b/server/cmd/main.go index 63260c9..01da235 100644 --- a/server/cmd/main.go +++ b/server/cmd/main.go @@ -3,6 +3,8 @@ package main import ( "context" "fmt" + "github.com/anacrolix/torrent" + "github.com/anacrolix/torrent/metainfo" "net" "os" "path/filepath" @@ -18,7 +20,6 @@ import ( "server/settings" "server/torr" "server/version" - "server/web/api/utils" ) type args struct { @@ -131,7 +132,7 @@ func watchTDir(dir string) { for _, file := range files { filename := filepath.Join(path, file.Name()) if strings.ToLower(filepath.Ext(file.Name())) == ".torrent" { - sp, err := utils.ParseLink("file://" + filename) + sp, err := openFile(filename) if err == nil { tor, err := torr.AddTorrent(sp, "", "", "") if err == nil { @@ -160,3 +161,23 @@ func watchTDir(dir string) { 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 +}