#1 - Created request to check newest version of the app
All checks were successful
Build / build (push) Successful in 2m35s
All checks were successful
Build / build (push) Successful in 2m35s
#2 - Added request to download the newest version of the app #3 - Added request to check progress during sync #4 - Now blocking all request while sync is in progress #5 - Implemented ants for thread pooling #6 - Changed the sync request to now only start the sync
This commit is contained in:
@@ -7,8 +7,7 @@ import (
|
||||
)
|
||||
|
||||
func GetCharacters() []string {
|
||||
musicPath := os.Getenv("MUSIC_PATH")
|
||||
charactersPath := musicPath + "characters/"
|
||||
charactersPath := os.Getenv("CHARACTERS_PATH")
|
||||
files, err := os.ReadDir(charactersPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -32,4 +31,3 @@ func GetCharacter(character string) string {
|
||||
func isImage(entry os.DirEntry) bool {
|
||||
return !entry.IsDir() && (strings.HasSuffix(entry.Name(), ".jpg") || strings.HasSuffix(entry.Name(), ".png"))
|
||||
}
|
||||
|
||||
|
||||
106
internal/backend/download.go
Normal file
106
internal/backend/download.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package backend
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type giteaResponse struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Assets []assetResponse `json:"assets"`
|
||||
}
|
||||
|
||||
type assetResponse struct {
|
||||
Id int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
DownloadUrl string `json:"browser_download_url"`
|
||||
}
|
||||
|
||||
func CheckLatest() string {
|
||||
resp, err := http.Get("https://gitea.sanplex.tech/api/v1/repos/sansan/MusicPlayer/releases/latest")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
//Create a variable of the same type as our model
|
||||
var cResp giteaResponse
|
||||
//Decode the data
|
||||
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
|
||||
fmt.Println(err)
|
||||
log.Fatal("ooopsss! an error occurred, please try again")
|
||||
}
|
||||
log.Printf("Id: %v, Name: %v", cResp.Id, cResp.Name)
|
||||
return cResp.Name
|
||||
}
|
||||
|
||||
func ListAssetsOfLatest() []string {
|
||||
resp, err := http.Get("https://gitea.sanplex.tech/api/v1/repos/sansan/MusicPlayer/releases/latest")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
//Create a variable of the same type as our model
|
||||
var cResp giteaResponse
|
||||
//Decode the data
|
||||
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
|
||||
fmt.Println(err)
|
||||
log.Fatal("ooopsss! an error occurred, please try again")
|
||||
}
|
||||
log.Printf("Id: %v, Name: %v", cResp.Id, cResp.Name)
|
||||
var assets []string
|
||||
for _, asset := range cResp.Assets {
|
||||
log.Printf("Id: %v, Name: %v, Asset: %v", cResp.Id, cResp.Name, asset.Name)
|
||||
assets = append(assets, asset.Name)
|
||||
}
|
||||
return assets
|
||||
}
|
||||
|
||||
func DownloadLatestWindows() string {
|
||||
resp, err := http.Get("https://gitea.sanplex.tech/api/v1/repos/sansan/MusicPlayer/releases/latest")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
//Create a variable of the same type as our model
|
||||
var cResp giteaResponse
|
||||
//Decode the data
|
||||
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
|
||||
fmt.Println(err)
|
||||
log.Fatal("ooopsss! an error occurred, please try again")
|
||||
}
|
||||
log.Printf("Id: %v, Name: %v", cResp.Id, cResp.Name)
|
||||
for _, asset := range cResp.Assets {
|
||||
log.Printf("Id: %v, Name: %v, Asset: %v", cResp.Id, cResp.Name, asset.Name)
|
||||
if strings.HasSuffix(asset.Name, ".exe") {
|
||||
return asset.DownloadUrl
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func DownloadLatestLinux() string {
|
||||
resp, err := http.Get("https://gitea.sanplex.tech/api/v1/repos/sansan/MusicPlayer/releases/latest")
|
||||
if err != nil {
|
||||
log.Fatalln(err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
//Create a variable of the same type as our model
|
||||
var cResp giteaResponse
|
||||
//Decode the data
|
||||
if err := json.NewDecoder(resp.Body).Decode(&cResp); err != nil {
|
||||
fmt.Println(err)
|
||||
log.Fatal("ooopsss! an error occurred, please try again")
|
||||
}
|
||||
log.Printf("Id: %v, Name: %v", cResp.Id, cResp.Name)
|
||||
for _, asset := range cResp.Assets {
|
||||
log.Printf("Id: %v, Name: %v, Asset: %v", cResp.Id, cResp.Name, asset.Name)
|
||||
if strings.HasSuffix(asset.Name, ".x86_64") {
|
||||
return asset.DownloadUrl
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -15,9 +15,26 @@ type VersionData struct {
|
||||
}
|
||||
|
||||
func GetVersionHistory() VersionData {
|
||||
data := VersionData{Version: "3.2",
|
||||
Changelog: "Upgraded Go version and the version of all dependencies. Fixed som more bugs.",
|
||||
data := VersionData{Version: "4.5.0",
|
||||
Changelog: "#1 - Created request to check newest version of the app\n" +
|
||||
"#2 - Added request to download the newest version of the app\n" +
|
||||
"#3 - Added request to check progress during sync\n" +
|
||||
"#4 - Now blocking all request while sync is in progress\n" +
|
||||
"#5 - Implemented ants for thread pooling\n" +
|
||||
"#6 - Changed the sync request to now only start the sync",
|
||||
History: []VersionData{
|
||||
{
|
||||
Version: "4.0.0",
|
||||
Changelog: "Changed framework from gin to Echo\n" +
|
||||
"Reorganized the code\n" +
|
||||
"Implemented sqlc\n" +
|
||||
"Added support to send character images from the server\n" +
|
||||
"Added function to create a new database of no one exists",
|
||||
},
|
||||
{
|
||||
Version: "3.2",
|
||||
Changelog: "Upgraded Go version and the version of all dependencies. Fixed som more bugs.",
|
||||
},
|
||||
{
|
||||
Version: "3.1",
|
||||
Changelog: "Fixed some bugs with songs not found made the application crash. Now checking if song exists and if not, remove song from DB and find another one. Frontend is now decoupled from the backend.",
|
||||
|
||||
@@ -21,13 +21,10 @@ type SongInfo struct {
|
||||
|
||||
var currentSong = -1
|
||||
|
||||
// var games []models.GameData
|
||||
var gamesNew []repository.Game
|
||||
|
||||
// var songQue []models.SongData
|
||||
var songQueNew []repository.Song
|
||||
|
||||
// var lastFetched models.SongData
|
||||
var lastFetchedNew repository.Song
|
||||
var repo *repository.Queries
|
||||
|
||||
@@ -60,7 +57,6 @@ func Reset() {
|
||||
currentSong = -1
|
||||
initRepo()
|
||||
gamesNew, _ = repo.FindAllGames(db.Ctx)
|
||||
//games = db.FindAllGames()
|
||||
}
|
||||
|
||||
func AddLatestToQue() {
|
||||
@@ -80,8 +76,6 @@ func AddLatestPlayed() {
|
||||
initRepo()
|
||||
repo.AddGamePlayed(db.Ctx, currentSongData.GameID)
|
||||
repo.AddSongPlayed(db.Ctx, repository.AddSongPlayedParams{GameID: currentSongData.GameID, SongName: currentSongData.SongName})
|
||||
//db.AddGamePlayed(currentSongData.GameId)
|
||||
//db.AddSongPlayed(currentSongData.GameId, currentSongData.SongName)
|
||||
}
|
||||
|
||||
func SetPlayed(songNumber int) {
|
||||
@@ -92,14 +86,9 @@ func SetPlayed(songNumber int) {
|
||||
initRepo()
|
||||
repo.AddGamePlayed(db.Ctx, songData.GameID)
|
||||
repo.AddSongPlayed(db.Ctx, repository.AddSongPlayedParams{GameID: songData.GameID, SongName: songData.SongName})
|
||||
//db.AddGamePlayed(songData.GameId)
|
||||
//db.AddSongPlayed(songData.GameId, songData.SongName)
|
||||
}
|
||||
|
||||
func GetRandomSong() string {
|
||||
/*if len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}*/
|
||||
getAllGames()
|
||||
if len(gamesNew) == 0 {
|
||||
return ""
|
||||
@@ -111,12 +100,8 @@ func GetRandomSong() string {
|
||||
}
|
||||
|
||||
func GetRandomSongLowChance() string {
|
||||
/*if len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}*/
|
||||
getAllGames()
|
||||
|
||||
//var listOfGames []models.GameData
|
||||
var listOfGames []repository.Game
|
||||
|
||||
var averagePlayed = getAveragePlayed()
|
||||
@@ -139,15 +124,10 @@ func GetRandomSongLowChance() string {
|
||||
}
|
||||
|
||||
func GetRandomSongClassic() string {
|
||||
/*if games == nil || len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}*/
|
||||
|
||||
getAllGames()
|
||||
|
||||
var listOfAllSongs []repository.Song
|
||||
for _, game := range gamesNew {
|
||||
//listOfAllSongs = append(listOfAllSongs, db.FindSongsFromGame(game.Id)...)
|
||||
songList, _ := repo.FindSongsFromGame(db.Ctx, game.ID)
|
||||
listOfAllSongs = append(listOfAllSongs, songList...)
|
||||
}
|
||||
@@ -156,13 +136,10 @@ func GetRandomSongClassic() string {
|
||||
var song repository.Song
|
||||
for !songFound {
|
||||
song = listOfAllSongs[rand.Intn(len(listOfAllSongs))]
|
||||
//gameData, err := db.GetGameById(song.GameId)
|
||||
gameData, err := repo.GetGameById(db.Ctx, song.GameID)
|
||||
|
||||
if err != nil {
|
||||
//db.RemoveBrokenSong(song)
|
||||
repo.RemoveBrokenSong(db.Ctx, song.Path)
|
||||
//log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + gameData.GameName + "' FileName: " + song.FileName)
|
||||
log.Printf("Song not found, song '%s' deleted from game '%s' FileName: %v\n", song.SongName, gameData.GameName, song.FileName)
|
||||
continue
|
||||
}
|
||||
@@ -171,9 +148,7 @@ func GetRandomSongClassic() string {
|
||||
openFile, err := os.Open(song.Path)
|
||||
if err != nil || (song.FileName != nil && gameData.Path+*song.FileName != song.Path) {
|
||||
//File not found
|
||||
//db.RemoveBrokenSong(song)
|
||||
repo.RemoveBrokenSong(db.Ctx, song.Path)
|
||||
//log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + gameData.GameName + "' FileName: " + song.FileName)
|
||||
log.Printf("Song not found, song '%s' deleted from game '%s' FileName: %v\n", song.SongName, gameData.GameName, song.FileName)
|
||||
} else {
|
||||
songFound = true
|
||||
@@ -234,9 +209,6 @@ func GetSong(song string) string {
|
||||
}
|
||||
|
||||
func GetAllGames() []string {
|
||||
/*if games == nil || len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}*/
|
||||
getAllGames()
|
||||
|
||||
var jsonArray []string
|
||||
@@ -247,9 +219,6 @@ func GetAllGames() []string {
|
||||
}
|
||||
|
||||
func GetAllGamesRandom() []string {
|
||||
/*if games == nil || len(games) == 0 {
|
||||
games = db.FindAllGames()
|
||||
}*/
|
||||
getAllGames()
|
||||
|
||||
var jsonArray []string
|
||||
@@ -293,10 +262,7 @@ func getSongFromList(games []repository.Game) repository.Song {
|
||||
var song repository.Song
|
||||
for !songFound {
|
||||
game := getRandomGame(games)
|
||||
//log.Println("game = ", game)
|
||||
//songs := db.FindSongsFromGame(game.Id)
|
||||
songs, _ := repo.FindSongsFromGame(db.Ctx, game.ID)
|
||||
//log.Println("songs = ", songs)
|
||||
if len(songs) == 0 {
|
||||
continue
|
||||
}
|
||||
@@ -305,14 +271,9 @@ func getSongFromList(games []repository.Game) repository.Song {
|
||||
|
||||
//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 || (song.FileName != nil && game.Path+*song.FileName != song.Path) || (song.FileName != nil && strings.HasSuffix(*song.FileName, ".wav")) {
|
||||
//File not found
|
||||
//db.RemoveBrokenSong(song)
|
||||
repo.RemoveBrokenSong(db.Ctx, song.Path)
|
||||
//log.Println("Song not found, song '" + song.SongName + "' deleted from game '" + game.GameName + "' FileName: " + song.FileName)
|
||||
log.Printf("Song not found, song '%s' deleted from game '%s' FileName: %v\n", song.SongName, game.GameName, song.FileName)
|
||||
} else {
|
||||
songFound = true
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
@@ -21,6 +22,11 @@ import (
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
var Syncing = false
|
||||
var foldersSynced float32
|
||||
var numberOfFoldersToSync float32
|
||||
var totalTime time.Duration
|
||||
|
||||
var allGames []repository.Game
|
||||
var gamesBeforeSync []repository.Game
|
||||
var gamesAfterSync []repository.Game
|
||||
@@ -61,67 +67,18 @@ func (gs GameStatus) String() string {
|
||||
return statusName[gs]
|
||||
}
|
||||
|
||||
var syncWg sync.WaitGroup
|
||||
|
||||
func ResetDB() {
|
||||
//db.ClearSongs(-1)
|
||||
repo.ClearSongs(db.Ctx)
|
||||
//db.ClearGames()
|
||||
repo.ClearGames(db.Ctx)
|
||||
}
|
||||
|
||||
func SyncGamesNewFull() Response {
|
||||
return syncGamesNew(true)
|
||||
func SyncProgress() string {
|
||||
//log.Printf("Progress: %v%%\n", int((foldersSynced/numberOfFoldersToSync)*10))
|
||||
log.Printf("Progress: %v/%v %v%%\n", int(foldersSynced), int(numberOfFoldersToSync), int((foldersSynced/numberOfFoldersToSync)*100))
|
||||
return fmt.Sprintf("Progress: %v%%", int((foldersSynced/numberOfFoldersToSync)*100))
|
||||
}
|
||||
|
||||
func SyncGamesNewOnlyChanges() Response {
|
||||
return syncGamesNew(false)
|
||||
}
|
||||
|
||||
func syncGamesNew(full bool) Response {
|
||||
musicPath := os.Getenv("MUSIC_PATH")
|
||||
fmt.Printf("dir: %s\n", musicPath)
|
||||
initRepo()
|
||||
start := time.Now()
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
|
||||
var err error
|
||||
gamesAdded = nil
|
||||
gamesReAdded = nil
|
||||
gamesChangedTitle = nil
|
||||
gamesChangedContent = nil
|
||||
gamesRemoved = nil
|
||||
catchedErrors = nil
|
||||
brokenSongs = nil
|
||||
|
||||
gamesBeforeSync, err = repo.FindAllGames(db.Ctx)
|
||||
handleError("FindAllGames Before", err, "")
|
||||
fmt.Printf("Games Before: %d\n", len(gamesBeforeSync))
|
||||
|
||||
allGames, err = repo.GetAllGamesIncludingDeleted(db.Ctx)
|
||||
handleError("GetAllGamesIncludingDeleted", err, "")
|
||||
err = repo.SetGameDeletionDate(db.Ctx)
|
||||
handleError("SetGameDeletionDate", err, "")
|
||||
|
||||
directories, err := os.ReadDir(musicPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
syncWg.Add(len(directories))
|
||||
for _, dir := range directories {
|
||||
go func() {
|
||||
defer syncWg.Done()
|
||||
syncGameNew(dir, foldersToSkip, musicPath, full)
|
||||
}()
|
||||
}
|
||||
syncWg.Wait()
|
||||
checkBrokenSongsNew()
|
||||
|
||||
gamesAfterSync, err = repo.FindAllGames(db.Ctx)
|
||||
handleError("FindAllGames After", err, "")
|
||||
|
||||
func SyncResult() Response {
|
||||
fmt.Printf("\nGames Before: %d\n", len(gamesBeforeSync))
|
||||
fmt.Printf("Games After: %d\n", len(gamesAfterSync))
|
||||
|
||||
@@ -148,16 +105,16 @@ func syncGamesNew(full bool) Response {
|
||||
fmt.Printf("\n\n")
|
||||
var gamesRemovedTemp []string
|
||||
for _, beforeGame := range gamesBeforeSync {
|
||||
var found bool = false
|
||||
var found = false
|
||||
for _, afterGame := range gamesAfterSync {
|
||||
if beforeGame.GameName == afterGame.GameName {
|
||||
found = true
|
||||
//fmt.Printf("Game: %s, Found: %v break\n", beforeGame.GameName, found)
|
||||
fmt.Printf("Game: %s, Found: %v break\n", beforeGame.GameName, found)
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
//fmt.Printf("Game: %s, Found: %v\n", beforeGame.GameName, found)
|
||||
fmt.Printf("Game: %s, Found: %v\n", beforeGame.GameName, found)
|
||||
gamesRemovedTemp = append(gamesRemovedTemp, beforeGame.GameName)
|
||||
}
|
||||
}
|
||||
@@ -186,8 +143,6 @@ func syncGamesNew(full bool) Response {
|
||||
fmt.Printf("%s\n", error)
|
||||
}
|
||||
|
||||
finished := time.Now()
|
||||
totalTime := finished.Sub(start)
|
||||
out := time.Time{}.Add(totalTime)
|
||||
fmt.Printf("\nTotal time: %v\n", totalTime)
|
||||
fmt.Printf("Total time: %v\n", out.Format("15:04:05.00000"))
|
||||
@@ -202,16 +157,94 @@ func syncGamesNew(full bool) Response {
|
||||
}
|
||||
}
|
||||
|
||||
func SyncGamesNewFull() {
|
||||
syncGamesNew(true)
|
||||
Reset()
|
||||
}
|
||||
|
||||
func SyncGamesNewOnlyChanges() {
|
||||
syncGamesNew(false)
|
||||
Reset()
|
||||
}
|
||||
|
||||
func syncGamesNew(full bool) {
|
||||
Syncing = true
|
||||
|
||||
musicPath := os.Getenv("MUSIC_PATH")
|
||||
fmt.Printf("dir: %s\n", musicPath)
|
||||
if !strings.HasSuffix(musicPath, "/") {
|
||||
musicPath += "/"
|
||||
}
|
||||
|
||||
var syncWg sync.WaitGroup
|
||||
|
||||
initRepo()
|
||||
start := time.Now()
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
|
||||
var err error
|
||||
gamesAdded = nil
|
||||
gamesReAdded = nil
|
||||
gamesChangedTitle = nil
|
||||
gamesChangedContent = nil
|
||||
gamesRemoved = nil
|
||||
catchedErrors = nil
|
||||
brokenSongs = nil
|
||||
|
||||
gamesBeforeSync, err = repo.FindAllGames(db.Ctx)
|
||||
handleError("FindAllGames Before", err, "")
|
||||
fmt.Printf("Games Before: %d\n", len(gamesBeforeSync))
|
||||
|
||||
allGames, err = repo.GetAllGamesIncludingDeleted(db.Ctx)
|
||||
handleError("GetAllGamesIncludingDeleted", err, "")
|
||||
err = repo.SetGameDeletionDate(db.Ctx)
|
||||
handleError("SetGameDeletionDate", err, "")
|
||||
|
||||
directories, err := os.ReadDir(musicPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
pool, _ := ants.NewPool(50, ants.WithPreAlloc(true))
|
||||
defer pool.Release()
|
||||
|
||||
numberOfFoldersToSync = float32(len(directories))
|
||||
syncWg.Add(int(numberOfFoldersToSync))
|
||||
for _, dir := range directories {
|
||||
pool.Submit(func() {
|
||||
defer syncWg.Done()
|
||||
syncGameNew(dir, foldersToSkip, musicPath, full)
|
||||
})
|
||||
}
|
||||
syncWg.Wait()
|
||||
checkBrokenSongsNew()
|
||||
|
||||
gamesAfterSync, err = repo.FindAllGames(db.Ctx)
|
||||
handleError("FindAllGames After", err, "")
|
||||
|
||||
finished := time.Now()
|
||||
totalTime = finished.Sub(start)
|
||||
out := time.Time{}.Add(totalTime)
|
||||
fmt.Printf("\nTotal time: %v\n", totalTime)
|
||||
fmt.Printf("Total time: %v\n", out.Format("15:04:05.00000"))
|
||||
|
||||
Syncing = false
|
||||
}
|
||||
|
||||
func checkBrokenSongsNew() {
|
||||
allSongs, err := repo.FetchAllSongs(db.Ctx)
|
||||
handleError("FetchAllSongs", err, "")
|
||||
var brokenWg sync.WaitGroup
|
||||
poolBroken, _ := ants.NewPool(50, ants.WithPreAlloc(true))
|
||||
defer poolBroken.Release()
|
||||
|
||||
brokenWg.Add(len(allSongs))
|
||||
for _, song := range allSongs {
|
||||
go func() {
|
||||
poolBroken.Submit(func() {
|
||||
defer brokenWg.Done()
|
||||
checkBrokenSongNew(song)
|
||||
}()
|
||||
})
|
||||
}
|
||||
brokenWg.Wait()
|
||||
err = repo.RemoveBrokenSongs(db.Ctx, brokenSongs)
|
||||
@@ -343,6 +376,8 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
|
||||
err = repo.RemoveDeletionDate(db.Ctx, id)
|
||||
handleError("RemoveDeletionDate", err, "")
|
||||
}
|
||||
foldersSynced++
|
||||
log.Printf("Progress: %v/%v %v%%\n", int(foldersSynced), int(numberOfFoldersToSync), int((foldersSynced/numberOfFoldersToSync)*100))
|
||||
}
|
||||
|
||||
func insertGameNew(name string, path string, hash string) int32 {
|
||||
@@ -365,19 +400,26 @@ func insertGameNew(name string, path string, hash string) int32 {
|
||||
func newCheckSongs(entries []os.DirEntry, gameDir string, id int32) int32 {
|
||||
//hasher := md5.New()
|
||||
var numberOfSongs int32
|
||||
numberOfFiles := len(entries)
|
||||
|
||||
var songWg sync.WaitGroup
|
||||
songWg.Add(len(entries))
|
||||
poolSong, _ := ants.NewPool(numberOfFiles, ants.WithPreAlloc(true))
|
||||
defer poolSong.Release()
|
||||
|
||||
songWg.Add(numberOfFiles)
|
||||
for _, entry := range entries {
|
||||
go func() {
|
||||
poolSong.Submit(func() {
|
||||
defer songWg.Done()
|
||||
newCheckSong(entry, gameDir, id)
|
||||
}()
|
||||
if newCheckSong(entry, gameDir, id) {
|
||||
numberOfSongs++
|
||||
}
|
||||
})
|
||||
}
|
||||
songWg.Wait()
|
||||
return numberOfSongs
|
||||
}
|
||||
|
||||
func newCheckSong(entry os.DirEntry, gameDir string, id int32) {
|
||||
func newCheckSong(entry os.DirEntry, gameDir string, id int32) bool {
|
||||
fileInfo, err := entry.Info()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@@ -396,7 +438,7 @@ func newCheckSong(entry os.DirEntry, gameDir string, id int32) {
|
||||
handleError("GetSongWithHash", err, fmt.Sprintf("GameID: %d | Path: %s | SongName: %s | SongHash: %s\n", id, path, entry.Name(), songHash))
|
||||
if err == nil {
|
||||
if song.SongName == songName && song.Path == path {
|
||||
return
|
||||
return false
|
||||
}
|
||||
}
|
||||
fmt.Printf("Song Changed\n")
|
||||
@@ -432,9 +474,11 @@ func newCheckSong(entry os.DirEntry, gameDir string, id int32) {
|
||||
|
||||
}
|
||||
}
|
||||
return true
|
||||
} else if isCoverImage(fileInfo) {
|
||||
//TODO: Later add cover art image here in db
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func handleError(funcName string, err error, msg string) {
|
||||
|
||||
@@ -1,103 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"music-server/internal/db"
|
||||
"os"
|
||||
"time"
|
||||
)
|
||||
|
||||
type gameData struct {
|
||||
Id int
|
||||
GameName string
|
||||
Added time.Time
|
||||
Deleted time.Time
|
||||
LastChanged time.Time
|
||||
Path string
|
||||
TimesPlayed int
|
||||
LastPlayed time.Time
|
||||
NumberOfSongs int32
|
||||
}
|
||||
|
||||
func GetGameName(gameId int) string {
|
||||
var gameName = ""
|
||||
err := db.Dbpool.QueryRow(db.Ctx,
|
||||
"SELECT game_name FROM game WHERE id = $1", gameId).Scan(&gameName)
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
return gameName
|
||||
}
|
||||
|
||||
func SetGameDeletionDate() {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"UPDATE game SET deleted=$1 WHERE deleted IS NULL", time.Now())
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func UpdateGameName(id int, name string, path string) {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"UPDATE game SET game_name=$1, path=$2, last_changed=$3 WHERE id=$4",
|
||||
name, path, time.Now(), id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func RemoveDeletionDate(id int) {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"UPDATE game SET deleted=null WHERE id=$1", id)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func GetIdByGameName(name string) int {
|
||||
var gameId = -1
|
||||
err := db.Dbpool.QueryRow(db.Ctx,
|
||||
"SELECT id FROM game WHERE game_name = $1", name).Scan(&gameId)
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
return gameId
|
||||
}
|
||||
|
||||
func InsertGame(name string, path string) int {
|
||||
gameId := -1
|
||||
err := db.Dbpool.QueryRow(db.Ctx,
|
||||
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
|
||||
name, path, time.Now()).Scan(&gameId)
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
db.ResetGameIdSeq()
|
||||
err2 := db.Dbpool.QueryRow(db.Ctx,
|
||||
"INSERT INTO game(game_name, path, added) VALUES ($1, $2, $3) RETURNING id",
|
||||
name, path, time.Now()).Scan(&gameId)
|
||||
if err2 != nil {
|
||||
if compareError.Error() != err2.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return -1
|
||||
}
|
||||
}
|
||||
return gameId
|
||||
}
|
||||
|
||||
func InsertGameWithExistingId(id int, name string, path string) {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"INSERT INTO game(id, game_name, path, added) VALUES ($1, $2, $3, $4)",
|
||||
id, name, path, time.Now())
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
@@ -1,206 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
|
||||
func SyncGames() {
|
||||
start := time.Now()
|
||||
dir := os.Getenv("MUSIC_PATH")
|
||||
fmt.Printf("dir: %s\n", dir)
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
SetGameDeletionDate()
|
||||
checkBrokenSongs()
|
||||
|
||||
files, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
syncGame(file, foldersToSkip, dir)
|
||||
}
|
||||
finished := time.Now()
|
||||
totalTime := finished.Sub(start)
|
||||
out := time.Time{}.Add(totalTime)
|
||||
fmt.Printf("\nTotal time: %v\n", totalTime)
|
||||
fmt.Printf("Total time: %v\n", out.Format("15:04:05.00000"))
|
||||
}
|
||||
|
||||
func SyncGamesQuick() {
|
||||
start := time.Now()
|
||||
dir := os.Getenv("MUSIC_PATH")
|
||||
fmt.Printf("dir: %s\n", dir)
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
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()
|
||||
finished := time.Now()
|
||||
totalTime := finished.Sub(start)
|
||||
out := time.Time{}.Add(totalTime)
|
||||
fmt.Printf("\nTotal time: %v\n", totalTime)
|
||||
fmt.Printf("Total time: %v\n", out.Format("15:04:05.00000"))
|
||||
}
|
||||
|
||||
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 = getIdFromFile(fileInfo)
|
||||
if id != -1 {
|
||||
break
|
||||
}
|
||||
}
|
||||
if id == -1 {
|
||||
addNewGame(file.Name(), path)
|
||||
} else {
|
||||
checkIfChanged(id, file.Name(), path)
|
||||
checkSongs(path, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func getIdFromFile(file os.FileInfo) int {
|
||||
name := file.Name()
|
||||
if !file.IsDir() && strings.HasSuffix(name, ".id") {
|
||||
name = strings.Replace(name, ".id", "", 1)
|
||||
name = strings.Replace(name, ".", "", 1)
|
||||
i, _ := strconv.Atoi(name)
|
||||
return i
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func checkIfChanged(id int, name string, path string) {
|
||||
fmt.Printf("Id from file: %v\n", id)
|
||||
nameFromDb := GetGameName(id)
|
||||
fmt.Printf("Name from file: %v\n", name)
|
||||
fmt.Printf("Name from DB: %v\n", nameFromDb)
|
||||
if nameFromDb == "" {
|
||||
fmt.Println("Not in db")
|
||||
InsertGameWithExistingId(id, name, path)
|
||||
fmt.Println("Added to db")
|
||||
} else if name != nameFromDb {
|
||||
fmt.Println("Diff name")
|
||||
UpdateGameName(id, name, path)
|
||||
checkBrokenSongs()
|
||||
}
|
||||
RemoveDeletionDate(id)
|
||||
}
|
||||
|
||||
func addNewGame(name string, path string) {
|
||||
newId := GetIdByGameName(name)
|
||||
if newId != -1 {
|
||||
checkBrokenSongs()
|
||||
RemoveDeletionDate(newId)
|
||||
} else {
|
||||
newId = InsertGame(name, path)
|
||||
}
|
||||
|
||||
fmt.Printf("newId = %v", newId)
|
||||
fileName := path + "/." + strconv.Itoa(newId) + ".id"
|
||||
fmt.Printf("fileName = %v", fileName)
|
||||
|
||||
err := os.WriteFile(fileName, nil, 0644)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
checkSongs(path, newId)
|
||||
}
|
||||
|
||||
func checkSongs(gameDir string, gameId int) {
|
||||
files, err := os.ReadDir(gameDir)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
for _, file := range files {
|
||||
entry, err := file.Info()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
if isSong(entry) {
|
||||
path := gameDir + entry.Name()
|
||||
fileName := entry.Name()
|
||||
songName, _ := strings.CutSuffix(fileName, ".mp3")
|
||||
if CheckSong(path) {
|
||||
UpdateSong(songName, fileName, path)
|
||||
} else {
|
||||
AddSong(SongData{GameId: gameId, SongName: songName, Path: path, FileName: fileName})
|
||||
}
|
||||
} else if isCoverImage(entry) {
|
||||
//TODO: Later add cover art image here in db
|
||||
}
|
||||
}
|
||||
//TODO: Add number of songs here
|
||||
}
|
||||
|
||||
func checkBrokenSongs() {
|
||||
allSongs := FetchAllSongs()
|
||||
var brokenSongs []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)
|
||||
fmt.Printf("song broken: %v", song.Path)
|
||||
} else {
|
||||
err = openFile.Close()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
RemoveBrokenSongs(brokenSongs)
|
||||
}
|
||||
|
||||
func isSong(entry fs.FileInfo) bool {
|
||||
return !entry.IsDir() && strings.HasSuffix(entry.Name(), ".mp3")
|
||||
}
|
||||
|
||||
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 {
|
||||
i := sort.SearchStrings(s, searchTerm)
|
||||
return i < len(s) && s[i] == searchTerm
|
||||
}
|
||||
@@ -1,92 +0,0 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"music-server/internal/db"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type SongData struct {
|
||||
GameId int
|
||||
SongName string
|
||||
Path string
|
||||
TimesPlayed int
|
||||
FileName string
|
||||
}
|
||||
|
||||
var compareError = errors.New("no rows in result set")
|
||||
|
||||
func AddSong(song SongData) {
|
||||
_, err := db.Dbpool.Exec(db.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 {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func CheckSong(songPath string) bool {
|
||||
var path string
|
||||
err := db.Dbpool.QueryRow(db.Ctx,
|
||||
"SELECT path FROM song WHERE path = $1", songPath).Scan(&path)
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
return path != ""
|
||||
}
|
||||
|
||||
func UpdateSong(songName string, fileName string, path string) {
|
||||
_, err := db.Dbpool.Exec(db.Ctx,
|
||||
"UPDATE song SET song_name=$1, file_name=$2 WHERE path = $3",
|
||||
songName, fileName, path)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
func FetchAllSongs() []SongData {
|
||||
rows, err := db.Dbpool.Query(db.Ctx,
|
||||
"SELECT song_name, path FROM song")
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var songDataList []SongData
|
||||
for rows.Next() {
|
||||
var songName string
|
||||
var path string
|
||||
|
||||
err := rows.Scan(&songName, &path)
|
||||
if err != nil {
|
||||
if compareError.Error() != err.Error() {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "QueryRow failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
|
||||
songDataList = append(songDataList, SongData{
|
||||
SongName: songName,
|
||||
Path: path,
|
||||
})
|
||||
}
|
||||
return songDataList
|
||||
}
|
||||
|
||||
func RemoveBrokenSongs(songs []SongData) {
|
||||
joined := ""
|
||||
for _, song := range songs {
|
||||
joined += "'" + song.Path + "',"
|
||||
}
|
||||
joined = strings.TrimSuffix(joined, ",")
|
||||
|
||||
_, err := db.Dbpool.Exec(db.Ctx, "DELETE FROM song where path in ($1)", joined)
|
||||
if err != nil {
|
||||
_, _ = fmt.Fprintf(os.Stderr, "Exec failed: %v\n", err)
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// sqlc v1.29.0
|
||||
|
||||
package repository
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// sqlc v1.29.0
|
||||
// source: game.sql
|
||||
|
||||
package repository
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// sqlc v1.29.0
|
||||
|
||||
package repository
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// sqlc v1.29.0
|
||||
// source: song.sql
|
||||
|
||||
package repository
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.27.0
|
||||
// sqlc v1.29.0
|
||||
// source: song_list.sql
|
||||
|
||||
package repository
|
||||
|
||||
41
internal/server/downloadHandler.go
Normal file
41
internal/server/downloadHandler.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/labstack/echo/v4"
|
||||
"log"
|
||||
"music-server/internal/backend"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type DownloadHandler struct {
|
||||
}
|
||||
|
||||
func NewDownloadHandler() *DownloadHandler {
|
||||
return &DownloadHandler{}
|
||||
}
|
||||
|
||||
func (d *DownloadHandler) checkLatest(ctx echo.Context) error {
|
||||
log.Println("Checking latest version")
|
||||
latest := backend.CheckLatest()
|
||||
return ctx.JSON(http.StatusOK, latest)
|
||||
}
|
||||
|
||||
func (d *DownloadHandler) listAssetsOfLatest(ctx echo.Context) error {
|
||||
log.Println("Listing assets")
|
||||
assets := backend.ListAssetsOfLatest()
|
||||
return ctx.JSON(http.StatusOK, assets)
|
||||
}
|
||||
|
||||
func (d *DownloadHandler) downloadLatestWindows(ctx echo.Context) error {
|
||||
log.Println("Downloading latest windows")
|
||||
asset := backend.DownloadLatestWindows()
|
||||
ctx.Response().Header().Set("Content-Type", "application/octet-stream")
|
||||
return ctx.Redirect(http.StatusFound, asset)
|
||||
}
|
||||
|
||||
func (d *DownloadHandler) downloadLatestLinux(ctx echo.Context) error {
|
||||
log.Println("Downloading latest linux")
|
||||
asset := backend.DownloadLatestLinux()
|
||||
ctx.Response().Header().Set("Content-Type", "application/octet-stream")
|
||||
return ctx.Redirect(http.StatusFound, asset)
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"log"
|
||||
"music-server/internal/backend"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -16,6 +17,10 @@ func NewMusicHandler() *MusicHandler {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetSong(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
song := ctx.QueryParam("song")
|
||||
if song == "" {
|
||||
return ctx.String(http.StatusBadRequest, "song can't be empty")
|
||||
@@ -30,6 +35,10 @@ func (m *MusicHandler) GetSong(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetSoundCheckSong(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
songPath := backend.GetSoundCheckSong()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
@@ -40,11 +49,19 @@ func (m *MusicHandler) GetSoundCheckSong(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) ResetMusic(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
backend.Reset()
|
||||
return ctx.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetRandomSong(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
songPath := backend.GetRandomSong()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
@@ -55,6 +72,10 @@ func (m *MusicHandler) GetRandomSong(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetRandomSongLowChance(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
songPath := backend.GetRandomSongLowChance()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
@@ -65,6 +86,10 @@ func (m *MusicHandler) GetRandomSongLowChance(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetRandomSongClassic(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
songPath := backend.GetRandomSongClassic()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
@@ -85,6 +110,10 @@ func (m *MusicHandler) GetPlayedSongs(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetNextSong(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
songPath := backend.GetNextSong()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
@@ -95,6 +124,10 @@ func (m *MusicHandler) GetNextSong(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetPreviousSong(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
songPath := backend.GetPreviousSong()
|
||||
file, err := os.Open(songPath)
|
||||
if err != nil {
|
||||
@@ -105,11 +138,19 @@ func (m *MusicHandler) GetPreviousSong(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetAllGames(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
gameList := backend.GetAllGames()
|
||||
return ctx.JSON(http.StatusOK, gameList)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) GetAllGamesRandom(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
gameList := backend.GetAllGamesRandom()
|
||||
return ctx.JSON(http.StatusOK, gameList)
|
||||
}
|
||||
@@ -119,6 +160,10 @@ type played struct {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) PutPlayed(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
var played played
|
||||
err := ctx.Bind(&played)
|
||||
if err != nil {
|
||||
@@ -129,11 +174,19 @@ func (m *MusicHandler) PutPlayed(ctx echo.Context) error {
|
||||
}
|
||||
|
||||
func (m *MusicHandler) AddLatestToQue(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
backend.AddLatestToQue()
|
||||
return ctx.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
func (m *MusicHandler) AddLatestPlayed(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
backend.AddLatestPlayed()
|
||||
return ctx.NoContent(http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -12,10 +12,22 @@ import (
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
echoSwagger "github.com/swaggo/echo-swagger" // echo-swagger middleware
|
||||
"github.com/swaggo/echo-swagger" // echo-swagger middleware
|
||||
//_ "github.com/swaggo/echo-swagger/example/docs" // docs is generated by Swag CLI, you have to import it.
|
||||
)
|
||||
|
||||
// @Title Swagger Example API
|
||||
// @version 0.5
|
||||
// @description This is a sample server Petstore server.
|
||||
// @termsOfService http://swagger.io/terms/
|
||||
|
||||
// @contact.name Sebastian Olsson
|
||||
// @contact.email zarnor91@gmail.com
|
||||
|
||||
// @license.name Apache 2.0
|
||||
// @license.url http://www.apache.org/licenses/LICENSE-2.0.html
|
||||
|
||||
// @host localhost:8080
|
||||
func (s *Server) RegisterRoutes() http.Handler {
|
||||
e := echo.New()
|
||||
e.Use(middleware.Logger())
|
||||
@@ -37,15 +49,15 @@ func (s *Server) RegisterRoutes() http.Handler {
|
||||
|
||||
e.Static("/", "/frontend")
|
||||
|
||||
swagger := http.FileServer(http.FS(web.Swagger))
|
||||
e.GET("/swagger/*", echo.WrapHandler(swagger))
|
||||
/*swagger := http.FileServer(http.FS(web.Swagger))
|
||||
e.GET("/swagger/*", echo.WrapHandler(swagger))*/
|
||||
|
||||
swaggerRedirect := func(c echo.Context) error {
|
||||
return c.Redirect(http.StatusMovedPermanently, "/doc/index.html")
|
||||
return c.Redirect(http.StatusMovedPermanently, "/swagger/index.html")
|
||||
}
|
||||
e.GET("/doc", swaggerRedirect)
|
||||
e.GET("/doc/", swaggerRedirect)
|
||||
e.GET("/doc/*", echoSwagger.WrapHandler)
|
||||
e.GET("/swagger", swaggerRedirect)
|
||||
e.GET("/swagger/", swaggerRedirect)
|
||||
e.GET("/swagger/*", echoSwagger.WrapHandler)
|
||||
|
||||
index := NewIndexHandler()
|
||||
e.GET("/version", index.GetVersion)
|
||||
@@ -54,12 +66,19 @@ func (s *Server) RegisterRoutes() http.Handler {
|
||||
e.GET("/character", index.GetCharacter)
|
||||
e.GET("/characters", index.GetCharacters)
|
||||
|
||||
download := NewDownloadHandler()
|
||||
e.GET("/download", download.checkLatest)
|
||||
e.GET("/download/list", download.listAssetsOfLatest)
|
||||
e.GET("/download/windows", download.downloadLatestWindows)
|
||||
e.GET("/download/linux", download.downloadLatestLinux)
|
||||
|
||||
sync := NewSyncHandler()
|
||||
syncGroup := e.Group("/sync")
|
||||
syncGroup.GET("", sync.SyncGames)
|
||||
syncGroup.GET("", sync.SyncGamesNewOnlyChanges)
|
||||
syncGroup.GET("/progress", sync.SyncProgress)
|
||||
syncGroup.GET("/new", sync.SyncGamesNewOnlyChanges)
|
||||
syncGroup.GET("/new/full", sync.SyncGamesNewFull)
|
||||
syncGroup.GET("/quick", sync.SyncGamesQuick)
|
||||
syncGroup.GET("/quick", sync.SyncGamesNewOnlyChanges)
|
||||
syncGroup.GET("/reset", sync.ResetGames)
|
||||
|
||||
music := NewMusicHandler()
|
||||
|
||||
@@ -15,12 +15,13 @@ type Server struct {
|
||||
}
|
||||
|
||||
var (
|
||||
host = os.Getenv("DB_HOST")
|
||||
dbPort = os.Getenv("DB_PORT")
|
||||
dbName = os.Getenv("DB_NAME")
|
||||
username = os.Getenv("DB_USERNAME")
|
||||
password = os.Getenv("DB_PASSWORD")
|
||||
musicPath = os.Getenv("MUSIC_PATH")
|
||||
host = os.Getenv("DB_HOST")
|
||||
dbPort = os.Getenv("DB_PORT")
|
||||
dbName = os.Getenv("DB_NAME")
|
||||
username = os.Getenv("DB_USERNAME")
|
||||
password = os.Getenv("DB_PASSWORD")
|
||||
musicPath = os.Getenv("MUSIC_PATH")
|
||||
charactersPath = os.Getenv("CHARACTERS_PATH")
|
||||
)
|
||||
|
||||
func NewServer() *http.Server {
|
||||
@@ -30,15 +31,16 @@ func NewServer() *http.Server {
|
||||
port: port,
|
||||
}
|
||||
|
||||
//conf.SetupDb()
|
||||
if host == "" || dbPort == "" || username == "" || password == "" || dbName == "" || musicPath == "" {
|
||||
log.Fatal("Invalid settings")
|
||||
}
|
||||
|
||||
fmt.Printf("host: %s, dbPort: %v, username: %s, password: %s, dbName: %s\n",
|
||||
host, dbPort, username, password, dbName)
|
||||
|
||||
log.Printf("Path: %s\n", musicPath)
|
||||
log.Printf("musicPath: %s\n", musicPath)
|
||||
log.Printf("charactersPath: %s\n", charactersPath)
|
||||
|
||||
//conf.SetupDb()
|
||||
if host == "" || dbPort == "" || username == "" || password == "" || dbName == "" || musicPath == "" || charactersPath == "" {
|
||||
log.Fatal("Invalid settings")
|
||||
}
|
||||
|
||||
db.Migrate_db(host, dbPort, username, password, dbName)
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@ package server
|
||||
import (
|
||||
"log"
|
||||
"music-server/internal/backend"
|
||||
"music-server/internal/database"
|
||||
"net/http"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -16,33 +15,42 @@ func NewSyncHandler() *SyncHandler {
|
||||
return &SyncHandler{}
|
||||
}
|
||||
|
||||
func (s *SyncHandler) SyncGames(ctx echo.Context) error {
|
||||
database.SyncGames()
|
||||
backend.Reset()
|
||||
return ctx.JSON(http.StatusOK, "Games are synced")
|
||||
}
|
||||
|
||||
func (s *SyncHandler) SyncGamesQuick(ctx echo.Context) error {
|
||||
database.SyncGamesQuick()
|
||||
backend.Reset()
|
||||
return ctx.JSON(http.StatusOK, "Games are synced")
|
||||
func (s *SyncHandler) SyncProgress(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Getting progress")
|
||||
response := backend.SyncProgress()
|
||||
return ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
log.Println("Getting result")
|
||||
response := backend.SyncResult()
|
||||
return ctx.JSON(http.StatusOK, response)
|
||||
}
|
||||
|
||||
func (s *SyncHandler) SyncGamesNewOnlyChanges(ctx echo.Context) error {
|
||||
log.Println("Syncing games new")
|
||||
response := backend.SyncGamesNewOnlyChanges()
|
||||
backend.Reset()
|
||||
return ctx.JSON(http.StatusOK, response)
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
log.Println("Start syncing games")
|
||||
go backend.SyncGamesNewOnlyChanges()
|
||||
return ctx.JSON(http.StatusOK, "Start syncing games")
|
||||
}
|
||||
|
||||
func (s *SyncHandler) SyncGamesNewFull(ctx echo.Context) error {
|
||||
log.Println("Syncing games new full")
|
||||
response := backend.SyncGamesNewFull()
|
||||
backend.Reset()
|
||||
return ctx.JSON(http.StatusOK, response)
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
log.Println("Start syncing games full")
|
||||
go backend.SyncGamesNewFull()
|
||||
return ctx.JSON(http.StatusOK, "Start syncing games full")
|
||||
}
|
||||
|
||||
func (s *SyncHandler) ResetGames(ctx echo.Context) error {
|
||||
if backend.Syncing {
|
||||
log.Println("Syncing is in progress")
|
||||
return ctx.JSON(http.StatusLocked, "Syncing is in progress")
|
||||
}
|
||||
backend.ResetDB()
|
||||
return ctx.JSON(http.StatusOK, "Games and songs are deleted from the database")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user