torrent list

This commit is contained in:
royalcat 2024-04-27 14:00:34 +03:00
parent d8ee8a3a24
commit 0d7aac068c
23 changed files with 1285 additions and 698 deletions

View file

@ -80,7 +80,7 @@ type ComplexityRoot struct {
Query struct {
FsEntry func(childComplexity int, path string) int
Torrents func(childComplexity int, filter *model.TorrentsFilter, pagination *model.Pagination) int
Torrents func(childComplexity int, filter *model.TorrentsFilter) int
}
ResolverFS struct {
@ -115,6 +115,7 @@ type ComplexityRoot struct {
Torrent struct {
BytesCompleted func(childComplexity int) int
BytesMissing func(childComplexity int) int
Downloading func(childComplexity int) int
ExcludedFiles func(childComplexity int) int
Files func(childComplexity int) int
Infohash func(childComplexity int) int
@ -166,7 +167,7 @@ type MutationResolver interface {
DedupeStorage(ctx context.Context) (int64, error)
}
type QueryResolver interface {
Torrents(ctx context.Context, filter *model.TorrentsFilter, pagination *model.Pagination) ([]*model.Torrent, error)
Torrents(ctx context.Context, filter *model.TorrentsFilter) ([]*model.Torrent, error)
FsEntry(ctx context.Context, path string) (model.FsEntry, error)
}
type ResolverFSResolver interface {
@ -316,7 +317,7 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
return 0, false
}
return e.complexity.Query.Torrents(childComplexity, args["filter"].(*model.TorrentsFilter), args["pagination"].(*model.Pagination)), true
return e.complexity.Query.Torrents(childComplexity, args["filter"].(*model.TorrentsFilter)), true
case "ResolverFS.entries":
if e.complexity.ResolverFS.Entries == nil {
@ -414,6 +415,13 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
return e.complexity.Torrent.BytesMissing(childComplexity), true
case "Torrent.downloading":
if e.complexity.Torrent.Downloading == nil {
break
}
return e.complexity.Torrent.Downloading(childComplexity), true
case "Torrent.excludedFiles":
if e.complexity.Torrent.ExcludedFiles == nil {
break
@ -731,7 +739,7 @@ type Task {
}
`, BuiltIn: false},
{Name: "../../../graphql/query.graphql", Input: `type Query {
torrents(filter: TorrentsFilter, pagination: Pagination): [Torrent!]!
torrents(filter: TorrentsFilter): [Torrent!]!
fsEntry(path: String!): FsEntry
}
@ -742,6 +750,7 @@ input TorrentsFilter {
bytesMissing: IntFilter
peersCount: IntFilter
downloading: BooleanFilter
}
input Pagination {
@ -859,6 +868,9 @@ type TorrentFileEntry implements File & FsEntry {
files: [TorrentFile!]!
excludedFiles: [TorrentFile!]!
peers: [TorrentPeer!]!
# if at least one piece of the torrent is request to download and not already downloaded
downloading: Boolean!
}
type TorrentFile {
@ -873,7 +885,8 @@ type TorrentPeer {
discovery: String!
port: Int!
clientName: String!
}`, BuiltIn: false},
}
`, BuiltIn: false},
}
var parsedSchema = gqlparser.MustLoadSchema(sources...)
@ -986,15 +999,6 @@ func (ec *executionContext) field_Query_torrents_args(ctx context.Context, rawAr
}
}
args["filter"] = arg0
var arg1 *model.Pagination
if tmp, ok := rawArgs["pagination"]; ok {
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("pagination"))
arg1, err = ec.unmarshalOPagination2ᚖgitᚗkmsignᚗruᚋroyalcatᚋtstorᚋsrcᚋdeliveryᚋgraphqlᚋmodelᚐPagination(ctx, tmp)
if err != nil {
return nil, err
}
}
args["pagination"] = arg1
return args, nil
}
@ -1546,7 +1550,7 @@ func (ec *executionContext) _Query_torrents(ctx context.Context, field graphql.C
}()
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
ctx = rctx // use context from middleware stack in children
return ec.resolvers.Query().Torrents(rctx, fc.Args["filter"].(*model.TorrentsFilter), fc.Args["pagination"].(*model.Pagination))
return ec.resolvers.Query().Torrents(rctx, fc.Args["filter"].(*model.TorrentsFilter))
})
if err != nil {
ec.Error(ctx, err)
@ -1587,6 +1591,8 @@ func (ec *executionContext) fieldContext_Query_torrents(ctx context.Context, fie
return ec.fieldContext_Torrent_excludedFiles(ctx, field)
case "peers":
return ec.fieldContext_Torrent_peers(ctx, field)
case "downloading":
return ec.fieldContext_Torrent_downloading(ctx, field)
}
return nil, fmt.Errorf("no field named %q was found under type Torrent", field.Name)
},
@ -2683,6 +2689,50 @@ func (ec *executionContext) fieldContext_Torrent_peers(ctx context.Context, fiel
return fc, nil
}
func (ec *executionContext) _Torrent_downloading(ctx context.Context, field graphql.CollectedField, obj *model.Torrent) (ret graphql.Marshaler) {
fc, err := ec.fieldContext_Torrent_downloading(ctx, field)
if err != nil {
return graphql.Null
}
ctx = graphql.WithFieldContext(ctx, fc)
defer func() {
if r := recover(); r != nil {
ec.Error(ctx, ec.Recover(ctx, r))
ret = graphql.Null
}
}()
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
ctx = rctx // use context from middleware stack in children
return obj.Downloading, nil
})
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
if resTmp == nil {
if !graphql.HasFieldError(ctx, fc) {
ec.Errorf(ctx, "must not be null")
}
return graphql.Null
}
res := resTmp.(bool)
fc.Result = res
return ec.marshalNBoolean2bool(ctx, field.Selections, res)
}
func (ec *executionContext) fieldContext_Torrent_downloading(ctx context.Context, field graphql.CollectedField) (fc *graphql.FieldContext, err error) {
fc = &graphql.FieldContext{
Object: "Torrent",
Field: field,
IsMethod: false,
IsResolver: false,
Child: func(ctx context.Context, field graphql.CollectedField) (*graphql.FieldContext, error) {
return nil, errors.New("field of type Boolean does not have child fields")
},
}
return fc, nil
}
func (ec *executionContext) _TorrentFS_name(ctx context.Context, field graphql.CollectedField, obj *model.TorrentFs) (ret graphql.Marshaler) {
fc, err := ec.fieldContext_TorrentFS_name(ctx, field)
if err != nil {
@ -2782,6 +2832,8 @@ func (ec *executionContext) fieldContext_TorrentFS_torrent(ctx context.Context,
return ec.fieldContext_Torrent_excludedFiles(ctx, field)
case "peers":
return ec.fieldContext_Torrent_peers(ctx, field)
case "downloading":
return ec.fieldContext_Torrent_downloading(ctx, field)
}
return nil, fmt.Errorf("no field named %q was found under type Torrent", field.Name)
},
@ -3064,6 +3116,8 @@ func (ec *executionContext) fieldContext_TorrentFileEntry_torrent(ctx context.Co
return ec.fieldContext_Torrent_excludedFiles(ctx, field)
case "peers":
return ec.fieldContext_Torrent_peers(ctx, field)
case "downloading":
return ec.fieldContext_Torrent_downloading(ctx, field)
}
return nil, fmt.Errorf("no field named %q was found under type Torrent", field.Name)
},
@ -3390,6 +3444,8 @@ func (ec *executionContext) fieldContext_TorrentProgress_torrent(ctx context.Con
return ec.fieldContext_Torrent_excludedFiles(ctx, field)
case "peers":
return ec.fieldContext_Torrent_peers(ctx, field)
case "downloading":
return ec.fieldContext_Torrent_downloading(ctx, field)
}
return nil, fmt.Errorf("no field named %q was found under type Torrent", field.Name)
},
@ -5773,7 +5829,7 @@ func (ec *executionContext) unmarshalInputTorrentsFilter(ctx context.Context, ob
asMap[k] = v
}
fieldsInOrder := [...]string{"infohash", "name", "bytesCompleted", "bytesMissing", "peersCount"}
fieldsInOrder := [...]string{"infohash", "name", "bytesCompleted", "bytesMissing", "peersCount", "downloading"}
for _, k := range fieldsInOrder {
v, ok := asMap[k]
if !ok {
@ -5815,6 +5871,13 @@ func (ec *executionContext) unmarshalInputTorrentsFilter(ctx context.Context, ob
return it, err
}
it.PeersCount = data
case "downloading":
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("downloading"))
data, err := ec.unmarshalOBooleanFilter2ᚖgitᚗkmsignᚗruᚋroyalcatᚋtstorᚋsrcᚋdeliveryᚋgraphqlᚋmodelᚐBooleanFilter(ctx, v)
if err != nil {
return it, err
}
it.Downloading = data
}
}
@ -6752,6 +6815,11 @@ func (ec *executionContext) _Torrent(ctx context.Context, sel ast.SelectionSet,
}
out.Concurrently(i, func(ctx context.Context) graphql.Marshaler { return innerFunc(ctx, out) })
case "downloading":
out.Values[i] = ec._Torrent_downloading(ctx, field, obj)
if out.Values[i] == graphql.Null {
atomic.AddUint32(&out.Invalids, 1)
}
default:
panic("unknown field " + strconv.Quote(field.Name))
}
@ -8008,6 +8076,14 @@ func (ec *executionContext) marshalOBoolean2ᚖbool(ctx context.Context, sel ast
return res
}
func (ec *executionContext) unmarshalOBooleanFilter2ᚖgitᚗkmsignᚗruᚋroyalcatᚋtstorᚋsrcᚋdeliveryᚋgraphqlᚋmodelᚐBooleanFilter(ctx context.Context, v interface{}) (*model.BooleanFilter, error) {
if v == nil {
return nil, nil
}
res, err := ec.unmarshalInputBooleanFilter(ctx, v)
return &res, graphql.ErrorOnPath(ctx, err)
}
func (ec *executionContext) unmarshalODateTime2ᚖtimeᚐTime(ctx context.Context, v interface{}) (*time.Time, error) {
if v == nil {
return nil, nil
@ -8107,14 +8183,6 @@ func (ec *executionContext) marshalOMutation2ᚖgitᚗkmsignᚗruᚋroyalcatᚋt
return ec._Mutation(ctx, sel)
}
func (ec *executionContext) unmarshalOPagination2ᚖgitᚗkmsignᚗruᚋroyalcatᚋtstorᚋsrcᚋdeliveryᚋgraphqlᚋmodelᚐPagination(ctx context.Context, v interface{}) (*model.Pagination, error) {
if v == nil {
return nil, nil
}
res, err := ec.unmarshalInputPagination(ctx, v)
return &res, graphql.ErrorOnPath(ctx, err)
}
func (ec *executionContext) marshalOProgress2gitᚗkmsignᚗruᚋroyalcatᚋtstorᚋsrcᚋdeliveryᚋgraphqlᚋmodelᚐProgress(ctx context.Context, sel ast.SelectionSet, v model.Progress) graphql.Marshaler {
if v == nil {
return graphql.Null

View file

@ -1,6 +1,8 @@
package model
import (
"context"
"git.kmsign.ru/royalcat/tstor/src/host/vfs"
)
@ -9,7 +11,7 @@ type FsElem interface {
IsDir() bool
}
func FillFsEntry(e FsElem, fs vfs.Filesystem, path string) FsEntry {
func FillFsEntry(ctx context.Context, e FsElem, fs vfs.Filesystem, path string) (FsEntry, error) {
switch e.(type) {
case *vfs.ArchiveFS:
e := e.(*vfs.ArchiveFS)
@ -17,31 +19,35 @@ func FillFsEntry(e FsElem, fs vfs.Filesystem, path string) FsEntry {
Name: e.Name(),
Size: e.Size(),
FS: e,
}
}, nil
case *vfs.ResolverFS:
e := e.(*vfs.ResolverFS)
return ResolverFs{
Name: e.Name(),
FS: e,
}
}, nil
case *vfs.TorrentFS:
e := e.(*vfs.TorrentFS)
torrent, err := MapTorrent(ctx, e.Torrent)
if err != nil {
return nil, err
}
return TorrentFs{
Name: e.Name(),
Torrent: MapTorrent(e.Torrent),
Torrent: torrent,
FS: e,
}
}, nil
default:
if e.IsDir() {
return SimpleDir{
Name: e.Name(),
FS: fs,
Path: path,
}
}, nil
} else {
return SimpleFile{
Name: e.Name(),
}
}, nil
}
}
}

