From e7a73e08594b3074dfc55805a67202bd9805806c Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Tue, 9 Feb 2021 17:50:51 +0000 Subject: [PATCH] Put only one author on the book file name - Closes: #64 --- lib/template.go | 16 ++++++++++++++-- lib/trantor.go | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/template.go b/lib/template.go index 059b624..9f6a276 100644 --- a/lib/template.go +++ b/lib/template.go @@ -93,9 +93,21 @@ func stringsJoin(strs []string) string { return strings.Join(strs, ", ") } +func bookFileName(book database.Book) string { + var authorsStr string + switch len(book.Authors) { + case 0: + authorsStr = "" + case 1: + authorsStr = book.Authors[0] + " - " + default: + authorsStr = book.Authors[0] + ", ... - " + } + return url.PathEscape(fmt.Sprintf("%s%s.epub", authorsStr, book.Title)) +} + func downloadUrl(book database.Book) string { - fileName := url.PathEscape(fmt.Sprintf("%s - %s.epub", strings.Join(book.Authors, ", "), book.Title)) - return fmt.Sprintf("/download/%s/%s", book.ID, fileName) + return fmt.Sprintf("/download/%s/%s", book.ID, bookFileName(book)) } func size2mb(size int) float32 { diff --git a/lib/trantor.go b/lib/trantor.go index 583b409..24b34e5 100644 --- a/lib/trantor.go +++ b/lib/trantor.go @@ -113,7 +113,7 @@ func downloadHandler(h handler) { defer f.Close() addCacheControlHeader(h.w, false) - http.ServeContent(h.w, h.r, fmt.Sprintf("%s - %s.epub", strings.Join(book.Authors, ", "), book.Title), book.UploadDate, f) + http.ServeContent(h.w, h.r, bookFileName(book), book.UploadDate, f) } type indexData struct {