Skip to content

Commit 97dd72c

Browse files
committed
bug fixes, time to test
1 parent 4c43dba commit 97dd72c

File tree

14 files changed

+185
-24
lines changed

14 files changed

+185
-24
lines changed

MyMusicBoxApi/buildProd.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
go build -buildvcs=false -o ~/mymusicbox_production

MyMusicBoxApi/database/playlistsong.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (pdb *PostgresDb) FetchPlaylistSongs(ctx context.Context, playlistId int, l
1616

1717
query := `SELECT s.Id, s.Name, s.Path, s.ThumbnailPath, s.Duration, s.SourceId, s.UpdatedAt, s.CreatedAt FROM playlistsong ps
1818
INNER JOIN song s ON s.id = ps.songid
19-
WHERE ps.playlistid = $1 AND ps.position > $2`
19+
WHERE ps.playlistid = $1 AND ps.position >= $2`
2020

2121
statement, err := pdb.connection.Prepare(query)
2222
defer statement.Close()

MyMusicBoxApi/database/postgres.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"database/sql"
55
"errors"
66
"fmt"
7+
"musicboxapi/configuration"
78
"musicboxapi/logging"
89
"os"
910
"strings"
@@ -20,7 +21,13 @@ type PostgresDb struct {
2021

2122
func (pdb *PostgresDb) OpenConnection() (created bool) {
2223

23-
baseConnectionString := "user=postgres dbname=postgres password=%s host=127.0.0.1 port=5432 sslmode=disable"
24+
baseConnectionString := ""
25+
26+
if configuration.Config.UseDevUrl {
27+
baseConnectionString = "user=postgres dbname=postgres password=%s host=127.0.0.1 port=5433 sslmode=disable"
28+
} else {
29+
baseConnectionString = "user=postgres dbname=postgres password=%s host=127.0.0.1 port=5432 sslmode=disable"
30+
}
2431

2532
connectionString := fmt.Sprintf(baseConnectionString, os.Getenv("POSTGRES_PASSWORD"))
2633

@@ -68,6 +75,10 @@ func (pdb *PostgresDb) NonScalarQuery(query string, params ...any) (error error)
6875

6976
if err != nil {
7077
logging.Error(fmt.Sprintf("[NonScalarQuery] Exec error: %s", err.Error()))
78+
logging.Error(fmt.Sprintf("Query: %s", query))
79+
for index := range params {
80+
logging.Error(params[index])
81+
}
7182
return err
7283
}
7384

MyMusicBoxApi/database/tasklog.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ func (pdb *PostgresDb) EndTaskLog(taskId int, nStatus int, data []byte) error {
2828
return pdb.NonScalarQuery(query, nStatus, data, time.Now(), taskId)
2929
}
3030

31+
func (pdb *PostgresDb) UpdateTaskLogError(params ...any) error {
32+
query := `UPDATE TaskLog
33+
SET Status = $1, OutputLog = $2, EndTime = $3
34+
WHERE Id = $4`
35+
return pdb.NonScalarQuery(query, params...)
36+
}
37+
3138
func (pdb *PostgresDb) GetTaskLogs(ctx context.Context) ([]models.TaskLog, error) {
3239
query := `SELECT Id, StartTime, EndTime, Status, OutputLog FROM TaskLog ORDER BY Id desc` // get the latest first
3340

2.83 KB
Loading

MyMusicBoxApi/go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ require (
88
github.com/ProtonMail/go-crypto v1.3.0 // indirect
99
github.com/cloudflare/circl v1.6.1 // indirect
1010
github.com/google/go-cmp v0.7.0 // indirect
11+
github.com/ulikunitz/xz v0.5.12 // indirect
1112
)
1213

1314
require (
@@ -25,7 +26,7 @@ require (
2526
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
2627
github.com/leodido/go-urn v1.4.0 // indirect
2728
github.com/lib/pq v1.10.9
28-
github.com/lrstanley/go-ytdlp v0.0.0-20250610000944-a2284ab714d8
29+
github.com/lrstanley/go-ytdlp v1.1.1-0.20250710012800-43387ad4d4a9
2930
github.com/mattn/go-isatty v0.0.20 // indirect
3031
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
3132
github.com/modern-go/reflect2 v1.0.2 // indirect

MyMusicBoxApi/http/tasklog.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package http
2+
3+
import (
4+
"fmt"
5+
"musicboxapi/database"
6+
"musicboxapi/models"
7+
8+
"github.com/gin-gonic/gin"
9+
)
10+
11+
func FetchTaskLogs(ctx *gin.Context) {
12+
db := database.PostgresDb{}
13+
defer db.CloseConnection()
14+
15+
logs, err := db.GetTaskLogs(ctx.Request.Context())
16+
17+
if err != nil {
18+
ctx.JSON(500, models.ErrorResponse(err.Error()))
19+
}
20+
21+
ctx.JSON(200, models.OkResponse(logs, fmt.Sprintf("%d", len(logs))))
22+
}

MyMusicBoxApi/main.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"musicboxapi/configuration"
77
"musicboxapi/http"
88
"musicboxapi/logging"
9+
"os"
910

1011
"github.com/gin-contrib/cors"
1112
"github.com/gin-gonic/gin"
@@ -14,7 +15,6 @@ import (
1415

1516
func main() {
1617
// If yt-dlp isn't installed yet, download and cache it for further use.
17-
// todo test....
1818
go ytdlp.MustInstall(context.TODO(), nil)
1919

2020
configuration.LoadConfig()
@@ -32,13 +32,29 @@ func main() {
3232
if configuration.Config.UseDevUrl {
3333
engine.Use(cors.Default())
3434
logging.Warning("CORS is enabled for all origins")
35+
} else {
36+
37+
origin := os.Getenv("CORS_ORIGIN")
38+
39+
// Use Default cors
40+
if len(origin) == 0 {
41+
engine.Use(cors.Default())
42+
logging.Warning("CORS is enabled for all origins")
43+
} else {
44+
strictCors := cors.New(cors.Config{
45+
AllowAllOrigins: false,
46+
AllowOrigins: []string{origin}, // move to env
47+
})
48+
engine.Use(strictCors)
49+
}
3550
}
3651

3752
apiv1Group := engine.Group(configuration.GetApiGroupUrl("v1"))
3853
apiv1Group.GET("/songs", http.FetchSongs)
3954
apiv1Group.GET("/playlist", http.FetchPlaylists)
4055
apiv1Group.GET("/playlist/:playlistId", http.FetchPlaylistSongs)
4156
apiv1Group.GET("/play/:sourceId", http.Play)
57+
apiv1Group.GET("/tasklogs", http.FetchTaskLogs)
4258

4359
apiv1Group.POST("/playlist", http.InsertPlaylist)
4460
apiv1Group.POST("/playlistsong/:playlistId/:songId", http.InsertPlaylistSong)

MyMusicBoxApi/models/service.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package models
22

33
type YtdlpJsonResult struct {
4-
Id string `json:"id"`
5-
Title string `json:"title"`
6-
Duration int `json:"duration"`
4+
Id string `json:"id"`
5+
Title string `json:"title"`
6+
Duration int `json:"duration"`
7+
Playlist string `json:"playlist"`
8+
PlaylistIndex int `json:"index"`
79
}

MyMusicBoxApi/service/playlist.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ func downloadPlaylist(
5353

5454
playlistExists := false
5555
playlistId := -1
56-
downloadCounter := 0
5756

5857
for _, playlist := range existingPlaylists {
5958
if playlist.Name == playlistNames[0] {
@@ -66,14 +65,21 @@ func downloadPlaylist(
6665
_playlistId, _ := readLines(playlistIdFileName)
6766

6867
if !playlistExists {
69-
playlistId, _ = db.InsertPlaylist(models.Playlist{
68+
_newPlaylistId, err := db.InsertPlaylist(models.Playlist{
7069
Name: playlistNames[0],
7170
Description: "Custom playlist",
7271
ThumbnailPath: fmt.Sprintf("%s.jpg", _playlistId[0]),
7372
CreationDate: time.Now(),
7473
IsPublic: true,
7574
UpdatedAt: time.Now(),
7675
})
76+
77+
if err != nil {
78+
logging.Error(fmt.Sprintf("[Creating custom playlist error]: %s", err.Error()))
79+
return
80+
}
81+
82+
playlistId = _newPlaylistId
7783
}
7884

7985
// Special case, thumbnail is written to root directory
@@ -112,7 +118,6 @@ func downloadPlaylist(
112118
for id := range downloadCount {
113119
name := names[id]
114120
if canDownload(name) {
115-
downloadCounter++
116121
ytdlpInstance := defaultSettings.Clone()
117122

118123
result, err := ytdlpInstance.Run(context.Background(), fmt.Sprintf("https://www.youtube.com/watch?v=%s", ids[id]))

0 commit comments

Comments
 (0)