add watch torrent dir

This commit is contained in:
YouROK
2021-04-27 11:19:14 +03:00
parent bd660fc39a
commit bda55fe162

View File

@@ -3,8 +3,11 @@ package main
import ( import (
"context" "context"
"fmt" "fmt"
"io/ioutil"
"net" "net"
"os" "os"
"path/filepath"
"strings"
"time" "time"
"github.com/alexflint/go-arg" "github.com/alexflint/go-arg"
@@ -13,7 +16,9 @@ import (
"server" "server"
"server/log" "server/log"
"server/settings" "server/settings"
"server/torr"
"server/version" "server/version"
"server/web/api/utils"
) )
type args struct { type args struct {
@@ -25,6 +30,7 @@ type args struct {
HttpAuth bool `arg:"-a" help:"http auth on all requests"` HttpAuth bool `arg:"-a" help:"http auth on all requests"`
DontKill bool `arg:"-k" help:"dont kill server on signal"` DontKill bool `arg:"-k" help:"dont kill server on signal"`
UI bool `arg:"-u" help:"run page torrserver in browser"` UI bool `arg:"-u" help:"run page torrserver in browser"`
TorrentsDir string `arg:"-t" help:"autoload torrent from dir"`
} }
func (args) Version() string { func (args) Version() string {
@@ -58,6 +64,10 @@ func main() {
}() }()
} }
if params.TorrentsDir != "" {
go watchTDir(params.TorrentsDir)
}
server.Start(params.Port, params.RDB) server.Start(params.Port, params.RDB)
log.TLogln(server.WaitServer()) log.TLogln(server.WaitServer())
log.Close() log.Close()
@@ -83,3 +93,34 @@ func dnsResolve() {
fmt.Println("Check new dns", addrs, err) fmt.Println("Check new dns", addrs, err)
} }
} }
func watchTDir(dir string) {
time.Sleep(5 * time.Second)
path, err := filepath.Abs(dir)
if err != nil {
path = dir
}
for {
files, err := ioutil.ReadDir(path)
if err == nil {
for _, file := range files {
filename := filepath.Join(path, file.Name())
if strings.ToLower(filepath.Ext(file.Name())) == ".torrent" {
sp, err := utils.ParseLink("file://" + filename)
if err == nil {
tor, err := torr.AddTorrent(sp, "", "", "")
if err == nil {
if tor.GotInfo() {
torr.SaveTorrentToDB(tor)
tor.Drop()
os.Remove(filename)
time.Sleep(time.Second)
}
}
}
}
}
}
time.Sleep(time.Second)
}
}