Backend: Apply linter recommendations to "photoprism/dl" package #5330

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer 2025-11-22 10:21:59 +01:00
parent cc651a84d0
commit 24e29a89ff
7 changed files with 31 additions and 26 deletions

View file

@ -21,15 +21,15 @@ func ytDlpCommand(ctx context.Context, args []string) *exec.Cmd {
if sh == "" {
sh = "bash"
}
return exec.CommandContext(ctx, sh, append([]string{bin}, args...)...)
return exec.CommandContext(ctx, sh, append([]string{bin}, args...)...) // #nosec G204 command constructed from validated yt-dlp path and args
}
return exec.CommandContext(ctx, bin, args...)
return exec.CommandContext(ctx, bin, args...) // #nosec G204 command constructed from validated yt-dlp path and args
}
// isShellScript tries to detect if a file starts with a shebang (#!).
func isShellScript(path string) bool {
f, err := os.Open(path)
f, err := os.Open(path) // #nosec G304 path provided by FindYtDlpBin
if err != nil {
return false
}

View file

@ -26,6 +26,7 @@ func (result Metadata) DownloadToFileWithOptions(
out := outTpl
out = strings.ReplaceAll(out, "%(id)s", "abc")
out = strings.ReplaceAll(out, "%(ext)s", "mp4")
// #nosec G301 media download directory should be accessible to user
if err := os.MkdirAll(filepath.Dir(out), 0o755); err != nil {
return nil, err
}
@ -33,6 +34,7 @@ func (result Metadata) DownloadToFileWithOptions(
if content == "" {
content = "dummy"
}
// #nosec G306 downloaded media files are intended to be user-readable
if err := os.WriteFile(out, []byte(content), 0o644); err != nil {
return nil, err
}
@ -59,7 +61,7 @@ func (result Metadata) DownloadToFileWithOptions(
if !result.Options.noInfoDownload {
jsonTempPath = filepath.Join(tempPath, "info.json")
if err := os.WriteFile(jsonTempPath, result.RawJSON, 0600); err != nil {
os.RemoveAll(tempPath)
_ = os.RemoveAll(tempPath)
return nil, err
}
}
@ -94,12 +96,12 @@ func (result Metadata) DownloadToFileWithOptions(
if result.Options.PlaylistStart > 0 {
cmd.Args = append(cmd.Args,
"--playlist-start", strconv.Itoa(int(result.Options.PlaylistStart)),
"--playlist-start", strconv.FormatUint(uint64(result.Options.PlaylistStart), 10),
)
}
if result.Options.PlaylistEnd > 0 {
cmd.Args = append(cmd.Args,
"--playlist-end", strconv.Itoa(int(result.Options.PlaylistEnd)),
"--playlist-end", strconv.FormatUint(uint64(result.Options.PlaylistEnd), 10),
)
}
if result.Options.FlatPlaylist {

View file

@ -59,8 +59,8 @@ type Info struct {
ChapterNumber float64 `json:"chapter_number"` // Number of the chapter the video belongs to
ChapterID string `json:"chapter_id"` // Id of the chapter the video belongs to
// Available for the video that is an episode of some series or programme:
Series string `json:"series"` // Title of the series or programme the video episode belongs to
// Available for the video that is an episode of some series or program:
Series string `json:"series"` // Title of the series or program the video episode belongs to
Season string `json:"season"` // Title of the season the video episode belongs to
SeasonNumber float64 `json:"season_number"` // Number of the season the video episode belongs to
SeasonID string `json:"season_id"` // Id of the season the video episode belongs to
@ -171,12 +171,12 @@ func infoFromURL(
if options.PlaylistStart > 0 {
cmd.Args = append(cmd.Args,
"--playlist-start", strconv.Itoa(int(options.PlaylistStart)),
"--playlist-start", strconv.FormatUint(uint64(options.PlaylistStart), 10),
)
}
if options.PlaylistEnd > 0 {
cmd.Args = append(cmd.Args,
"--playlist-end", strconv.Itoa(int(options.PlaylistEnd)),
"--playlist-end", strconv.FormatUint(uint64(options.PlaylistEnd), 10),
)
}
if options.FlatPlaylist {
@ -280,7 +280,7 @@ func infoFromURL(
resp, respErr := get(info.Thumbnail)
if respErr == nil {
buf, _ := io.ReadAll(resp.Body)
resp.Body.Close()
_ = resp.Body.Close()
info.ThumbnailBytes = buf
}
}
@ -297,7 +297,7 @@ func infoFromURL(
resp, respErr := get(subtitle.URL)
if respErr == nil {
buf, _ := io.ReadAll(resp.Body)
resp.Body.Close()
_ = resp.Body.Close()
subtitles[i].Bytes = buf
}
}

View file

@ -89,7 +89,7 @@ func (result Metadata) DownloadWithOptions(
if !result.Options.noInfoDownload {
jsonTempPath = path.Join(tempPath, "info.json")
if err := os.WriteFile(jsonTempPath, result.RawJSON, 0600); err != nil {
os.RemoveAll(tempPath)
_ = os.RemoveAll(tempPath)
return nil, err
}
}
@ -98,7 +98,7 @@ func (result Metadata) DownloadWithOptions(
waitCh: make(chan struct{}),
}
cmd := exec.CommandContext(
cmd := exec.CommandContext( // #nosec G204 yt-dlp command uses validated binary path and controlled args
ctx,
FindYtDlpBin(),
// see comment below about ignoring errors for playlists
@ -130,12 +130,12 @@ func (result Metadata) DownloadWithOptions(
if result.Options.PlaylistStart > 0 {
cmd.Args = append(cmd.Args,
"--playlist-start", strconv.Itoa(int(result.Options.PlaylistStart)),
"--playlist-start", strconv.FormatUint(uint64(result.Options.PlaylistStart), 10),
)
}
if result.Options.PlaylistEnd > 0 {
cmd.Args = append(cmd.Args,
"--playlist-end", strconv.Itoa(int(result.Options.PlaylistEnd)),
"--playlist-end", strconv.FormatUint(uint64(result.Options.PlaylistEnd), 10),
)
}
} else {
@ -268,15 +268,15 @@ func (result Metadata) DownloadWithOptions(
log.Trace("cmd", " ", redactArgs(cmd.Args))
if err := cmd.Start(); err != nil {
os.RemoveAll(tempPath)
_ = os.RemoveAll(tempPath)
return nil, err
}
go func() {
_ = cmd.Wait()
stdoutW.Close()
stderrW.Close()
os.RemoveAll(tempPath)
_ = stdoutW.Close()
_ = stderrW.Close()
_ = os.RemoveAll(tempPath)
close(dr.waitCh)
}()

View file

@ -39,6 +39,7 @@ func createFakeYtDlp(t *testing.T) string {
script.WriteString("echo '[download]' 1>&2\n")
script.WriteString("echo 'DATA'\n")
}
// #nosec G306 executable test helper script needs exec permissions
if err := os.WriteFile(path, script.Bytes(), 0o755); err != nil {
t.Fatalf("failed to write fake yt-dlp: %v", err)
}
@ -64,7 +65,7 @@ func TestInfoFromURL_IncludesHeadersAndCookies(t *testing.T) {
t.Fatalf("infoFromURL error: %v", err)
}
data, err := os.ReadFile(argsLog)
data, err := os.ReadFile(filepath.Clean(argsLog))
if err != nil {
t.Fatalf("reading args log failed: %v", err)
}
@ -102,7 +103,7 @@ func TestDownloadWithOptions_IncludesHeadersAndCookies_Pipe(t *testing.T) {
_, _ = dr.Read(buf)
_ = dr.Close()
data, err := os.ReadFile(argsLog)
data, err := os.ReadFile(filepath.Clean(argsLog))
if err != nil {
t.Fatalf("reading args log failed: %v", err)
}
@ -132,7 +133,7 @@ func TestDownloadWithOptions_OmitsFilterWhenDirect(t *testing.T) {
if err != nil {
t.Fatalf("DownloadWithOptions error: %v", err)
}
data, err := os.ReadFile(argsLog)
data, err := os.ReadFile(filepath.Clean(argsLog))
if err != nil {
t.Fatalf("reading args log failed: %v", err)
}
@ -196,7 +197,7 @@ func TestDownloadToFileWithOptions_IncludesPostprocessorArgs(t *testing.T) {
t.Fatalf("DownloadToFileWithOptions error: %v", err)
}
data, err := os.ReadFile(argsLog)
data, err := os.ReadFile(filepath.Clean(argsLog))
if err != nil {
t.Fatalf("reading args log failed: %v", err)
}
@ -231,7 +232,7 @@ func TestDownloadWithOptions_IncludesPostprocessorArgs_Pipe(t *testing.T) {
_, _ = dr.Read(buf)
_ = dr.Close()
data, err := os.ReadFile(argsLog)
data, err := os.ReadFile(filepath.Clean(argsLog))
if err != nil {
t.Fatalf("reading args log failed: %v", err)
}

View file

@ -29,7 +29,7 @@ func VersionWarning() (string, bool) {
return
}
cmd := exec.Command(bin, "--version")
cmd := exec.Command(bin, "--version") // #nosec G204 yt-dlp path validated via FindYtDlpBin
cmd.Env = os.Environ()
var out bytes.Buffer
cmd.Stdout = &out

View file

@ -50,6 +50,7 @@ func writeVersionScript(t *testing.T, version string) string {
if runtime.GOOS == "windows" {
content := "@echo off\r\n" +
"echo " + version + "\r\n"
// #nosec G306 executable test helper script
if err := os.WriteFile(path, []byte(content), 0o755); err != nil {
t.Fatalf("failed to write fake yt-dlp: %v", err)
}
@ -59,6 +60,7 @@ func writeVersionScript(t *testing.T, version string) string {
content := "#!/usr/bin/env bash\n" +
"set -euo pipefail\n" +
"echo '" + version + "'\n"
// #nosec G306 executable test helper script
if err := os.WriteFile(path, []byte(content), 0o755); err != nil {
t.Fatalf("failed to write fake yt-dlp: %v", err)
}