Added concurrent sync, added search page. Other small changes
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"music-server/pkg/models"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var currentSong = -1
|
||||
@@ -232,9 +233,9 @@ func getSongFromList(games []models.GameData) models.SongData {
|
||||
var song models.SongData
|
||||
for !songFound {
|
||||
game := getRandomGame(games)
|
||||
log.Println("game = ", game)
|
||||
//log.Println("game = ", game)
|
||||
songs := db.FindSongsFromGame(game.Id)
|
||||
log.Println("songs = ", songs)
|
||||
//log.Println("songs = ", songs)
|
||||
if len(songs) == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -243,10 +244,10 @@ func getSongFromList(games []models.GameData) models.SongData {
|
||||
|
||||
//Check if file exists and open
|
||||
openFile, err := os.Open(song.Path)
|
||||
log.Println("game.Path+song.FileName: ", game.Path+song.FileName)
|
||||
log.Println("song.Path: ", song.Path)
|
||||
log.Println("game.Path+song.FileName != song.Path: ", game.Path+song.FileName != song.Path)
|
||||
if err != nil || game.Path+song.FileName != song.Path {
|
||||
//log.Println("game.Path+song.FileName: ", game.Path+song.FileName)
|
||||
//log.Println("song.Path: ", song.Path)
|
||||
//log.Println("game.Path+song.FileName != song.Path: ", game.Path+song.FileName != song.Path)
|
||||
if err != nil || game.Path+song.FileName != song.Path || strings.HasSuffix(song.FileName, ".wav") {
|
||||
//File not found
|
||||
db.RemoveBrokenSong(song)
|
||||
log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + game.GameName + "' FileName: " + song.FileName)
|
||||
|
||||
@@ -10,9 +10,11 @@ import (
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// SyncGames TODO: Make sync concurrent
|
||||
var wg sync.WaitGroup
|
||||
|
||||
func SyncGames() {
|
||||
host := os.Getenv("DB_HOST")
|
||||
var dir string
|
||||
@@ -33,33 +35,66 @@ func SyncGames() {
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if file.IsDir() && !contains(foldersToSkip, file.Name()) {
|
||||
fmt.Println(file.Name())
|
||||
path := dir + file.Name() + "/"
|
||||
fmt.Println(path)
|
||||
syncGame(file, foldersToSkip, dir)
|
||||
}
|
||||
}
|
||||
|
||||
entries, err := os.ReadDir(path)
|
||||
func SyncGamesQuick() {
|
||||
host := os.Getenv("DB_HOST")
|
||||
var dir string
|
||||
if host != "" {
|
||||
dir = "/sorted/"
|
||||
} else {
|
||||
dir = "/Users/sebastian/Resilio Sync/Sorterat_test/"
|
||||
}
|
||||
fmt.Printf("dir: %s\n", dir)
|
||||
foldersToSkip := []string{".sync", "dist", "old"}
|
||||
fmt.Println(foldersToSkip)
|
||||
db.SetGameDeletionDate()
|
||||
checkBrokenSongs()
|
||||
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
syncGame(file, foldersToSkip, dir)
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func syncGame(file os.DirEntry, foldersToSkip []string, dir string) {
|
||||
if file.IsDir() && !contains(foldersToSkip, file.Name()) {
|
||||
fmt.Println(file.Name())
|
||||
path := dir + file.Name() + "/"
|
||||
fmt.Println(path)
|
||||
|
||||
entries, err := os.ReadDir(path)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
id := -1
|
||||
for _, entry := range entries {
|
||||
fileInfo, err := entry.Info()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
id := -1
|
||||
for _, entry := range entries {
|
||||
fileInfo, err := entry.Info()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
id = getIdFromFile(fileInfo)
|
||||
if id != -1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if id == -1 {
|
||||
addNewGame(file.Name(), path)
|
||||
} else {
|
||||
checkIfChanged(id, file.Name(), path)
|
||||
checkSongs(path, id)
|
||||
id = getIdFromFile(fileInfo)
|
||||
if id != -1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if id == -1 {
|
||||
addNewGame(file.Name(), path)
|
||||
} else {
|
||||
checkIfChanged(id, file.Name(), path)
|
||||
checkSongs(path, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -160,7 +195,7 @@ func checkBrokenSongs() {
|
||||
}
|
||||
|
||||
func isSong(entry fs.FileInfo) bool {
|
||||
return !entry.IsDir() && strings.HasSuffix(entry.Name(), ".mp3") || strings.HasSuffix(entry.Name(), ".wav")
|
||||
return !entry.IsDir() && strings.HasSuffix(entry.Name(), ".mp3")
|
||||
}
|
||||
|
||||
func isCoverImage(entry fs.FileInfo) bool {
|
||||
|
||||
Reference in New Issue
Block a user