audio.go 963 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package read
  2. import (
  3. "os"
  4. "path/filepath"
  5. tag "github.com/dhowden/tag"
  6. duration "github.com/felamaslen/gmus-backend/pkg/read/duration"
  7. "github.com/felamaslen/gmus-backend/pkg/types"
  8. )
  9. func ReadFile(basePath string, scannedFile *types.File) (song *types.Song, err error) {
  10. fullPath := filepath.Join(basePath, scannedFile.RelativePath)
  11. file, errFile := os.Open(fullPath)
  12. if errFile != nil {
  13. return &types.Song{}, errFile
  14. }
  15. defer file.Close()
  16. tags, errTags := tag.ReadFrom(file)
  17. if errTags != nil {
  18. return &types.Song{}, errTags
  19. }
  20. durationSeconds := duration.GetSongDurationSeconds(file, tags)
  21. trackNumber, _ := tags.Track()
  22. result := types.Song{
  23. TrackNumber: trackNumber,
  24. Title: tags.Title(),
  25. Artist: tags.Artist(),
  26. Album: tags.Album(),
  27. Duration: durationSeconds,
  28. BasePath: basePath,
  29. RelativePath: scannedFile.RelativePath,
  30. ModifiedDate: scannedFile.ModifiedDate,
  31. }
  32. return &result, nil
  33. }