2020-12-26 07:47:10 +00:00
|
|
|
package common
|
|
|
|
|
2021-11-11 15:27:07 +00:00
|
|
|
import (
|
2022-11-18 23:25:43 +00:00
|
|
|
"io"
|
2021-11-11 15:27:07 +00:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2022-12-05 00:54:40 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2021-11-11 15:27:07 +00:00
|
|
|
)
|
|
|
|
|
2022-12-04 15:05:38 +00:00
|
|
|
type DecoderParams struct {
|
|
|
|
Reader io.ReadSeeker // required
|
|
|
|
Extension string // required, source extension, eg. ".mp3"
|
|
|
|
|
|
|
|
FilePath string // optional, source file path
|
2022-12-05 00:54:40 +00:00
|
|
|
|
|
|
|
Logger *zap.Logger // required
|
2022-12-04 15:05:38 +00:00
|
|
|
}
|
|
|
|
type NewDecoderFunc func(p *DecoderParams) Decoder
|
2020-12-26 07:47:10 +00:00
|
|
|
|
2024-10-20 18:57:56 +00:00
|
|
|
type DecoderFactory struct {
|
|
|
|
noop bool
|
|
|
|
Suffix string
|
|
|
|
Create NewDecoderFunc
|
2021-11-11 15:43:20 +00:00
|
|
|
}
|
|
|
|
|
2024-10-20 18:57:56 +00:00
|
|
|
var DecoderRegistry []DecoderFactory
|
2020-12-26 07:47:10 +00:00
|
|
|
|
2021-11-11 15:43:20 +00:00
|
|
|
func RegisterDecoder(ext string, noop bool, dispatchFunc NewDecoderFunc) {
|
2024-10-20 18:57:56 +00:00
|
|
|
DecoderRegistry = append(DecoderRegistry,
|
|
|
|
DecoderFactory{noop: noop, Create: dispatchFunc, Suffix: "." + strings.TrimPrefix(ext, ".")})
|
2020-12-26 07:47:10 +00:00
|
|
|
}
|
2022-11-21 23:08:10 +00:00
|
|
|
|
2024-10-20 18:57:56 +00:00
|
|
|
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
|
|
|
|
}
|
2021-11-11 15:43:20 +00:00
|
|
|
if skipNoop && dec.noop {
|
|
|
|
continue
|
|
|
|
}
|
2024-10-20 18:57:56 +00:00
|
|
|
result = append(result, dec)
|
2021-11-11 15:43:20 +00:00
|
|
|
}
|
2024-10-20 18:57:56 +00:00
|
|
|
return result
|
2020-12-26 07:47:10 +00:00
|
|
|
}
|