From 92e4454bca759f34385fcdb3a79f6d8feef2b7f1 Mon Sep 17 00:00:00 2001 From: rroller Date: Fri, 15 Nov 2024 21:25:41 -0800 Subject: [PATCH] Handle input with new lines --- src/media/fetch.go | 7 ++++--- src/utils/urls.go | 16 ++++++++++++---- src/utils/urls_test.go | 2 ++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/media/fetch.go b/src/media/fetch.go index fc3f8cd..b207ba5 100644 --- a/src/media/fetch.go +++ b/src/media/fetch.go @@ -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 diff --git a/src/utils/urls.go b/src/utils/urls.go index cd32f31..9e749ed 100644 --- a/src/utils/urls.go +++ b/src/utils/urls.go @@ -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 + } } } diff --git a/src/utils/urls_test.go b/src/utils/urls_test.go index 8f38445..e5a4da4 100644 --- a/src/utils/urls_test.go +++ b/src/utils/urls_test.go @@ -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) {