Put only one author on the book file name

- Closes: #64
This commit is contained in:
Las Zenow 2021-02-09 17:50:51 +00:00
parent a4b11142b2
commit e7a73e0859
2 changed files with 15 additions and 3 deletions

View file

@ -93,9 +93,21 @@ func stringsJoin(strs []string) string {
return strings.Join(strs, ", ") 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 { 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, bookFileName(book))
return fmt.Sprintf("/download/%s/%s", book.ID, fileName)
} }
func size2mb(size int) float32 { func size2mb(size int) float32 {

View file

@ -113,7 +113,7 @@ func downloadHandler(h handler) {
defer f.Close() defer f.Close()
addCacheControlHeader(h.w, false) 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 { type indexData struct {