files.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. package read
  2. import (
  3. "io/ioutil"
  4. "path/filepath"
  5. config "github.com/felamaslen/go-music-player/pkg/config"
  6. "github.com/felamaslen/go-music-player/pkg/database"
  7. "github.com/felamaslen/go-music-player/pkg/logger"
  8. "github.com/jmoiron/sqlx"
  9. )
  10. func ReadMultipleFiles(basePath string, files chan *File) chan *Song {
  11. var l = logger.CreateLogger(config.GetConfig().LogLevel)
  12. songs := make(chan *Song)
  13. go func() {
  14. defer func() {
  15. l.Verbose("Finished reading files")
  16. close(songs)
  17. }()
  18. for {
  19. select {
  20. case file, more := <- files:
  21. if more {
  22. l.Verbose("Reading file: %s\n", file.RelativePath)
  23. song, err := ReadFile(basePath, file)
  24. if err == nil {
  25. songs <- song
  26. } else {
  27. l.Error("Error reading file (%s): %s\n", file.RelativePath, err)
  28. }
  29. } else {
  30. return
  31. }
  32. }
  33. }
  34. }()
  35. return songs
  36. }
  37. func isValidFile(file string) bool {
  38. // TODO: support FLAC/MP3
  39. return filepath.Ext(file) == ".ogg"
  40. }
  41. func recursiveDirScan(
  42. db *sqlx.DB,
  43. l *logger.Logger,
  44. output *chan *File,
  45. rootDirectory string,
  46. relativePath string,
  47. isRoot bool,
  48. ) {
  49. directoryToScan := filepath.Join(rootDirectory, relativePath)
  50. if (isRoot) {
  51. l.Verbose("Scanning root directory: %s\n", directoryToScan)
  52. defer func() {
  53. l.Verbose("Finished recursive directory scan")
  54. close(*output)
  55. }()
  56. } else {
  57. l.Debug("Scanning subdirectory: %s\n", directoryToScan)
  58. }
  59. files, err := ioutil.ReadDir(directoryToScan)
  60. if err != nil {
  61. l.Fatal("Error scanning directory: (%s): %s", directoryToScan, err)
  62. return
  63. }
  64. for _, file := range(files) {
  65. fileRelativePath := filepath.Join(relativePath, file.Name())
  66. if file.IsDir() {
  67. recursiveDirScan(
  68. db,
  69. l,
  70. output,
  71. rootDirectory,
  72. fileRelativePath,
  73. false,
  74. )
  75. } else if isValidFile(file.Name()) {
  76. modifiedDate := file.ModTime().Unix()
  77. var existingCount = 0
  78. err := db.Get(
  79. &existingCount,
  80. `
  81. select count(*) from songs
  82. where base_path = $1 and relative_path = $2 and modified_date = $3
  83. `,
  84. rootDirectory,
  85. fileRelativePath,
  86. modifiedDate,
  87. )
  88. if err == nil && existingCount == 0 {
  89. l.Verbose("Found file: %s\n", fileRelativePath)
  90. *output <- &File{
  91. RelativePath: fileRelativePath,
  92. ModifiedDate: modifiedDate,
  93. }
  94. }
  95. }
  96. }
  97. }
  98. func ScanDirectory(directory string) chan *File {
  99. db := database.GetConnection()
  100. l := logger.CreateLogger(config.GetConfig().LogLevel)
  101. files := make(chan *File)
  102. go func() {
  103. recursiveDirScan(
  104. db,
  105. l,
  106. &files,
  107. directory,
  108. "",
  109. true,
  110. )
  111. }()
  112. return files
  113. }