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:
2022-01-15 01:19:07 +01:00
parent 0e5c792aec
commit 8172778ffe
11 changed files with 109 additions and 10 deletions

4
.idea/MusicServer.iml generated
View File

@@ -1,4 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="Go" enabled="true" />
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

4
.idea/dataSources.xml generated
View File

@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="music_dev_local@localhost" uuid="e63caf71-2b1a-400d-9cc4-4b87b1f9b807">
<data-source source="LOCAL" name="music_prod@192.168.86.181" uuid="e63caf71-2b1a-400d-9cc4-4b87b1f9b807">
<driver-ref>postgresql</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.postgresql.Driver</jdbc-driver>
<jdbc-url>jdbc:postgresql://localhost:5432/music_dev_local</jdbc-url>
<jdbc-url>jdbc:postgresql://192.168.86.181:9432/music_prod</jdbc-url>
</data-source>
</component>
</project>

10
cmd/frontend/dist/index.html vendored Normal file
View File

@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>Här kan en webbsida vara</h1>
</body>
</html>

View File

@@ -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:

View File

@@ -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")

View File

@@ -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)
}
}

View File

@@ -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.",

View File

@@ -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
}

View File

@@ -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 {

BIN
songs/01. Opening.mp3 Normal file

Binary file not shown.

BIN
songs/01. Title.mp3 Normal file

Binary file not shown.