diff --git a/core/playback/mpv/socket_name.go b/core/playback/mpv/socket_name.go new file mode 100644 index 00000000..a3722fe3 --- /dev/null +++ b/core/playback/mpv/socket_name.go @@ -0,0 +1,9 @@ +//go:build !windows + +package mpv + +import "github.com/navidrome/navidrome/utils" + +func SocketName(prefix, suffix string) string { + return utils.TempFileName(prefix, suffix) +} diff --git a/core/playback/mpv/socket_name_win.go b/core/playback/mpv/socket_name_win.go new file mode 100644 index 00000000..e6285a93 --- /dev/null +++ b/core/playback/mpv/socket_name_win.go @@ -0,0 +1,15 @@ +//go:build windows + +package mpv + +import ( + "path/filepath" + + "github.com/google/uuid" +) + +func SocketName() string { + // Windows needs to use a named pipe for the socket + // see https://mpv.io/manual/master#using-mpv-from-other-programs-or-scripts + return filepath.Join(`\\.\pipe\mpvsocket`, prefix+uuid.NewString()+suffix) +} diff --git a/core/playback/mpv/track.go b/core/playback/mpv/track.go index c8cdae0b..7bfd2776 100644 --- a/core/playback/mpv/track.go +++ b/core/playback/mpv/track.go @@ -32,7 +32,7 @@ func NewTrack(playbackDoneChannel chan bool, deviceName string, mf model.MediaFi return nil, err } - tmpSocketName := utils.RandomSocketOrFileName("mpv-ctrl-", ".socket") + tmpSocketName := utils.TempFileName("mpv-ctrl-", ".socket") args := createMPVCommand(mpvComdTemplate, deviceName, mf.Path, tmpSocketName) exe, err := start(args) diff --git a/scanner/metadata/taglib/taglib_test.go b/scanner/metadata/taglib/taglib_test.go index f0c1784e..31007910 100644 --- a/scanner/metadata/taglib/taglib_test.go +++ b/scanner/metadata/taglib/taglib_test.go @@ -181,7 +181,7 @@ var _ = Describe("Extractor", func() { // Only run permission tests if we are not root RegularUserContext("when run without root privileges", func() { BeforeEach(func() { - accessForbiddenFile = utils.RandomSocketOrFileName("access_forbidden-", ".mp3") + accessForbiddenFile = utils.TempFileName("access_forbidden-", ".mp3") f, err := os.OpenFile(accessForbiddenFile, os.O_WRONLY|os.O_CREATE, 0222) Expect(err).ToNot(HaveOccurred()) diff --git a/utils/files.go b/utils/files.go index bae79b08..293aba94 100644 --- a/utils/files.go +++ b/utils/files.go @@ -3,17 +3,10 @@ package utils import ( "os" "path/filepath" - "runtime" "github.com/google/uuid" ) -func RandomSocketOrFileName(prefix, suffix string) string { - socketPath := os.TempDir() - // Windows needs to use a named pipe instead of a file for the socket - // see https://mpv.io/manual/master#using-mpv-from-other-programs-or-scripts - if runtime.GOOS == "windows" { - socketPath = `\\.\pipe\mpvsocket` - } - return filepath.Join(socketPath, prefix+uuid.NewString()+suffix) +func TempFileName(prefix, suffix string) string { + return filepath.Join(os.TempDir(), prefix+uuid.NewString()+suffix) }