View file

@ -42,3 +42,13 @@ func (f *StringFilter) Include(v string) bool {
return true
}
func (f *BooleanFilter) Include(v bool) bool {
if f == nil {
return true
} else if f.Eq != nil {
return v == *f.Eq
}
return true
}

View file

@ -1,6 +1,8 @@
package model
import (
"context"
"git.kmsign.ru/royalcat/tstor/src/host/controller"
"github.com/anacrolix/torrent"
)
@ -26,12 +28,25 @@ func MapPeerSource(source torrent.PeerSource) string {
}
}
func MapTorrent(t *controller.Torrent) *Torrent {
func MapTorrent(ctx context.Context, t *controller.Torrent) (*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() {
downloading = true
break
}
}
return &Torrent{
Infohash: t.InfoHash(),
Name: t.Name(),
BytesCompleted: t.BytesCompleted(),
BytesMissing: t.BytesMissing(),
T: t,
}
Downloading: downloading,
}, nil
}

View file

@ -179,6 +179,7 @@ type Torrent struct {
Files []*TorrentFile `json:"files"`
ExcludedFiles []*TorrentFile `json:"excludedFiles"`
Peers []*TorrentPeer `json:"peers"`
Downloading bool `json:"downloading"`
T *controller.Torrent `json:"-"`
}
@ -248,9 +249,10 @@ func (this TorrentProgress) GetCurrent() int64 { return this.Current }
func (this TorrentProgress) GetTotal() int64 { return this.Total }
type TorrentsFilter struct {
Infohash *StringFilter `json:"infohash,omitempty"`
Name *StringFilter `json:"name,omitempty"`
BytesCompleted *IntFilter `json:"bytesCompleted,omitempty"`
BytesMissing *IntFilter `json:"bytesMissing,omitempty"`
PeersCount *IntFilter `json:"peersCount,omitempty"`
Infohash *StringFilter `json:"infohash,omitempty"`
Name *StringFilter `json:"name,omitempty"`
BytesCompleted *IntFilter `json:"bytesCompleted,omitempty"`
BytesMissing *IntFilter `json:"bytesMissing,omitempty"`
PeersCount *IntFilter `json:"peersCount,omitempty"`
Downloading *BooleanFilter `json:"downloading,omitempty"`
}

