mirror of
https://github.com/GopeedLab/gopeed.git
synced 2026-11-08 09:30:27 +00:00
fix: clear UPNP port mapping when exist app (#624)
This commit is contained in:
22 files changed
+506
-320
No files matched your search
@@ -4,6 +4,7 @@ import (
|
||||
"github.com/GopeedLab/gopeed/internal/controller"
|
||||
"github.com/GopeedLab/gopeed/pkg/base"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Fetcher defines the interface for a download protocol.
|
||||
@@ -71,12 +72,42 @@ func (m *FetcherMeta) RootDirPath() string {
|
||||
}
|
||||
}
|
||||
|
||||
// FetcherBuilder defines the interface for a fetcher builder.
|
||||
type FetcherBuilder interface {
|
||||
type FilterType int
|
||||
|
||||
const (
|
||||
// FilterTypeUrl url type, pattern is the scheme, e.g. http://github.com -> http
|
||||
FilterTypeUrl FilterType = iota
|
||||
// FilterTypeFile file type, pattern is the file extension name, e.g. test.torrent -> torrent
|
||||
FilterTypeFile
|
||||
// FilterTypeBase64 base64 data type, pattern is the data mime type, e.g. data:application/x-bittorrent;base64 -> application/x-bittorrent
|
||||
FilterTypeBase64
|
||||
)
|
||||
|
||||
type SchemeFilter struct {
|
||||
Type FilterType
|
||||
Pattern string
|
||||
}
|
||||
|
||||
func (s *SchemeFilter) Match(uri string) bool {
|
||||
uriUpper := strings.ToUpper(uri)
|
||||
patternUpper := strings.ToUpper(s.Pattern)
|
||||
switch s.Type {
|
||||
case FilterTypeUrl:
|
||||
return strings.HasPrefix(uriUpper, patternUpper+":")
|
||||
case FilterTypeFile:
|
||||
return strings.HasSuffix(uriUpper, "."+patternUpper)
|
||||
case FilterTypeBase64:
|
||||
return strings.HasPrefix(uriUpper, "DATA:"+patternUpper+";BASE64,")
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// FetcherManager manage and control the fetcher
|
||||
type FetcherManager interface {
|
||||
// Name return the name of the protocol.
|
||||
Name() string
|
||||
// Schemes returns the schemes supported by the fetcher.
|
||||
Schemes() []string
|
||||
// Filters registers the supported schemes.
|
||||
Filters() []*SchemeFilter
|
||||
// Build returns a new fetcher.
|
||||
Build() Fetcher
|
||||
|
||||
@@ -86,6 +117,8 @@ type FetcherBuilder interface {
|
||||
Store(fetcher Fetcher) (any, error)
|
||||
// Restore fetcher
|
||||
Restore() (v any, f func(meta *FetcherMeta, v any) Fetcher)
|
||||
// Close the fetcher manager, release resources.
|
||||
Close() error
|
||||
}
|
||||
|
||||
type DefaultFetcher struct {
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
package fetcher
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSchemeFilter_Match(t *testing.T) {
|
||||
type fields struct {
|
||||
Type FilterType
|
||||
Pattern string
|
||||
}
|
||||
type args struct {
|
||||
uri string
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
args args
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "url match",
|
||||
fields: fields{
|
||||
Type: FilterTypeUrl,
|
||||
Pattern: "https",
|
||||
},
|
||||
args: args{
|
||||
uri: "https://github.com",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "url not match",
|
||||
fields: fields{
|
||||
Type: FilterTypeUrl,
|
||||
Pattern: "https",
|
||||
},
|
||||
args: args{
|
||||
uri: "ftp://github.com",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "file match",
|
||||
fields: fields{
|
||||
Type: FilterTypeFile,
|
||||
Pattern: "torrent",
|
||||
},
|
||||
args: args{
|
||||
uri: "d:/temp/test.torrent",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "file not match",
|
||||
fields: fields{
|
||||
Type: FilterTypeFile,
|
||||
Pattern: "torrent",
|
||||
},
|
||||
args: args{
|
||||
uri: "d:/temp/test.txt",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "base64 match",
|
||||
fields: fields{
|
||||
Type: FilterTypeBase64,
|
||||
Pattern: "application/x-bittorrent",
|
||||
},
|
||||
args: args{
|
||||
uri: "data:application/x-bittorrent;base64,xxx",
|
||||
},
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "base64 not match",
|
||||
fields: fields{
|
||||
Type: FilterTypeBase64,
|
||||
Pattern: "application/x-bittorrent",
|
||||
},
|
||||
args: args{
|
||||
uri: "data:application/javascript;base64,xxx",
|
||||
},
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
s := &SchemeFilter{
|
||||
Type: tt.fields.Type,
|
||||
Pattern: tt.fields.Pattern,
|
||||
}
|
||||
if got := s.Match(tt.args.uri); got != tt.want {
|
||||
t.Errorf("Match() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user