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
This commit is contained in:
@@ -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")
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.",
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user