fix(podcast): truncate filenames longer than 200 chars

fixes #624
This commit is contained in:
sentriz
2025-11-05 12:10:32 -03:00
parent c2ebe96313
commit e12ab544c2
3 changed files with 24 additions and 3 deletions

No files matched your search

+5 -1
View File
@@ -14,9 +14,13 @@ import (
var nonAlphaNumExpr = regexp.MustCompile("[^a-zA-Z0-9_.]+") var nonAlphaNumExpr = regexp.MustCompile("[^a-zA-Z0-9_.]+")
const maxFilenameLength = 200
func Safe(filename string) string { func Safe(filename string) string {
filename = nonAlphaNumExpr.ReplaceAllString(filename, "") filename = nonAlphaNumExpr.ReplaceAllString(filename, "")
return filename ext := filepath.Ext(filename)
filename = filename[:min(maxFilenameLength, len(filename))-len(ext)]
return filename + ext
} }
// try to find a unqiue file (or dir) name. incrementing like "example (1)" // try to find a unqiue file (or dir) name. incrementing like "example (1)"
+17
View File
@@ -57,3 +57,20 @@ func TestFirst(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, p("test"), r) require.Equal(t, p("test"), r)
} }
func TestSafe(t *testing.T) {
t.Parallel()
longName := "hi_00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"
result := Safe(longName)
require.Equal(t, 200, len(result))
require.Equal(t, longName[:200], result)
longNameWithExt := "hi_00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.mp3"
result = Safe(longNameWithExt)
require.Equal(t, 200, len(result))
require.Equal(t, ".mp3", result[len(result)-4:])
require.Equal(t, "test.mp3", Safe("test.mp3"))
require.Equal(t, "testing.mp3", Safe("test ing.mp3"))
}
+2 -2
View File
@@ -513,7 +513,7 @@ func (p *Podcasts) doPodcastDownload(podcast *db.Podcast, podcastEpisode *db.Pod
return fmt.Errorf("get podcast episode filename: %w", err) return fmt.Errorf("get podcast episode filename: %w", err)
} }
podcastEpisode.Filename = filename podcastEpisode.Filename = filename
if err := p.db.Save(&podcastEpisode).Error; err != nil { if err := p.db.Save(podcastEpisode).Error; err != nil {
return fmt.Errorf("save podcast episode: %w", err) return fmt.Errorf("save podcast episode: %w", err)
} }
if err := os.MkdirAll(filepath.Dir(filepath.Join(podcast.RootDir, podcastEpisode.Filename)), os.ModePerm); err != nil { if err := os.MkdirAll(filepath.Dir(filepath.Join(podcast.RootDir, podcastEpisode.Filename)), os.ModePerm); err != nil {
@@ -528,7 +528,7 @@ func (p *Podcasts) doPodcastDownload(podcast *db.Podcast, podcastEpisode *db.Pod
defer func() { defer func() {
if err != nil { if err != nil {
podcastEpisode.Status = db.PodcastEpisodeStatusError podcastEpisode.Status = db.PodcastEpisodeStatusError
_ = p.db.Save(&podcastEpisode).Error _ = p.db.Save(podcastEpisode).Error
} }
}() }()