provide better download name

This commit is contained in:
Carl Pearson
2024-09-16 05:44:58 -06:00
parent 71abe246e2
commit 62ed290570
3 changed files with 67 additions and 27 deletions

View File

@@ -50,5 +50,5 @@ Build and push this container to ghcr
- [ ] edit original metadata - [ ] edit original metadata
- [ ] Download playlists - [ ] Download playlists
- [ ] change from Audio -> Video - [ ] change from Audio -> Video
- [ ] Provide a better name for downloaded files - [x] Provide a better name for downloaded files
- [x] Environment variable to control whether "Secure" flag set on cookie - [x] Environment variable to control whether "Secure" flag set on cookie

View File

@@ -7,6 +7,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"regexp"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@@ -635,6 +636,7 @@ type VideoTemplate struct {
FPS string FPS string
Size string Size string
Filename string Filename string
DownloadFilename string
StreamRate string StreamRate string
TempURL TempURL
} }
@@ -643,10 +645,46 @@ type AudioTemplate struct {
Kbps string Kbps string
Size string Size string
Filename string Filename string
DownloadFilename string
StreamRate string StreamRate string
TempURL TempURL
} }
func makeNiceFilename(input string) string {
// Convert to lowercase
input = strings.ToLower(input)
// Replace spaces with underscores
input = strings.ReplaceAll(input, " ", "_")
// Replace common special characters
replacements := map[string]string{
"æ": "ae", "ø": "oe", "å": "aa", "ä": "ae", "ö": "oe", "ü": "ue",
"ß": "ss", "ñ": "n", "ç": "c", "œ": "oe",
}
for old, new := range replacements {
input = strings.ReplaceAll(input, old, new)
}
// Remove any remaining non-alphanumeric characters (except underscores)
reg := regexp.MustCompile("[^a-z0-9_\\.]+")
input = reg.ReplaceAllString(input, "")
// Trim leading/trailing underscores
input = strings.Trim(input, "_")
// Collapse multiple underscores into a single one
reg = regexp.MustCompile("_+")
input = reg.ReplaceAllString(input, "_")
// Ensure the filename is not empty
if input == "" {
input = "unnamed_file"
}
return input
}
func videoHandler(c echo.Context) error { func videoHandler(c echo.Context) error {
id, _ := strconv.Atoi(c.Param("id")) id, _ := strconv.Atoi(c.Param("id"))
var orig Original var orig Original
@@ -684,6 +722,7 @@ func videoHandler(c echo.Context) error {
FPS: fmt.Sprintf("%.1f", video.FPS), FPS: fmt.Sprintf("%.1f", video.FPS),
Size: humanSize(video.Size), Size: humanSize(video.Size),
Filename: video.Filename, Filename: video.Filename,
DownloadFilename: makeNiceFilename(orig.Title),
StreamRate: fmt.Sprintf("%.1f KiB/s", rate/1024), StreamRate: fmt.Sprintf("%.1f KiB/s", rate/1024),
TempURL: tempURL, TempURL: tempURL,
}) })
@@ -701,6 +740,7 @@ func videoHandler(c echo.Context) error {
Kbps: fmt.Sprintf("%.1f kbps", kbps), Kbps: fmt.Sprintf("%.1f kbps", kbps),
Size: humanSize(audio.Size), Size: humanSize(audio.Size),
Filename: audio.Filename, Filename: audio.Filename,
DownloadFilename: makeNiceFilename(orig.Title),
StreamRate: fmt.Sprintf("%.1f KiB/s", rate/1024), StreamRate: fmt.Sprintf("%.1f KiB/s", rate/1024),
TempURL: tempURL, TempURL: tempURL,
}) })

View File

@@ -54,7 +54,7 @@
</video> </video>
</div> </div>
<div class="video-download"> <div class="video-download">
<a href="/data/{{.Filename}}" download>Download ({{.Size}}, {{.StreamRate}})</a> <a href="/data/{{.Filename}}" download="{{.DownloadFilename}}">Download ({{.Size}}, {{.StreamRate}})</a>
</div> </div>
{{end}} {{end}}
@@ -67,7 +67,7 @@
</audio> </audio>
</div> </div>
<div class="audio-download"> <div class="audio-download">
<a href="/data/{{.Filename}}" download>Download ({{.Size}}, {{.StreamRate}})</a> <a href="/data/{{.Filename}}" download="{{.DownloadFilename}}">Download ({{.Size}}, {{.StreamRate}})</a>
</div> </div>
{{end}} {{end}}