Skip to content

Commit 2a62c56

Browse files
author
Datanoise
committed
fix: resolve form action path corruption and improve playlist loading safety
1 parent aebbca5 commit 2a62c56

6 files changed

Lines changed: 52 additions & 35 deletions

File tree

playlists/foo.pls

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
[playlist]
2-
NumberOfEntries=0
2+
NumberOfEntries=2
3+
File1=/Users/syso/dev/tinyice/music/a_d.mp3
4+
Title1=a_d.mp3
5+
File2=/Users/syso/dev/tinyice/music/Lexyyysy42l.mp3
6+
Title2=Lexyyysy42l.mp3
37
Version=2

relay/streamer.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,13 @@ func (s *Streamer) LoadPlaylist(filename string) error {
272272
if err != nil {
273273
if os.IsNotExist(err) {
274274
if filename == s.Name+".pls" {
275-
return s.SavePlaylist()
275+
// Only save if we don't have a playlist in memory either
276+
s.mu.RLock()
277+
empty := len(s.Playlist) == 0
278+
s.mu.RUnlock()
279+
if empty {
280+
return s.SavePlaylist()
281+
}
276282
}
277283
return nil
278284
}
@@ -449,7 +455,12 @@ func (sm *StreamerManager) StartStreamer(name, mount, musicDir string, loop bool
449455
}
450456

451457
sm.instances[mount] = s
452-
s.LoadPlaylist("")
458+
459+
if lastPlaylist != "" {
460+
s.LoadPlaylist(lastPlaylist)
461+
} else {
462+
s.LoadPlaylist("")
463+
}
453464

454465
go sm.runStreamerLoop(ctx, s)
455466
return s, nil

relay/transcode.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,15 +273,11 @@ func EncodeOpus(ctx context.Context, relay *Relay, output *Stream, decoder io.Re
273273
case <-ctx.Done():
274274
return
275275
default:
276-
rn, rerr := io.ReadFull(decoder, pcmBuf)
276+
_, rerr := io.ReadFull(decoder, pcmBuf)
277277
if rerr != nil {
278278
return
279279
}
280280

281-
if sentCount == 0 {
282-
logrus.Debugf("Opus Encoder: Successfully read first PCM frame (%d bytes)", rn)
283-
}
284-
285281
for i := 0; i < len(pcmSamples); i++ {
286282
pcmSamples[i] = int16(pcmBuf[i*2]) | int16(pcmBuf[i*2+1])<<8
287283
}

server/server.go

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,7 +2536,10 @@ func (s *Server) handlePlayerLoadPlaylist(w http.ResponseWriter, r *http.Request
25362536
return
25372537
}
25382538

2539-
if err := streamer.LoadPlaylist(filename); err != nil {
2539+
// Filename from library browser is absolute now
2540+
playlistName := filepath.Base(filename)
2541+
2542+
if err := streamer.LoadPlaylist(playlistName); err != nil {
25402543
http.Error(w, err.Error(), http.StatusInternalServerError)
25412544
return
25422545
}
@@ -2546,7 +2549,7 @@ func (s *Server) handlePlayerLoadPlaylist(w http.ResponseWriter, r *http.Request
25462549
for _, adj := range s.Config.AutoDJs {
25472550
if adj.Mount == mount {
25482551
adj.Playlist = playlistCopy
2549-
adj.LastPlaylist = filename
2552+
adj.LastPlaylist = playlistName
25502553
s.Config.SaveConfig()
25512554
break
25522555
}
@@ -2795,21 +2798,33 @@ func (s *Server) handlePlayerPlaylistAction(w http.ResponseWriter, r *http.Reque
27952798

27962799
if action == "add" {
27972800
fullPath := relPath // Already absolute from frontend now
2798-
logrus.Debugf("AutoDJ %s: Adding song %s to playlist", mount, fullPath)
2801+
logrus.Infof("AutoDJ %s: Attempting to add %s", mount, fullPath)
27992802

28002803
info, err := os.Stat(fullPath)
2801-
if err == nil {
2802-
if info.IsDir() {
2803-
// Add folder recursively
2804-
filepath.Walk(fullPath, func(p string, i os.FileInfo, e error) error {
2805-
if e == nil && !i.IsDir() && strings.ToLower(filepath.Ext(p)) == ".mp3" {
2806-
streamer.AddToPlaylist(p)
2807-
}
2804+
if err != nil {
2805+
logrus.WithError(err).Errorf("AutoDJ %s: Failed to stat path %s", mount, fullPath)
2806+
http.Error(w, "File not found", http.StatusNotFound)
2807+
return
2808+
}
2809+
2810+
if info.IsDir() {
2811+
logrus.Infof("AutoDJ %s: Adding directory %s", mount, fullPath)
2812+
err := filepath.Walk(fullPath, func(p string, i os.FileInfo, e error) error {
2813+
if e != nil {
2814+
logrus.WithError(e).Warnf("AutoDJ %s: Error walking path %s", mount, p)
28082815
return nil
2809-
})
2810-
} else {
2811-
streamer.AddToPlaylist(fullPath)
2816+
}
2817+
if !i.IsDir() && strings.ToLower(filepath.Ext(p)) == ".mp3" {
2818+
streamer.AddToPlaylist(p)
2819+
}
2820+
return nil
2821+
})
2822+
if err != nil {
2823+
logrus.WithError(err).Errorf("AutoDJ %s: Walk failed for %s", mount, fullPath)
28122824
}
2825+
} else {
2826+
logrus.Infof("AutoDJ %s: Adding single file %s", mount, fullPath)
2827+
streamer.AddToPlaylist(fullPath)
28132828
}
28142829
} else if action == "remove" {
28152830
var idx int

server/templates/admin.html

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -745,7 +745,8 @@ <h2>Edit AutoDJ</h2>
745745
}
746746
}, 2000);
747747

748-
fetch(form.action, {
748+
const url = form.getAttribute('action');
749+
fetch(url, {
749750
method: 'POST',
750751
body: formData
751752
}).then(r => {
@@ -909,14 +910,14 @@ <h2>Edit AutoDJ</h2>
909910
</div>
910911
<div style="display:flex; gap:0.3rem;">
911912
${(!e.is_dir && !e.is_pls) ? `
912-
<form action="/admin/player/queue" method="POST" onsubmit="return submitForm(event, false)" style="margin:0;">
913+
<form action="/admin/player/queue" method="POST" onsubmit="console.log('Queueing:', '${e.abs_path}'); return submitForm(event, false)" style="margin:0;">
913914
<input type="hidden" name="csrf" value="${csrfToken}">
914915
<input type="hidden" name="mount" value="${mount}">
915916
<input type="hidden" name="action" value="add">
916917
<input type="hidden" name="path" value="${e.abs_path}">
917918
<button type="submit" class="btn btn-outline btn-sm" style="padding:0.1rem 0.3rem; font-size:0.7rem;" title="Queue Next">QUEUE</button>
918919
</form>` : ''}
919-
<form action="${actionURL}" method="POST" onsubmit="${confirmMsg ? `if(confirm('${confirmMsg}')) ` : ''}return submitForm(event, true)" style="margin:0;">
920+
<form action="${actionURL}" method="POST" onsubmit="console.log('Adding:', '${e.abs_path}'); ${confirmMsg ? `if(confirm('${confirmMsg}')) ` : ''}return submitForm(event, true)" style="margin:0;">
920921
<input type="hidden" name="csrf" value="${csrfToken}">
921922
<input type="hidden" name="mount" value="${mount}">
922923
<input type="hidden" name="action" value="add">
@@ -1089,17 +1090,6 @@ <h2>Edit AutoDJ</h2>
10891090
});
10901091
}
10911092

1092-
// Ensure forms redirect back to the current tab
1093-
document.addEventListener('submit', function(e) {
1094-
var form = e.target;
1095-
if (form.method.toLowerCase() === 'post') {
1096-
var action = form.getAttribute('action');
1097-
if (action && action.indexOf('#') === -1) {
1098-
form.setAttribute('action', action + window.location.hash);
1099-
}
1100-
}
1101-
});
1102-
11031093
function loadHistory(mount, btn) {
11041094
if (!mount) return;
11051095
document.querySelectorAll('.history-mount-btn').forEach(function(b) { b.classList.remove('active'); });

tinyice.pid

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
38148

0 commit comments

Comments
 (0)