feat: auto rename on duplicate (#76)

This commit is contained in:
Li Wei authored and GitHub committed 2023-02-10 15:18:31 +08:00
1 parent 048554df4d
commit 0515ce6081
8 files changed
+205 -152

No files matched your search

+9
View File
@@ -3,6 +3,7 @@ package fetcher
import (
"github.com/monkeyWie/gopeed/internal/controller"
"github.com/monkeyWie/gopeed/pkg/base"
"path"
)
// Fetcher defines the interface for a download protocol.
@@ -36,6 +37,14 @@ type FetcherMeta struct {
Opts *base.Options `json:"opts"`
}
func (m *FetcherMeta) Filepath(file *base.FileInfo) string {
finalName := m.Opts.Name
if finalName == "" {
finalName = file.Name
}
return path.Join(m.Opts.Path, m.Res.RootDir, finalName)
}
// FetcherBuilder defines the interface for a fetcher builder.
type FetcherBuilder interface {
// Schemes returns the schemes supported by the fetcher.
+8 -6
View File
@@ -8,6 +8,7 @@ import (
"github.com/monkeyWie/gopeed/internal/fetcher"
"github.com/monkeyWie/gopeed/pkg/base"
"github.com/monkeyWie/gopeed/pkg/util"
"path"
"path/filepath"
"sync"
"sync/atomic"
@@ -74,15 +75,16 @@ func (f *Fetcher) Resolve(req *base.Request) error {
}
defer f.torrent.Drop()
res := &base.Resource{
Name: f.torrent.Name(),
Range: true,
Files: make([]*base.FileInfo, len(f.torrent.Files())),
Hash: f.torrent.InfoHash().String(),
Name: f.torrent.Name(),
Range: true,
RootDir: f.torrent.Name(),
Files: make([]*base.FileInfo, len(f.torrent.Files())),
Hash: f.torrent.InfoHash().String(),
}
for i, file := range f.torrent.Files() {
res.Files[i] = &base.FileInfo{
Name: filepath.Base(file.DisplayPath()),
Path: util.Dir(file.Path()),
Path: util.Dir(path.Join(f.torrent.Info().Name, file.Path())),
Size: file.Length(),
}
res.Size += file.Length()
@@ -101,7 +103,7 @@ func (f *Fetcher) Create(opts *base.Options) (err error) {
}
}
if opts.Path != "" {
f.torrentPaths[f.meta.Res.Hash] = f.meta.Opts.Path
f.torrentPaths[f.meta.Res.Hash] = path.Join(f.meta.Opts.Path, f.meta.Res.RootDir)
}
f.progress = make(fetcher.Progress, len(f.meta.Opts.SelectFiles))
+1
View File
@@ -33,6 +33,7 @@ func doResolve(t *testing.T, fetcher fetcher.Fetcher) {
Files: []*base.FileInfo{
{
Name: "ubuntu-22.04-live-server-amd64.iso",
Path: "ubuntu-22.04-live-server-amd64.iso",
Size: 1466714112,
},
},
+1 -2
View File
@@ -8,7 +8,6 @@ import (
"github.com/monkeyWie/gopeed/internal/fetcher"
"github.com/monkeyWie/gopeed/pkg/base"
fhttp "github.com/monkeyWie/gopeed/pkg/protocol/http"
"github.com/monkeyWie/gopeed/pkg/util"
"golang.org/x/sync/errgroup"
"io"
"mime"
@@ -245,7 +244,7 @@ func (f *Fetcher) Wait() (err error) {
}
func (f *Fetcher) filepath() string {
return util.Filepath(f.meta.Opts.Path, f.meta.Res.Files[0].Name, f.meta.Opts.Name)
return f.meta.Filepath(f.meta.Res.Files[0])
}
func (f *Fetcher) fetch() {