Handle input with new lines

This commit is contained in:
rroller 2024-11-15 21:25:41 -08:00
parent bdcbc8ddbb
commit 92e4454bca
3 changed files with 18 additions and 7 deletions

View file

@ -90,12 +90,13 @@ func getUrl(r *http.Request) string {
return strings.TrimSpace(r.URL.Query().Get("url"))
}
func getMediaResults(url string) ([]Media, string, error) {
if url == "" {
func getMediaResults(inputUrl string) ([]Media, string, error) {
if inputUrl == "" {
return nil, "", errors.New("missing URL")
}
url = utils.NormalizeUrl(url)
url := utils.NormalizeUrl(inputUrl)
log.Info().Msgf("Got input '%s' and extracted '%s'", inputUrl, url)
// NOTE: This system is for a simple use case, meant to run at home. This is not a great design for a robust system.
// We are hashing the URL here and writing files to disk to a consistent directory based on the ID. You can imagine

View file

@ -1,16 +1,24 @@
package utils
import "strings"
import (
"bufio"
"strings"
)
func NormalizeUrl(url string) string {
url = strings.TrimSpace(url)
parts := strings.Split(url, " ")
// Find the first URL. Will split the string by spaces and new lines and return the first thing that looks like a URL
// TODO: We could try to parse the url, but will save that for later
for _, part := range parts {
// Take the firs string that looks like a URL.
// TODO: We could try to parse the url, but will save that for later
if strings.HasPrefix(part, "http") || strings.HasPrefix(part, "www") {
return part
sc := bufio.NewScanner(strings.NewReader(part))
for sc.Scan() {
p := sc.Text()
if strings.HasPrefix(p, "http") || strings.HasPrefix(p, "www") {
return p
}
}
}

View file

@ -10,6 +10,8 @@ func TestNormalizeUrl(t *testing.T) {
{url: "example.com", want: "example.com"},
{url: "https://example.com", want: "https://example.com"},
{url: "https://example.com this is an example", want: "https://example.com"},
{url: "https://example.com\nthis is an example", want: "https://example.com"},
{url: "Downloading https://example.com\nthis is an example", want: "https://example.com"},
}
for _, tt := range tests {
t.Run(tt.url, func(t *testing.T) {