diff --git a/public/link.png b/public/link.png index cd1ddb3..0daeb9c 100644 Binary files a/public/link.png and b/public/link.png differ diff --git a/public/photo.png b/public/photo.png new file mode 100644 index 0000000..96eec15 Binary files /dev/null and b/public/photo.png differ diff --git a/public/style.css b/public/style.css index 0be05bb..25c7730 100644 --- a/public/style.css +++ b/public/style.css @@ -38,7 +38,7 @@ summary { height: 52px; width: 70px; background-repeat: no-repeat; - background-size: cover; + background-size: contain; background-position: center; } .rank { @@ -809,10 +809,14 @@ nav .host { font-size: 12px; } -nav .spacer { +nav div.spacer { height: 32px; position: relative; } +nav span.spacer { + height: 20px; + display: inline-block; +} nav .space a { z-index: 100; } diff --git a/public/text.png b/public/text.png index 564a031..4dc1246 100644 Binary files a/public/text.png and b/public/text.png differ diff --git a/routes.go b/routes.go index b7fa609..1dfaca9 100644 --- a/routes.go +++ b/routes.go @@ -25,6 +25,21 @@ import ( "golang.org/x/text/message" ) +var pictrs = regexp.MustCompile(`\/pictrs\/image\/([a-z0-9\-]+)\.([a-z]+)$`) +var imgur = regexp.MustCompile(`^https:\/\/(i\.)?imgur.com\/([a-zA-Z0-9]{5,})(\.[a-zA-Z0-9]+)?`) + +var isImage = func(u string) bool { + p, err := url.Parse(u) + if err != nil || p.Path == "" { + return false + } + ext := filepath.Ext(p.Path) + if ext == ".jpeg" || ext == ".jpg" || ext == ".png" || ext == ".webp" || ext == ".gif" { + return true + } + return false +} + var funcMap = template.FuncMap{ "host": func(host string) string { if l := os.Getenv("LEMMY_DOMAIN"); l != "" { @@ -112,38 +127,61 @@ var funcMap = template.FuncMap{ } return "" }, - "isImage": func(u string) bool { - p, err := url.Parse(u) - if err != nil || p.Path == "" { - return false - } - ext := filepath.Ext(p.Path) - if ext == ".jpeg" || ext == ".jpg" || ext == ".png" || ext == ".webp" || ext == ".gif" { - return true - } - return false - }, + "isImage": isImage, "isYoutube": func(u string) bool { re := regexp.MustCompile(`^((?:https?:)?\/\/)?((?:www|m)\.)?((?:youtube(-nocookie)?\.com|youtu.be))(\/(?:[\w\-]+\?v=|embed\/|live\/|v\/)?)([\w\-]+)(\S+)?$`) return re.MatchString(u) }, "thumbnail": func(p lemmy.Post) string { + if pictrs.MatchString(p.ThumbnailURL.String()) { + return p.ThumbnailURL.String() + "?format=jpg&thumbnail=64" + } else if pictrs.MatchString(p.URL.String()) { + return p.URL.String() + "?format=jpg&thumbnail=64" + } + if imgur.MatchString(p.URL.String()) { + return imgur.ReplaceAllString(p.URL.String(), "https://i.imgur.com/${2}s.jpg") + } if p.ThumbnailURL.IsValid() { - return p.ThumbnailURL.String() + "?format=jpg&thumbnail=96" + return p.ThumbnailURL.String() } - re := regexp.MustCompile(`\/pictrs\/image\/([a-z0-9\-]+)\.([a-z]+)$`) - if re.MatchString(p.URL.String()) { - return p.URL.String() + "?format=jpg&thumbnail=96" - } - re = regexp.MustCompile(`^https:\/\/(i\.)?imgur.com\/([a-zA-Z0-9]{5,})(\.[a-zA-Z0-9]+)?`) - if re.MatchString(p.URL.String()) { - return re.ReplaceAllString(p.URL.String(), "https://i.imgur.com/${2}s.jpg") + if isImage(p.URL.String()) { + return "/_/static/photo.png" } if p.URL.IsValid() { return "/_/static/link.png" } return "/_/static/text.png" }, + "banner": func(site lemmy.Site) string { + bannerURL := "" + if site.Banner.IsValid() { + bannerURL = site.Banner.String() + } else if site.Icon.IsValid() { + bannerURL = site.Icon.String() + } + if pictrs.MatchString(bannerURL) { + return bannerURL + "?format=jpg&thumbnail=300" + } + return bannerURL + }, + "cbanner": func(c lemmy.Community) string { + bannerURL := "" + if c.Banner.IsValid() { + bannerURL = c.Banner.String() + } else if c.Icon.IsValid() { + bannerURL = c.Icon.String() + } + if pictrs.MatchString(bannerURL) { + return bannerURL + "?format=jpg&thumbnail=300" + } + return bannerURL + }, + "shrink": func(u string) string { + if pictrs.MatchString(u) { + return u + "?format=png&thumbnail=60" + } + return u + }, "humanize": humanize.Time, "markdown": func(host string, body string) template.HTML { var buf bytes.Buffer @@ -452,7 +490,11 @@ func GetIcon(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { w.Write([]byte("404 - Not Found")) return } - iresp, err := state.HTTPClient.Get(resp.SiteView.Site.Icon.String()) + u := resp.SiteView.Site.Icon.String() + if pictrs.MatchString(u) { + u += "?format=jpg&thumbnail=60" + } + iresp, err := state.HTTPClient.Get(u) if err != nil { fmt.Println(err) w.WriteHeader(http.StatusInternalServerError) diff --git a/templates/login.html b/templates/login.html index c2437a0..7ce4439 100644 --- a/templates/login.html +++ b/templates/login.html @@ -24,10 +24,11 @@
- {{ host .Host}} - sign up or login + {{ host .Host}} - sign up or login + {{ if .Alert }}