refactor torrent

This commit is contained in:
royalcat 2024-05-20 00:24:09 +03:00
parent 99cdd5471e
commit 991c15fdef
33 changed files with 223 additions and 275 deletions

View file

@ -7,12 +7,12 @@ import (
"net/http"
"os"
"git.kmsign.ru/royalcat/tstor/src/host/service"
"git.kmsign.ru/royalcat/tstor/src/host/torrent"
"github.com/anacrolix/missinggo/v2/filecache"
"github.com/gin-gonic/gin"
)
var apiStatusHandler = func(fc *filecache.Cache, ss *service.Stats) gin.HandlerFunc {
var apiStatusHandler = func(fc *filecache.Cache, ss *torrent.Stats) gin.HandlerFunc {
return func(ctx *gin.Context) {
stat := gin.H{
"torrentStats": ss.GlobalStats(),

View file

@ -3,6 +3,7 @@ package model
import (
"context"
"git.kmsign.ru/royalcat/tstor/src/host/torrent"
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
)
@ -26,8 +27,8 @@ func FillFsEntry(ctx context.Context, e FsElem, fs vfs.Filesystem, path string)
Name: e.Name(),
FS: e,
}, nil
case *vfs.TorrentFS:
e := e.(*vfs.TorrentFS)
case *torrent.TorrentFS:
e := e.(*torrent.TorrentFS)
torrent, err := MapTorrent(ctx, e.Torrent)
if err != nil {
return nil, err

View file

@ -3,39 +3,39 @@ package model
import (
"context"
"git.kmsign.ru/royalcat/tstor/src/host/controller"
"github.com/anacrolix/torrent"
"git.kmsign.ru/royalcat/tstor/src/host/torrent"
atorrent "github.com/anacrolix/torrent"
)
func MapPeerSource(source torrent.PeerSource) string {
func MapPeerSource(source atorrent.PeerSource) string {
switch source {
case torrent.PeerSourceDirect:
case atorrent.PeerSourceDirect:
return "Direct"
case torrent.PeerSourceUtHolepunch:
case atorrent.PeerSourceUtHolepunch:
return "Ut Holepunch"
case torrent.PeerSourceDhtAnnouncePeer:
case atorrent.PeerSourceDhtAnnouncePeer:
return "DHT Announce"
case torrent.PeerSourceDhtGetPeers:
case atorrent.PeerSourceDhtGetPeers:
return "DHT"
case torrent.PeerSourceIncoming:
case atorrent.PeerSourceIncoming:
return "Incoming"
case torrent.PeerSourceTracker:
case atorrent.PeerSourceTracker:
return "Tracker"
case torrent.PeerSourcePex:
case atorrent.PeerSourcePex:
return "PEX"
default:
return "Unknown"
}
}
func MapTorrent(ctx context.Context, t *controller.Torrent) (*Torrent, error) {
func MapTorrent(ctx context.Context, t *torrent.Controller) (*Torrent, error) {
downloading := false
files, err := t.Files(ctx)
if err != nil {
return nil, err
}
for _, file := range files {
if file.Priority() > torrent.PiecePriorityNone && file.BytesCompleted() < file.Length() {
if file.Priority() > atorrent.PiecePriorityNone && file.BytesCompleted() < file.Length() {
downloading = true
break
}

View file

@ -5,9 +5,9 @@ package model
import (
"time"
"git.kmsign.ru/royalcat/tstor/src/host/controller"
"git.kmsign.ru/royalcat/tstor/src/host/torrent"
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
"github.com/anacrolix/torrent"
torrent1 "github.com/anacrolix/torrent"
)
type Dir interface {
@ -180,14 +180,14 @@ type Torrent struct {
ExcludedFiles []*TorrentFile `json:"excludedFiles"`
Peers []*TorrentPeer `json:"peers"`
Downloading bool `json:"downloading"`
T *controller.Torrent `json:"-"`
T *torrent.Controller `json:"-"`
}
type TorrentFs struct {
Name string `json:"name"`
Torrent *Torrent `json:"torrent"`
Entries []FsEntry `json:"entries"`
FS *vfs.TorrentFS `json:"-"`
Name string `json:"name"`
Torrent *Torrent `json:"torrent"`
Entries []FsEntry `json:"entries"`
FS *torrent.TorrentFS `json:"-"`
}
func (TorrentFs) IsDir() {}
@ -206,10 +206,10 @@ func (this TorrentFs) GetEntries() []FsEntry {
func (TorrentFs) IsFsEntry() {}
type TorrentFile struct {
Filename string `json:"filename"`
Size int64 `json:"size"`
BytesCompleted int64 `json:"bytesCompleted"`
F *torrent.File `json:"-"`
Filename string `json:"filename"`
Size int64 `json:"size"`
BytesCompleted int64 `json:"bytesCompleted"`
F *torrent1.File `json:"-"`
}
type TorrentFileEntry struct {
@ -230,12 +230,12 @@ type TorrentFilter struct {
}
type TorrentPeer struct {
IP string `json:"ip"`
DownloadRate float64 `json:"downloadRate"`
Discovery string `json:"discovery"`
Port int64 `json:"port"`
ClientName string `json:"clientName"`
F *torrent.PeerConn `json:"-"`
IP string `json:"ip"`
DownloadRate float64 `json:"downloadRate"`
Discovery string `json:"discovery"`
Port int64 `json:"port"`
ClientName string `json:"clientName"`
F *torrent1.PeerConn `json:"-"`
}
type TorrentProgress struct {

View file

@ -14,7 +14,7 @@ import (
"git.kmsign.ru/royalcat/tstor/pkg/uuid"
graph "git.kmsign.ru/royalcat/tstor/src/delivery/graphql"
"git.kmsign.ru/royalcat/tstor/src/delivery/graphql/model"
"git.kmsign.ru/royalcat/tstor/src/host/service"
"git.kmsign.ru/royalcat/tstor/src/host/torrent"
"github.com/99designs/gqlgen/graphql"
aih "github.com/anacrolix/torrent/types/infohash"
)
@ -79,7 +79,7 @@ func (r *mutationResolver) DownloadTorrent(ctx context.Context, infohash string,
f = *file
}
err := r.Service.Download(ctx, &service.TorrentDownloadTask{
err := r.Service.Download(ctx, &torrent.DownloadTask{
ID: uuid.New(),
InfoHash: aih.FromHexString(infohash),
File: f,

View file

@ -11,7 +11,7 @@ import (
graph "git.kmsign.ru/royalcat/tstor/src/delivery/graphql"
"git.kmsign.ru/royalcat/tstor/src/delivery/graphql/model"
"git.kmsign.ru/royalcat/tstor/src/host/controller"
"git.kmsign.ru/royalcat/tstor/src/host/torrent"
)
// Torrents is the resolver for the torrents field.
@ -80,7 +80,7 @@ func (r *queryResolver) Torrents(ctx context.Context, filter *model.TorrentsFilt
tr = append(tr, d)
}
slices.SortStableFunc(torrents, func(t1, t2 *controller.Torrent) int {
slices.SortStableFunc(torrents, func(t1, t2 *torrent.Controller) int {
return strings.Compare(t1.Name(), t2.Name())
})

View file

@ -1,7 +1,7 @@
package resolver
import (
"git.kmsign.ru/royalcat/tstor/src/host/service"
"git.kmsign.ru/royalcat/tstor/src/host/torrent"
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
"github.com/go-git/go-billy/v5"
)
@ -11,7 +11,7 @@ import (
// It serves as dependency injection for your app, add any dependencies you require here.
type Resolver struct {
Service *service.Service
Service *torrent.Service
VFS vfs.Filesystem
SourceFS billy.Filesystem
}

View file

@ -7,7 +7,7 @@ import (
"git.kmsign.ru/royalcat/tstor/pkg/rlog"
"git.kmsign.ru/royalcat/tstor/src/config"
"git.kmsign.ru/royalcat/tstor/src/host/service"
"git.kmsign.ru/royalcat/tstor/src/host/torrent"
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
"github.com/anacrolix/missinggo/v2/filecache"
echopprof "github.com/labstack/echo-contrib/pprof"
@ -15,7 +15,7 @@ import (
"github.com/labstack/echo/v4/middleware"
)
func New(fc *filecache.Cache, ss *service.Stats, s *service.Service, vfs vfs.Filesystem, logPath string, cfg *config.Settings) error {
func New(fc *filecache.Cache, s *torrent.Service, vfs vfs.Filesystem, logPath string, cfg *config.Settings) error {
log := slog.With()
r := echo.New()

View file

@ -8,7 +8,7 @@ import (
"git.kmsign.ru/royalcat/tstor/pkg/rlog"
graph "git.kmsign.ru/royalcat/tstor/src/delivery/graphql"
"git.kmsign.ru/royalcat/tstor/src/delivery/graphql/resolver"
"git.kmsign.ru/royalcat/tstor/src/host/service"
"git.kmsign.ru/royalcat/tstor/src/host/torrent"
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
@ -18,7 +18,7 @@ import (
"github.com/ravilushqa/otelgqlgen"
)
func GraphQLHandler(service *service.Service, vfs vfs.Filesystem) http.Handler {
func GraphQLHandler(service *torrent.Service, vfs vfs.Filesystem) http.Handler {
graphqlHandler := handler.NewDefaultServer(
graph.NewExecutableSchema(
graph.Config{