60 lines
1.3 KiB
Go
60 lines
1.3 KiB
Go
package decrypt
|
|
|
|
import (
|
|
"io"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"jsuse.com/dev/dec-music/log"
|
|
)
|
|
|
|
// DecoderParams configures a decoder instance.
|
|
type DecoderParams struct {
|
|
Reader io.ReadSeeker
|
|
Extension string
|
|
FilePath string
|
|
Logger *log.Logger
|
|
}
|
|
|
|
// Factory creates decoders for a registered file suffix.
|
|
type Factory struct {
|
|
Suffix string
|
|
Noop bool
|
|
Create func(*DecoderParams) Decoder
|
|
}
|
|
|
|
var registry []Factory
|
|
|
|
// RegisterDecoder registers a file suffix handler (called from format init).
|
|
func RegisterDecoder(ext string, noop bool, fn func(*DecoderParams) Decoder) {
|
|
registry = append(registry, Factory{
|
|
Noop: noop, Create: fn, Suffix: "." + strings.TrimPrefix(ext, "."),
|
|
})
|
|
}
|
|
|
|
// GetDecoder returns matching factories for a file name.
|
|
func GetDecoder(filename string, skipNoop bool) []Factory {
|
|
var result []Factory
|
|
name := strings.ToLower(filepath.Base(filename))
|
|
for _, dec := range registry {
|
|
if !strings.HasSuffix(name, dec.Suffix) {
|
|
continue
|
|
}
|
|
if skipNoop && dec.Noop {
|
|
continue
|
|
}
|
|
result = append(result, dec)
|
|
}
|
|
return result
|
|
}
|
|
|
|
// SupportedExtensions returns registered suffixes and handler counts.
|
|
func SupportedExtensions() map[string]int {
|
|
extSet := make(map[string]int)
|
|
for _, f := range registry {
|
|
ext := strings.TrimPrefix(f.Suffix, ".")
|
|
extSet[ext]++
|
|
}
|
|
return extSet
|
|
}
|