View file

@ -19,7 +19,11 @@ func (r *archiveFSResolver) Entries(ctx context.Context, obj *model.ArchiveFs) (
}
out := []model.FsEntry{}
for _, e := range entries {
out = append(out, model.FillFsEntry(e, obj.FS, "."))
entry, err := model.FillFsEntry(ctx, e, obj.FS, ".")
if err != nil {
return nil, err
}
out = append(out, entry)
}
return out, nil
}
@ -32,7 +36,11 @@ func (r *resolverFSResolver) Entries(ctx context.Context, obj *model.ResolverFs)
}
out := []model.FsEntry{}
for _, e := range entries {
out = append(out, model.FillFsEntry(e, obj.FS, "."))
entry, err := model.FillFsEntry(ctx, e, obj.FS, ".")
if err != nil {
return nil, err
}
out = append(out, entry)
}
return out, nil
}
@ -45,7 +53,11 @@ func (r *simpleDirResolver) Entries(ctx context.Context, obj *model.SimpleDir) (
}
out := []model.FsEntry{}
for _, e := range entries {
out = append(out, model.FillFsEntry(e, obj.FS, obj.Path))
entry, err := model.FillFsEntry(ctx, e, obj.FS, obj.Path)
if err != nil {
return nil, err
}
out = append(out, entry)
}
return out, nil
}
@ -58,7 +70,11 @@ func (r *torrentFSResolver) Entries(ctx context.Context, obj *model.TorrentFs) (
}
out := []model.FsEntry{}
for _, e := range entries {
out = append(out, model.FillFsEntry(e, obj.FS, "."))
entry, err := model.FillFsEntry(ctx, e, obj.FS, ".")
if err != nil {
return nil, err
}
out = append(out, entry)
}
return out, nil
}

