You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.1 KiB
Go
50 lines
1.1 KiB
Go
package common
|
|
|
|
import (
|
|
"io"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type DecoderParams struct {
|
|
Reader io.ReadSeeker // required
|
|
Extension string // required, source extension, eg. ".mp3"
|
|
|
|
FilePath string // optional, source file path
|
|
|
|
Logger *zap.Logger // required
|
|
}
|
|
type NewDecoderFunc func(p *DecoderParams) Decoder
|
|
|
|
type DecoderFactory struct {
|
|
noop bool
|
|
Suffix string
|
|
Create NewDecoderFunc
|
|
}
|
|
|
|
var DecoderRegistry []DecoderFactory
|
|
|
|
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
|
|
DecoderRegistry = append(DecoderRegistry,
|
|
DecoderFactory{noop: noop, Create: dispatchFunc, Suffix: "." + strings.TrimPrefix(ext, ".")})
|
|
}
|
|
|
|
func GetDecoder(filename string, skipNoop bool) []DecoderFactory {
|
|
var result []DecoderFactory
|
|
// Some extensions contains multiple dots, e.g. ".kgm.flac", hence iterate
|
|
// all decoders for each extension.
|
|
name := strings.ToLower(filepath.Base(filename))
|
|
for _, dec := range DecoderRegistry {
|
|
if !strings.HasSuffix(name, dec.Suffix) {
|
|
continue
|
|
}
|
|
if skipNoop && dec.noop {
|
|
continue
|
|
}
|
|
result = append(result, dec)
|
|
}
|
|
return result
|
|
}
|