mirror of
https://github.com/aljazceru/njump.git
synced 2025-12-17 14:24:27 +01:00
shorten URLs when rendering text image so they don't use all the space.
This commit is contained in:
@@ -8,6 +8,7 @@ import (
|
|||||||
"image/draw"
|
"image/draw"
|
||||||
"math"
|
"math"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -256,3 +257,39 @@ func lookupScript(r rune) int {
|
|||||||
}
|
}
|
||||||
return 0 // unknown
|
return 0 // unknown
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// shortenURLs takes a text content and returns the same content, but with all big URLs like https://image.nostr.build/0993112ab590e04b978ad32002005d42c289d43ea70d03dafe9ee99883fb7755.jpg#m=image%2Fjpeg&dim=1361x1148&blurhash=%3B7Jjhw00.l.QEh%3FuIA-pMe00%7EVjXX8x%5DE2xuSgtQcr%5E%2500%3FHxD%24%25%25Ms%2Bt%2B-%3BVZK59a%252MyD%2BV%5BI.8%7Ds%3B%25Lso-oi%5ENINHnjI%3BR*%3DdM%7BX7%25MIUtksn%24LM%7BMySeR%25R*%251M%7DRkv%23RjtjS%239as%3AxDnO%251&x=61be75a3e3e0cc88e7f0e625725d66923fdd777b3b691a1c7072ba494aef188d shortened to something like https://image.nostr.build/.../...7755.jpg
|
||||||
|
func shortenURLs(text string) string {
|
||||||
|
return urlMatcher.ReplaceAllStringFunc(text, func(match string) string {
|
||||||
|
if len(match) < 50 {
|
||||||
|
return match
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed, err := url.Parse(match)
|
||||||
|
if err != nil {
|
||||||
|
return match
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed.Fragment = ""
|
||||||
|
|
||||||
|
if len(parsed.RawQuery) > 10 {
|
||||||
|
parsed.RawQuery = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
pathParts := strings.Split(parsed.Path, "/")
|
||||||
|
nParts := len(pathParts)
|
||||||
|
lastPart := pathParts[nParts-1]
|
||||||
|
if len(lastPart) > 12 {
|
||||||
|
pathParts[nParts-1] = "…" + lastPart[len(lastPart)-11:]
|
||||||
|
}
|
||||||
|
if nParts > 2 {
|
||||||
|
pathParts[1] = "…"
|
||||||
|
pathParts[2] = pathParts[nParts-1]
|
||||||
|
pathParts = pathParts[0:2]
|
||||||
|
}
|
||||||
|
|
||||||
|
parsed.Path = "/////"
|
||||||
|
urlStr := parsed.String()
|
||||||
|
return strings.Replace(urlStr, "/////", strings.Join(pathParts, "/"), 1)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ func renderImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
content := strings.Replace(data.event.Content, "\n\n\n\n", "\n\n", -1)
|
content := strings.Replace(data.event.Content, "\n\n\n\n", "\n\n", -1)
|
||||||
content = strings.Replace(data.event.Content, "\n\n\n", "\n\n", -1)
|
content = strings.Replace(data.event.Content, "\n\n\n", "\n\n", -1)
|
||||||
|
content = shortenURLs(content)
|
||||||
|
|
||||||
// this turns the raw event.Content into a series of lines ready to drawn
|
// this turns the raw event.Content into a series of lines ready to drawn
|
||||||
paragraphs := replaceUserReferencesWithNames(r.Context(),
|
paragraphs := replaceUserReferencesWithNames(r.Context(),
|
||||||
@@ -72,7 +73,6 @@ func renderImage(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
if err := png.Encode(w, img); err != nil {
|
if err := png.Encode(w, img); err != nil {
|
||||||
log.Printf("error encoding image: %s", err)
|
log.Printf("error encoding image: %s", err)
|
||||||
http.Error(w, "error encoding image!", 500)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user