From 7943636201ab38603dba900c2a95e10e4989d349 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sat, 15 Jan 2022 01:19:07 +0100 Subject: [PATCH] Fixed some bugs Images should not be included in the database Removes songs where the path doesn't work Started working on adding cover images Started adding vue directly in the application --- .idea/MusicServer.iml | 4 +++ .idea/dataSources.xml | 4 +-- cmd/frontend/dist/index.html | 10 ++++++++ docs/swagger.yaml | 4 +-- pkg/conf/conf.go | 1 + pkg/db/song.go | 47 ++++++++++++++++++++++++++++++++++++ pkg/server/index.go | 8 ++++-- pkg/server/music.go | 3 ++- pkg/server/sync.go | 38 ++++++++++++++++++++++++++--- 9 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 cmd/frontend/dist/index.html diff --git a/.idea/MusicServer.iml b/.idea/MusicServer.iml index 7ee078d..eeb7bce 100644 --- a/.idea/MusicServer.iml +++ b/.idea/MusicServer.iml @@ -1,4 +1,8 @@ + + + + \ No newline at end of file diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml index 9a5fffc..e7ae04f 100644 --- a/.idea/dataSources.xml +++ b/.idea/dataSources.xml @@ -1,11 +1,11 @@ - + postgresql true org.postgresql.Driver - jdbc:postgresql://localhost:5432/music_dev_local + jdbc:postgresql://192.168.86.181:9432/music_prod \ No newline at end of file diff --git a/cmd/frontend/dist/index.html b/cmd/frontend/dist/index.html new file mode 100644 index 0000000..266e188 --- /dev/null +++ b/cmd/frontend/dist/index.html @@ -0,0 +1,10 @@ + + + + + Title + + +

Här kan en webbsida vara

