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

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 {