Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
vintikzzz committed Jan 19, 2023
1 parent 59ad55f commit a42debe
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
32 changes: 28 additions & 4 deletions services/torrent_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package services
import (
"bytes"
"context"
"strings"
"time"

"github.com/anacrolix/torrent/metainfo"
Expand All @@ -26,7 +27,15 @@ func NewTorrentStore(ts *TorrentStoreClient) *TorrentStore {
}
}

func (s *TorrentStore) get(h string) (*metainfo.MetaInfo, error) {
func getPath(info *metainfo.Info, f *metainfo.FileInfo) []string {
res := []string{info.Name}
if len(f.Path) > 0 {
res = append(res, f.Path...)
}
return res
}

func (s *TorrentStore) get(h string) ([]file, error) {
c, err := s.ts.Get()
if err != nil {
return nil, errors.Wrap(err, "failed to get torrent store client")
Expand All @@ -43,15 +52,30 @@ func (s *TorrentStore) get(h string) (*metainfo.MetaInfo, error) {
return nil, errors.Wrap(err, "failed to parse torrent")
}
log.Info("torrent pulled successfully")
return mi, nil
info, err := mi.UnmarshalInfo()
if err != nil {
return nil, err
}
var res []file
for _, f := range info.UpvertedFiles() {
p := getPath(&info, &f)
path := strings.Join(p, "/")
res = append(res, file{
path: path,
size: uint64(f.Length),
modified: time.Unix(mi.CreationDate, 0),
})
}

return res, nil
}

func (s *TorrentStore) Get(h string) (*metainfo.MetaInfo, error) {
func (s *TorrentStore) Get(h string) ([]file, error) {
mi, err := s.LazyMap.Get(h, func() (interface{}, error) {
return s.get(h)
})
if err != nil {
return nil, err
}
return mi.(*metainfo.MetaInfo), nil
return mi.([]file), nil
}
28 changes: 4 additions & 24 deletions services/zip.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ import (

"github.com/pkg/errors"

"github.com/anacrolix/torrent/metainfo"

log "github.com/sirupsen/logrus"
)

Expand Down Expand Up @@ -107,14 +105,6 @@ func (s *Zip) writeFile(zw *zip.Writer, f file, fw *folderWriter) error {
return nil
}

func getPath(info *metainfo.Info, f *metainfo.FileInfo) []string {
res := []string{info.Name}
if len(f.Path) > 0 {
res = append(res, f.Path...)
}
return res
}

func (s *Zip) Size() (size int64, err error) {
files, err := s.generateFileList()
if err != nil {
Expand Down Expand Up @@ -149,24 +139,14 @@ func (s *Zip) Size() (size int64, err error) {
return
}
func (s *Zip) generateFileList() ([]file, error) {
mi, err := s.ts.Get(s.infoHash)
if err != nil {
return nil, err
}
info, err := mi.UnmarshalInfo()
files, err := s.ts.Get(s.infoHash)
if err != nil {
return nil, err
}
var res []file
for _, f := range info.UpvertedFiles() {
p := getPath(&info, &f)
path := strings.Join(p, "/")
if strings.HasPrefix(path, s.path) {
res = append(res, file{
path: path,
size: uint64(f.Length),
modified: time.Unix(mi.CreationDate, 0),
})
for _, f := range files {
if strings.HasPrefix(f.path, s.path) {
res = append(res, f)
}
}
return res, nil
Expand Down

0 comments on commit a42debe

Please sign in to comment.