musicutil/utils/scan_for_music.go

54 lines
976 B
Go
Raw Normal View History

2022-02-22 14:02:58 +00:00
package utils
import (
"os"
"path/filepath"
"strings"
"gitlab.com/ChaotiCryptidz/musicutil/types"
)
/*
def scan_for_music(src: str) -> list[File]:
files: list[File] = []
for format in supported_formats:
for path in Path(src).rglob("*." + format):
files.append(file_from_path(path, src))
return files
*/
func isValidFileExtension(filePath string) bool {
ext := strings.TrimPrefix(filepath.Ext(filePath), ".")
if ext == "mp3" {
return true
} else if ext == "flac" {
return true
}
return false
}
func ScanForMusic(srcDir string) ([]*types.File, error) {
files := make([]*types.File, 0)
err := filepath.Walk(srcDir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() && isValidFileExtension(path) {
files = append(files, types.FileFromPath(
srcDir,
path,
))
}
return nil
})
if err != nil {
return nil, err
}
return files, nil
}