Added concurrent sync, added search page. Other small changes
This commit is contained in:
@@ -19,6 +19,12 @@ func (s *Sync) SyncGames(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusOK, "Games are synced")
|
||||
}
|
||||
|
||||
func (s *Sync) SyncGamesQuick(ctx *gin.Context) {
|
||||
server.SyncGamesQuick()
|
||||
server.Reset()
|
||||
ctx.JSON(http.StatusOK, "Games are synced")
|
||||
}
|
||||
|
||||
func (s *Sync) ResetGames(ctx *gin.Context) {
|
||||
server.ResetDB()
|
||||
ctx.JSON(http.StatusOK, "Games and songs are deleted from the database")
|
||||
|
||||
@@ -3,14 +3,15 @@ package conf
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"music-server/pkg/api"
|
||||
"music-server/pkg/db"
|
||||
"music-server/pkg/helpers"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SetupDb() {
|
||||
@@ -50,7 +51,7 @@ func CloseDb() {
|
||||
defer db.CloseDb()
|
||||
}
|
||||
|
||||
func SetupRestServer(swagger embed.FS) {
|
||||
func SetupRestServer(swagger embed.FS, search embed.FS) {
|
||||
|
||||
router := gin.Default()
|
||||
router.Use(helpers.SetCorsAndNoCacheHeaders())
|
||||
@@ -59,6 +60,7 @@ func SetupRestServer(swagger embed.FS) {
|
||||
syncGroup := router.Group("/sync")
|
||||
{
|
||||
syncGroup.GET("", sync.SyncGames)
|
||||
syncGroup.GET("/quick", sync.SyncGamesQuick)
|
||||
syncGroup.GET("/reset", sync.ResetGames)
|
||||
}
|
||||
|
||||
@@ -87,8 +89,10 @@ func SetupRestServer(swagger embed.FS) {
|
||||
router.GET("/version", index.GetVersion)
|
||||
router.GET("/test", index.GetDBTest)
|
||||
router.StaticFS("/swagger", helpers.EmbedFolder(swagger, "swagger", false))
|
||||
router.StaticFS("/search", helpers.EmbedFolder(search, "search", true))
|
||||
router.Use(static.Serve("/", static.LocalFile("/frontend", true)))
|
||||
router.Use(static.Serve("/new", static.LocalFile("/newFrontend", true)))
|
||||
//router.Use(static.Serve("/search", static.LocalFile("/search", true)))
|
||||
|
||||
port := os.Getenv("PORT")
|
||||
if port == "" {
|
||||
|
||||
@@ -3,11 +3,13 @@ package db
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"os"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
var pool *pgx.Conn
|
||||
var dbpool *pgxpool.Pool
|
||||
var ctx = context.Background()
|
||||
|
||||
func InitDB(host string, port int, user string, password string, dbname string) {
|
||||
|
||||
@@ -18,14 +20,14 @@ func InitDB(host string, port int, user string, password string, dbname string)
|
||||
fmt.Println(psqlInfo)
|
||||
|
||||
var err error
|
||||
pool, err = pgx.Connect(context.Background(), psqlInfo)
|
||||
dbpool, err = pgxpool.New(ctx, psqlInfo)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Unable to connect to database: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var success string
|
||||
err = pool.QueryRow(context.Background(), "select 'Successfully connected!'").Scan(&success)
|
||||
err = dbpool.QueryRow(ctx, "select 'Successfully connected!'").Scan(&success)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
os.Exit(1)
|
||||
@@ -35,14 +37,11 @@ func InitDB(host string, port int, user string, password string, dbname string)
|
||||
}
|
||||
|
||||
func CloseDb() {
|
||||
err := pool.Close(context.Background())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
dbpool.Close()
|
||||
}
|
||||
|
||||
func Testf() {
|
||||
rows, dbErr := pool.Query(context.Background(), "select game_name from game")
|
||||
rows, dbErr := dbpool.Query(ctx, "select game_name from game")
|
||||
if dbErr != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", dbErr)
|
||||
os.Exit(1)
|
||||
@@ -58,7 +57,7 @@ func Testf() {
|
||||
}
|
||||
|
||||
func resetGameIdSeq() {
|
||||
_, err := pool.Query(context.Background(), "SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);")
|
||||
_, err := dbpool.Query(ctx, "SELECT setval('game_id_seq', (SELECT MAX(id) FROM game)+1);")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"music-server/pkg/models"
|
||||
@@ -13,10 +12,12 @@ import (
|
||||
|
||||
func GetGameName(gameId int) string {
|
||||
var gameName = ""
|
||||
err := pool.QueryRow(context.Background(),
|
||||
err := dbpool.QueryRow(ctx,
|
||||
"SELECT game_name FROM game WHERE id = $1", gameId).Scan(&gameName)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return gameName
|
||||
@@ -27,11 +28,13 @@ func GetGameById(gameId int) (models.GameData, error) {
|
||||
var numberOfSongs pgtype.Int4
|
||||
var gameName, path string
|
||||
var added, deleted, lastChanged, lastPlayed pgtype.Timestamp
|
||||
err := pool.QueryRow(context.Background(),
|
||||
err := dbpool.QueryRow(ctx,
|
||||
"SELECT id, game_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs "+
|
||||
"FROM game WHERE id = $1 AND deleted IS NULL ", gameId).Scan(&id, &gameName, &added, &deleted, &lastChanged, &path, ×Played, &lastPlayed, &numberOfSongs)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return models.GameData{}, errors.New("Game not found")
|
||||
}
|
||||
return models.GameData{
|
||||
@@ -48,7 +51,7 @@ func GetGameById(gameId int) (models.GameData, error) {
|
||||
}
|
||||
|
||||
func SetGameDeletionDate() {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
_, err := dbpool.Exec(ctx,
|
||||
"UPDATE game SET deleted=$1 WHERE deleted IS NULL", time.Now())
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
@@ -56,14 +59,14 @@ func SetGameDeletionDate() {
|
||||
}
|
||||
|
||||
func ClearGames() {
|
||||
_, err := pool.Exec(context.Background(), "DELETE FROM game")
|
||||
_, err := dbpool.Exec(ctx, "DELETE FROM game")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateGameName(id int, name string, path string) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
_, err := dbpool.Exec(ctx,
|
||||
"UPDATE game SET game_name=$1, path=$2, last_changed=$3 WHERE id=$4",
|
||||
name, path, time.Now(), id)
|
||||
if err != nil {
|
||||
@@ -72,7 +75,7 @@ func UpdateGameName(id int, name string, path string) {
|
||||
}
|
||||
|
||||
func RemoveDeletionDate(id int) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
_, err := dbpool.Exec(ctx,
|
||||
"UPDATE game SET deleted=null WHERE id=$1", id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
@@ -81,10 +84,12 @@ func RemoveDeletionDate(id int) {
|
||||
|
||||
func GetIdByGameName(name string) int {
|
||||
var gameId = -1
|
||||
err := pool.QueryRow(context.Background(),
|
||||
err := dbpool.QueryRow(ctx,
|
||||
"SELECT id FROM game WHERE game_name = $1", name).Scan(&gameId)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
return gameId
|
||||
@@ -92,17 +97,21 @@ func GetIdByGameName(name string) int {
|
||||
|
||||
func InsertGame(name string, path string) int {
|
||||
gameId := -1
|
||||
err := pool.QueryRow(context.Background(),
|
||||
err := dbpool.QueryRow(ctx,
|
||||
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
|
||||
name, path, time.Now()).Scan(&gameId)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
resetGameIdSeq()
|
||||
err2 := pool.QueryRow(context.Background(),
|
||||
err2 := dbpool.QueryRow(ctx,
|
||||
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
|
||||
name, path, time.Now()).Scan(&gameId)
|
||||
if err2 != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err2.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
}
|
||||
@@ -110,7 +119,7 @@ func InsertGame(name string, path string) int {
|
||||
}
|
||||
|
||||
func InsertGameWithExistingId(id int, name string, path string) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
_, err := dbpool.Exec(ctx,
|
||||
"INSERT INTO game(id, game_name, path, added) VALUES ($1, $2, $3, $4)",
|
||||
id, name, path, time.Now())
|
||||
if err != nil {
|
||||
@@ -119,12 +128,14 @@ func InsertGameWithExistingId(id int, name string, path string) {
|
||||
}
|
||||
|
||||
func FindAllGames() []models.GameData {
|
||||
rows, err := pool.Query(context.Background(),
|
||||
rows, err := dbpool.Query(ctx,
|
||||
"SELECT id, game_name, added, deleted, last_changed, path, times_played, last_played, number_of_songs "+
|
||||
"FROM game WHERE deleted IS NULL "+
|
||||
"ORDER BY game_name")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
var gameList []models.GameData
|
||||
for rows.Next() {
|
||||
@@ -134,7 +145,9 @@ func FindAllGames() []models.GameData {
|
||||
var added, deleted, lastChanged, lastPlayed pgtype.Timestamp
|
||||
err := rows.Scan(&id, &gameName, &added, &deleted, &lastChanged, &path, ×Played, &lastPlayed, &numberOfSongs)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
gameList = append(gameList, models.GameData{
|
||||
Id: id,
|
||||
@@ -152,7 +165,7 @@ func FindAllGames() []models.GameData {
|
||||
}
|
||||
|
||||
func AddGamePlayed(id int) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
_, err := dbpool.Exec(ctx,
|
||||
"UPDATE game SET times_played=times_played+1, last_played=now() WHERE id=$1", id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
|
||||
@@ -1,21 +1,23 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"music-server/pkg/models"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var compareError = errors.New("no rows in result set")
|
||||
|
||||
func ClearSongs(gameId int) {
|
||||
if gameId == -1 {
|
||||
_, err := pool.Exec(context.Background(), "DELETE FROM song")
|
||||
_, err := dbpool.Exec(ctx, "DELETE FROM song")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
} else {
|
||||
_, err := pool.Exec(context.Background(), "DELETE FROM song where game_id=$1", gameId)
|
||||
_, err := dbpool.Exec(ctx, "DELETE FROM song where game_id=$1", gameId)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
@@ -23,7 +25,7 @@ func ClearSongs(gameId int) {
|
||||
}
|
||||
|
||||
func AddSong(song models.SongData) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
_, err := dbpool.Exec(ctx,
|
||||
"INSERT INTO song(game_id, song_name, path, file_name) VALUES ($1, $2, $3, $4)",
|
||||
song.GameId, song.SongName, song.Path, song.FileName)
|
||||
if err != nil {
|
||||
@@ -33,17 +35,18 @@ func AddSong(song models.SongData) {
|
||||
|
||||
func CheckSong(songPath string) bool {
|
||||
var path string
|
||||
err := pool.QueryRow(context.Background(),
|
||||
err := dbpool.QueryRow(ctx,
|
||||
"SELECT path FROM song WHERE path = $1", songPath).Scan(&path)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
println("CheckSong path", path)
|
||||
return path != ""
|
||||
}
|
||||
|
||||
func UpdateSong(songName string, fileName string, path string) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
_, err := dbpool.Exec(ctx,
|
||||
"UPDATE song SET song_name=$1, file_name=$2 WHERE path = $3",
|
||||
songName, fileName, path)
|
||||
if err != nil {
|
||||
@@ -52,10 +55,12 @@ func UpdateSong(songName string, fileName string, path string) {
|
||||
}
|
||||
|
||||
func FindSongsFromGame(id int) []models.SongData {
|
||||
rows, err := pool.Query(context.Background(),
|
||||
rows, err := dbpool.Query(ctx,
|
||||
"SELECT song_name, path, file_name, times_played FROM song WHERE game_id = $1", id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -68,7 +73,9 @@ func FindSongsFromGame(id int) []models.SongData {
|
||||
|
||||
err := rows.Scan(&songName, &path, &fileName, ×Played)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
songDataList = append(songDataList, models.SongData{
|
||||
@@ -83,7 +90,7 @@ func FindSongsFromGame(id int) []models.SongData {
|
||||
}
|
||||
|
||||
func AddSongPlayed(id int, name string) {
|
||||
_, err := pool.Exec(context.Background(),
|
||||
_, err := dbpool.Exec(ctx,
|
||||
"UPDATE song SET times_played=times_played+1 WHERE game_id=$1 AND song_name=$2", id, name)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
@@ -91,10 +98,12 @@ func AddSongPlayed(id int, name string) {
|
||||
}
|
||||
|
||||
func FetchAllSongs() []models.SongData {
|
||||
rows, err := pool.Query(context.Background(),
|
||||
rows, err := dbpool.Query(ctx,
|
||||
"SELECT song_name, path FROM song")
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -105,7 +114,9 @@ func FetchAllSongs() []models.SongData {
|
||||
|
||||
err := rows.Scan(&songName, &path)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
songDataList = append(songDataList, models.SongData{
|
||||
@@ -117,7 +128,7 @@ func FetchAllSongs() []models.SongData {
|
||||
}
|
||||
|
||||
func RemoveBrokenSong(song models.SongData) {
|
||||
_, err := pool.Exec(context.Background(), "DELETE FROM song where path=$1", song.Path)
|
||||
_, err := dbpool.Exec(ctx, "DELETE FROM song where path=$1", song.Path)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
@@ -130,7 +141,7 @@ func RemoveBrokenSongs(songs []models.SongData) {
|
||||
}
|
||||
joined = strings.TrimSuffix(joined, ",")
|
||||
|
||||
_, err := pool.Exec(context.Background(), "DELETE FROM song where path in ($1)", joined)
|
||||
_, err := dbpool.Exec(ctx, "DELETE FROM song where path in ($1)", joined)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"music-server/pkg/models"
|
||||
"os"
|
||||
@@ -9,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
func InsertSongInList(song models.SongListData) {
|
||||
err := pool.QueryRow(context.Background(),
|
||||
_, err := dbpool.Exec(ctx,
|
||||
`INSERT INTO song_list (match_date, match_id, song_no, game_name, song_name) VALUES ($1, $2, $3, $4, $5)`,
|
||||
song.MatchDate, song.MatchId, song.SongNo, song.GameName, song.SongName)
|
||||
if err != nil {
|
||||
@@ -18,12 +17,14 @@ func InsertSongInList(song models.SongListData) {
|
||||
}
|
||||
|
||||
func GetSongList(matchId int) []models.SongListData {
|
||||
rows, err := pool.Query(context.Background(),
|
||||
rows, err := dbpool.Query(ctx,
|
||||
"SELECT match_date, match_id, song_no, game_name, song_name "+
|
||||
"FROM song_list WHERE match_date = $1"+
|
||||
"ORDER BY song_no DESC", matchId)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
var songList []models.SongListData
|
||||
for rows.Next() {
|
||||
|
||||
@@ -3,14 +3,15 @@ package helpers
|
||||
import (
|
||||
"embed"
|
||||
"fmt"
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/gin-contrib/static"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func SetCorsAndNoCacheHeaders() gin.HandlerFunc {
|
||||
|
||||
@@ -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