refactor: change decoder init parameter

This commit is contained in:
Unlock Music Dev 2022-12-04 23:05:38 +08:00
parent ad64a0f91d
commit ea3236e14b
No known key found for this signature in database
GPG Key ID: 95202E10D3413A1D
10 changed files with 32 additions and 20 deletions

View File

@ -6,7 +6,13 @@ import (
"strings"
)
type NewDecoderFunc func(rd io.ReadSeeker) Decoder
type DecoderParams struct {
Reader io.ReadSeeker // required
Extension string // required, source extension, eg. ".mp3"
FilePath string // optional, source file path
}
type NewDecoderFunc func(p *DecoderParams) Decoder
type decoderItem struct {
noop bool

View File

@ -14,8 +14,8 @@ type RawDecoder struct {
audioExt string
}
func NewRawDecoder(rd io.ReadSeeker) Decoder {
return &RawDecoder{rd: rd}
func NewRawDecoder(p *DecoderParams) Decoder {
return &RawDecoder{rd: p.Reader}
}
func (d *RawDecoder) Validate() error {

View File

@ -16,8 +16,8 @@ type Decoder struct {
header header
}
func NewDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{rd: rd}
func NewDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{rd: p.Reader}
}
// Validate checks if the file is a valid Kugou (.kgm, .vpr, .kgma) file.

View File

@ -30,8 +30,8 @@ func (d *Decoder) GetAudioExt() string {
return "." + d.outputExt
}
func NewDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{rd: rd}
func NewDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{rd: p.Reader}
}
// Validate checks if the file is a valid Kuwo .kw file.

View File

@ -29,10 +29,8 @@ var (
}
)
func NewDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{
rd: rd,
}
func NewDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{rd: p.Reader}
}
type Decoder struct {

View File

@ -38,8 +38,8 @@ func (d *Decoder) Read(p []byte) (int, error) {
return n, err
}
func NewDecoder(r io.ReadSeeker) common.Decoder {
return &Decoder{raw: r}
func NewDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{raw: p.Reader}
}
func (d *Decoder) Validate() error {
@ -214,6 +214,8 @@ func init() {
"mgg", "mgg1", "mggl", //QQ Music New Ogg
"mflac", "mflac0", //QQ Music New Flac
"mflach", // QQ Music Flac (storing key in dedicate MMKV)
}
for _, ext := range supportedExts {
common.RegisterDecoder(ext, false, NewDecoder)

View File

@ -43,8 +43,8 @@ func (d *Decoder) Read(buf []byte) (int, error) {
return d.audio.Read(buf)
}
func NewTmDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{raw: rd}
func NewTmDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{raw: p.Reader}
}
func init() {

View File

@ -37,8 +37,8 @@ func (d *Decoder) GetAudioExt() string {
return ""
}
func NewDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{rd: rd}
func NewDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{rd: p.Reader}
}
// Validate checks if the file is a valid xiami .xm file.

View File

@ -16,8 +16,8 @@ type Decoder struct {
audio io.Reader
}
func NewDecoder(rd io.ReadSeeker) common.Decoder {
return &Decoder{rd: rd}
func NewDecoder(p *common.DecoderParams) common.Decoder {
return &Decoder{rd: p.Reader}
}
func (d *Decoder) Validate() error {

View File

@ -163,9 +163,15 @@ func tryDecFile(inputFile string, outputDir string, allDec []common.NewDecoderFu
}
defer file.Close()
decParams := &common.DecoderParams{
Reader: file,
Extension: filepath.Ext(inputFile),
FilePath: inputFile,
}
var dec common.Decoder
for _, decFunc := range allDec {
dec = decFunc(file)
dec = decFunc(decParams)
if err := dec.Validate(); err == nil {
break
} else {