+ + \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 57aa742..8bdd07c 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,8 +1,8 @@ openapi: 3.0.3 info: - version: "2.2.0" + version: "2.3.0" title: "Music Server" - description: "Changed the structure of the whole application, should be no changes to functionality." + description: "Images should not be included in the database, removes songs where the path doesn't work." contact: email: "zarnor91@gmail.com" servers: diff --git a/pkg/conf/conf.go b/pkg/conf/conf.go index ebaf095..1fc62ee 100644 --- a/pkg/conf/conf.go +++ b/pkg/conf/conf.go @@ -56,6 +56,7 @@ func SetupRestServer() { r.HandleFunc("/music/{func}", api.MusicHandler) r.HandleFunc("/music/{func}/{func2}", api.MusicHandler) r.HandleFunc("/{func}", api.IndexHandler) + r.Handle("/", http.FileServer(http.FS(os.DirFS("frontend/dist")))) http.Handle("/", r) port := os.Getenv("PORT") diff --git a/pkg/db/song.go b/pkg/db/song.go index 4e847e1..3524949 100644 --- a/pkg/db/song.go +++ b/pkg/db/song.go @@ -5,6 +5,7 @@ import ( "fmt" "music-server/pkg/models" "os" + "strings" ) func ClearSongs(gameId int) { @@ -66,3 +67,49 @@ func AddSongPlayed(id int, name string) { _, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err) } } + +func FetchAllSongs() []models.SongData { + rows, err := pool.Query(context.Background(), + "SELECT song_name, path FROM song") + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err) + return nil + } + + var songDataList []models.SongData + for rows.Next() { + var songName string + var path string + + err := rows.Scan(&songName, &path) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err) + } + + songDataList = append(songDataList, models.SongData{ + SongName: songName, + Path: path, + }) + } + return songDataList +} + +func RemoveBrokenSong(song models.SongData) { + _, err := pool.Exec(context.Background(), "DELETE FROM song where path=$1", song.Path) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err) + } +} + +func RemoveBrokenSongs(songs []models.SongData) { + joined := "" + for _, song := range songs { + joined += "'" + song.Path + "'," + } + joined = strings.TrimSuffix(joined, ",") + + _, err := pool.Exec(context.Background(), "DELETE FROM song where path in ($1)", joined) + if err != nil { + _, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err) + } +} diff --git a/pkg/server/index.go b/pkg/server/index.go index 05ddba2..442abf7 100644 --- a/pkg/server/index.go +++ b/pkg/server/index.go @@ -8,9 +8,13 @@ import ( func GetVersionHistory() models.VersionData { db.Testf() - data := models.VersionData{Version: "2.2.0", - Changelog: "Changed the structure of the whole application, should be no changes to functionality.", + data := models.VersionData{Version: "2.3.0", + Changelog: "Images should not be included in the database, removes songs where the path doesn't work.", History: []models.VersionData{ + { + Version: "2.2.0", + Changelog: "Changed the structure of the whole application, should be no changes to functionality.", + }, { Version: "2.1.4", Changelog: "Game list should now be sorted, a new endpoint with the game list in random order have been added.", diff --git a/pkg/server/music.go b/pkg/server/music.go index ee4c04d..37fa48f 100644 --- a/pkg/server/music.go +++ b/pkg/server/music.go @@ -198,7 +198,8 @@ func getSongFromList(games []models.GameData) models.SongData { openFile, err := os.Open(song.Path) if err != nil { //File not found - log.Fatal("Song not found, maybe delete song and/or game" + song.SongName + " songPath: " + song.Path) + db.RemoveBrokenSong(song) + log.Fatal("Song not found, song '" + song.SongName + "' deleted from game '" + game.GameName + "' songPath: " + song.Path) } else { songFound = true } diff --git a/pkg/server/sync.go b/pkg/server/sync.go index 8f111d9..7eb629d 100644 --- a/pkg/server/sync.go +++ b/pkg/server/sync.go @@ -2,6 +2,7 @@ package server import ( "fmt" + "io/fs" "io/ioutil" "log" "music-server/pkg/db" @@ -24,6 +25,7 @@ func SyncGames() { foldersToSkip := []string{".sync"} fmt.Println(foldersToSkip) db.SetGameDeletionDate() + checkBrokenSongs() files, err := ioutil.ReadDir(dir) if err != nil { @@ -116,9 +118,11 @@ func checkSongs(gameDir string, gameId int) { } for _, entry := range files { path := gameDir + entry.Name() - songName := entry.Name() - if !entry.IsDir() && !strings.HasSuffix(songName, ".id") { - songs = append(songs, models.SongData{GameId: gameId, SongName: songName, Path: path}) + fileName := entry.Name() + if isSong(entry) { + songs = append(songs, models.SongData{GameId: gameId, SongName: fileName, Path: path}) + } else if isCoverImage(entry) { + //TODO: Later add cover art image here in db } } if len(songs) != len(findSongsFromGame) { @@ -127,6 +131,34 @@ func checkSongs(gameDir string, gameId int) { db.AddSong(song) } } + //TODO: Add number of songs here +} + +func checkBrokenSongs() { + allSongs := db.FetchAllSongs() + var brokenSongs []models.SongData + for _, song := range allSongs { + //Check if file exists and open + openFile, err := os.Open(song.Path) + if err != nil { + //File not found + brokenSongs = append(brokenSongs, song) + } + err = openFile.Close() + if err != nil { + log.Fatal(err) + } + } + db.RemoveBrokenSongs(brokenSongs) +} + +func isSong(entry fs.FileInfo) bool { + return !entry.IsDir() && strings.HasSuffix(entry.Name(), ".mp3") || strings.HasSuffix(entry.Name(), ".wav") +} + +func isCoverImage(entry fs.FileInfo) bool { + return !entry.IsDir() && strings.Contains(entry.Name(), "cover") && + (strings.HasSuffix(entry.Name(), ".jpg") || strings.HasSuffix(entry.Name(), ".png")) } func contains(s []string, searchTerm string) bool {