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"
@@ -629,24 +630,61 @@ func videosHandler(c echo.Context) error {
} }
type VideoTemplate struct { type VideoTemplate struct {
Source string Source string
Width uint Width uint
Height uint Height uint
FPS string FPS string
Size string Size string
Filename string Filename string
StreamRate string DownloadFilename string
StreamRate string
TempURL TempURL
} }
type AudioTemplate struct { type AudioTemplate struct {
Kbps string Kbps string
Size string Size string
Filename string Filename string
StreamRate string DownloadFilename 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
@@ -678,14 +716,15 @@ func videoHandler(c echo.Context) error {
rate := float64(video.Size) / video.Length rate := float64(video.Size) / video.Length
videoURLs = append(videoURLs, VideoTemplate{ videoURLs = append(videoURLs, VideoTemplate{
Source: video.Source, Source: video.Source,
Width: video.Width, Width: video.Width,
Height: video.Height, Height: video.Height,
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,
StreamRate: fmt.Sprintf("%.1f KiB/s", rate/1024), DownloadFilename: makeNiceFilename(orig.Title),
TempURL: tempURL, StreamRate: fmt.Sprintf("%.1f KiB/s", rate/1024),
TempURL: tempURL,
}) })
} }
for _, audio := range audios { for _, audio := range audios {
@@ -698,11 +737,12 @@ func videoHandler(c echo.Context) error {
rate := float64(audio.Size) / audio.Length rate := float64(audio.Size) / audio.Length
audioURLs = append(audioURLs, AudioTemplate{ audioURLs = append(audioURLs, AudioTemplate{
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,
StreamRate: fmt.Sprintf("%.1f KiB/s", rate/1024), DownloadFilename: makeNiceFilename(orig.Title),
TempURL: tempURL, StreamRate: fmt.Sprintf("%.1f KiB/s", rate/1024),
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}}