View file

@ -15,7 +15,7 @@ import (
)
// Torrents is the resolver for the torrents field.
func (r *queryResolver) Torrents(ctx context.Context, filter *model.TorrentsFilter, pagination *model.Pagination) ([]*model.Torrent, error) {
func (r *queryResolver) Torrents(ctx context.Context, filter *model.TorrentsFilter) ([]*model.Torrent, error) {
torrents, err := r.Service.ListTorrents(ctx)
if err != nil {
return nil, err
@ -49,6 +49,13 @@ func (r *queryResolver) Torrents(ctx context.Context, filter *model.TorrentsFilt
})
}
if filter.Downloading != nil {
filterFuncs = append(filterFuncs, func(torrent *model.Torrent) bool {
return filter.Downloading.Include(
torrent.Downloading,
)
})
}
}
filterFunc := func(torrent *model.Torrent) bool {
@ -62,7 +69,10 @@ func (r *queryResolver) Torrents(ctx context.Context, filter *model.TorrentsFilt
tr := []*model.Torrent{}
for _, t := range torrents {
d := model.MapTorrent(t)
d, err := model.MapTorrent(ctx, t)
if err != nil {
return nil, err
}
if !filterFunc(d) {
continue
@ -71,7 +81,7 @@ func (r *queryResolver) Torrents(ctx context.Context, filter *model.TorrentsFilt
}
slices.SortStableFunc(torrents, func(t1, t2 *controller.Torrent) int {
return strings.Compare(t1.InfoHash(), t2.InfoHash())
return strings.Compare(t1.Name(), t2.Name())
})
return tr, nil
@ -84,7 +94,7 @@ func (r *queryResolver) FsEntry(ctx context.Context, path string) (model.FsEntry
return nil, err
}
return model.FillFsEntry(entry, r.VFS, path), nil
return model.FillFsEntry(ctx, entry, r.VFS, path)
}
// Query returns graph.QueryResolver implementation.

View file

@ -32,8 +32,13 @@ func (r *subscriptionResolver) TorrentDownloadUpdates(ctx context.Context) (<-ch
fmt.Println("nil torrent")
continue
}
torrent, err := model.MapTorrent(ctx, p.Torrent)
if err != nil {
// TODO logs
continue
}
po := &model.TorrentProgress{
Torrent: model.MapTorrent(p.Torrent),
Torrent: torrent,
Current: p.Current,
Total: p.Total,
}

View file

@ -20,7 +20,7 @@ func New(fc *filecache.Cache, ss *service.Stats, s *service.Service, vfs vfs.Fil
r := echo.New()
r.Use(
middleware.Recover(),
// middleware.Recover(),
middleware.Gzip(),
middleware.Decompress(),
Logger(),