Skip to content

Commit

Permalink
Fix filename issue caused by LimitLength
Browse files Browse the repository at this point in the history
  • Loading branch information
chinuno-usami committed Sep 29, 2019
1 parent b5f6f3e commit 795edc2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 13 deletions.
4 changes: 2 additions & 2 deletions downloader/downloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,9 +180,9 @@ func Download(v Data, refer string, chunkSizeMB int) error {
stream string
)
if config.OutputName == "" {
title = utils.FileName(v.Title)
title = utils.FileName(v.Title, "")
} else {
title = utils.FileName(config.OutputName)
title = utils.FileName(config.OutputName, "")
}
if config.Stream == "" {
stream = v.sortedStreams[0].name
Expand Down
6 changes: 1 addition & 5 deletions extractors/bilibili/bilibili.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,7 @@ func bilibiliDownload(options bilibiliOptions) downloader.Data {
}
title := parser.Title(doc)
if options.subtitle != "" {
tempTitle := fmt.Sprintf("%s %s", title, options.subtitle)
if len([]rune(tempTitle)) > utils.MAXLENGTH {
tempTitle = fmt.Sprintf("%s P%d %s", title, options.page, options.subtitle)
}
title = tempTitle
title = fmt.Sprintf("%s P%d %s", title, options.page, options.subtitle)
}

downloader.Caption(
Expand Down
17 changes: 12 additions & 5 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,19 @@ func LimitLength(s string, length int) string {
}

// FileName Converts a string to a valid filename
func FileName(name string) string {
func FileName(name string, ext string) string {
rep := strings.NewReplacer("\n", " ", "/", " ", "|", "-", ": ", ":", ":", ":", "'", "’")
name = rep.Replace(name)
if runtime.GOOS == "windows" {
rep = strings.NewReplacer("\"", " ", "?", " ", "*", " ", "\\", " ", "<", " ", ">", " ")
name = rep.Replace(name)
}
return LimitLength(name, MAXLENGTH)
limitedName := LimitLength(name, MAXLENGTH)
if ext == "" {
return limitedName
} else {
return fmt.Sprintf("%s.%s", limitedName, ext)
}
}

// FilePath gen valid file path
Expand All @@ -106,10 +111,12 @@ func FilePath(name, ext string, escape bool) (string, error) {
return "", err
}
}
fileName := fmt.Sprintf("%s.%s", name, ext)
var fileName string
if escape {
fileName = FileName(fileName)
}
fileName = FileName(name, ext)
} else {
fileName = fmt.Sprintf("%s.%s", name, ext)
}
outputPath = filepath.Join(config.OutputPath, fileName)
return outputPath, nil
}
Expand Down
2 changes: 1 addition & 1 deletion utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ func TestFileName(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := FileName(tt.args.name); got != tt.want {
if got := FileName(tt.args.name, ""); got != tt.want {
t.Errorf("FileName() = %v, want %v", got, tt.want)
}
})
Expand Down

0 comments on commit 795edc2

Please sign in to comment.