BlueGit

timfa bluegit

Files

bluegit / internal / server / social.go

social.go

fix: gebruik CommitFileChanges voor de linkkaart
timfa project_87923614_bot_3aff574f6f98414b945a8799374f9092 · pushed by timfa · 3 weeks ago
ece85aa0 History
social.go 19.1 KiB Download Raw
1package server
2
3import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "html"
9 "image"
10 _ "image/gif"
11 _ "image/jpeg"
12 "image/png"
13 "net/http"
14 "net/url"
15 "os"
16 "strconv"
17 "strings"
18
19 "gitlab.com/Timfa/bluegit/internal/card"
20 "gitlab.com/Timfa/bluegit/internal/gitrepo"
21 "gitlab.com/Timfa/bluegit/internal/store"
22)
23
24func (s *Server) cardCache() *card.Cache {
25 if s.cards == nil {
26 s.cards = card.NewCache(128)
27 }
28 return s.cards
29}
30
31func (s *Server) absURL(path string) string {
32 base := strings.TrimRight(s.Cfg.BaseURL, "/")
33 if path == "" {
34 path = "/"
35 }
36 if !strings.HasPrefix(path, "/") {
37 path = "/" + path
38 }
39 return base + path
40}
41
42func (s *Server) lockedRender() card.Render {
43 return card.Locked(s.absURL("/"), s.absURL("/-/card.png?locked=1"), s.absURL("/api/v1/oembed?locked=1"))
44}
45
46// cardForPath bouwt de kaart als anonieme bezoeker. De sessie wordt niet gelezen.
47func (s *Server) cardForPath(ctx context.Context, path, rawQuery, fragment string) card.Render {
48 path = "/" + strings.Trim(strings.TrimSpace(path), "/")
49 if path == "/" {
50 return s.siteRender(path, rawQuery)
51 }
52 page := s.absURL(path)
53 if rawQuery != "" {
54 page += "?" + rawQuery
55 }
56 img := s.imageFor(path, rawQuery, fragment)
57 oem := s.oembedFor(page)
58 if s.Store == nil {
59 return card.Site(page, img, oem)
60 }
61 if strings.HasPrefix(path, "/groups/") {
62 return s.groupCard(ctx, strings.TrimPrefix(path, "/groups/"), page, img, oem)
63 }
64 if strings.HasPrefix(path, "/users/") {
65 name := strings.Trim(strings.TrimPrefix(path, "/users/"), "/")
66 if i := strings.Index(name, "/"); i >= 0 {
67 name = name[:i]
68 }
69 return s.userCard(ctx, name, page, img, oem)
70 }
71 parts := strings.Split(strings.Trim(path, "/"), "/")
72 if len(parts) < 2 || strings.HasPrefix(parts[0], "-") {
73 return card.Site(page, img, oem)
74 }
75 repo, err := s.Store.RepoByPath(ctx, parts[0], strings.TrimSuffix(parts[1], ".git"))
76 if err != nil {
77 return card.Site(page, img, oem)
78 }
79 if repo.Visibility != "public" {
80 return s.lockedRender()
81 }
82 q, _ := url.ParseQuery(rawQuery)
83 return s.publicRepoCard(ctx, repo, parts[2:], q, fragment, page, img, oem)
84}
85
86func (s *Server) siteRender(path, rawQuery string) card.Render {
87 page := s.absURL(path)
88 if rawQuery != "" {
89 page += "?" + rawQuery
90 }
91 return card.Site(page, s.imageFor(path, rawQuery, ""), s.oembedFor(page))
92}
93
94func (s *Server) imageFor(path, rawQuery, fragment string) string {
95 v := url.Values{}
96 v.Set("path", path)
97 q, _ := url.ParseQuery(rawQuery)
98 if l := q.Get("L"); l != "" {
99 v.Set("L", l)
100 } else if _, _, ok := card.ParseLines(fragment); ok {
101 v.Set("L", strings.TrimPrefix(strings.TrimSpace(fragment), "#"))
102 }
103 if rev := q.Get("rev"); rev != "" {
104 v.Set("rev", rev)
105 }
106 return s.absURL("/-/card.png?" + v.Encode())
107}
108
109func (s *Server) oembedFor(page string) string {
110 return s.absURL("/api/v1/oembed?url=" + url.QueryEscape(page))
111}
112
113func (s *Server) publicRepoCard(ctx context.Context, repo store.Repo, rest []string, q url.Values, fragment, page, img, oem string) card.Render {
114 kind := ""
115 if len(rest) >= 2 && rest[0] == "-" {
116 kind = rest[1]
117 }
118 switch kind {
119 case "blob":
120 return s.fileCard(ctx, repo, strings.Join(rest[2:], "/"), q, fragment, page, img, oem)
121 case "tree":
122 return s.repoCard(ctx, repo, page, img, oem)
123 case "commit":
124 if len(rest) >= 3 {
125 return s.commitCard(ctx, repo, rest[2], page, img, oem)
126 }
127 case "merge-requests":
128 if len(rest) >= 3 && rest[2] != "new" {
129 if n, err := strconv.ParseInt(rest[2], 10, 64); err == nil {
130 return s.mrCard(ctx, repo, n, page, img, oem)
131 }
132 }
133 case "issues":
134 if len(rest) >= 3 {
135 if n, err := strconv.ParseInt(rest[2], 10, 64); err == nil && n > 0 {
136 return s.issueCard(ctx, repo, n, page, img, oem)
137 }
138 }
139 case "pipelines":
140 if len(rest) >= 3 {
141 if n, err := strconv.ParseInt(rest[2], 10, 64); err == nil && n > 0 {
142 return s.pipeCard(ctx, repo, n, page, img, oem)
143 }
144 }
145 case "releases":
146 return s.releaseCard(ctx, repo, page, img, oem)
147 }
148 return s.repoCard(ctx, repo, page, img, oem)
149}
150
151func (s *Server) repoCard(ctx context.Context, repo store.Repo, page, img, oem string) card.Render {
152 prose := card.Excerpt(repo.Description, 180)
153 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
154 rev := repo.DefaultBranch
155 if prose == "" && s.StoragePath() != "" {
156 if entries, err := gitrepo.Tree(ctx, s.Git, dir, rev, ""); err == nil {
157 readme, _ := findDocs(entries)
158 if readme != "" {
159 if text, binary, err := gitrepo.Blob(ctx, s.Git, dir, rev, readme, 80_000); err == nil && !binary {
160 prose = card.Excerpt(text, 180)
161 }
162 }
163 }
164 }
165 var facts []string
166 if lang := s.repoLanguage(ctx, dir, rev); lang != "" {
167 facts = append(facts, lang)
168 }
169 if counts, _, err := s.Store.StarInfo(ctx, 0, []int64{repo.ID}); err == nil {
170 facts = append(facts, starLabel(counts[repo.ID]))
171 }
172 if s.StoragePath() != "" {
173 if c, err := gitrepo.LastCommit(ctx, s.Git, dir, rev, ""); err == nil && c.SHA != "" {
174 short := c.SHA
175 if len(short) > 8 {
176 short = short[:8]
177 }
178 facts = append(facts, short+" "+card.Clip(c.Subject, 72))
179 }
180 }
181 return card.Render{Meta: card.Meta{
182 Title: repo.OwnerPath + "/" + repo.Name, Description: prose, URL: page, Image: img, OEmbed: oem,
183 Theme: "#1f6feb", Kind: "repository", Facts: facts, Initial: repo.Name,
184 }, Avatar: s.avatarForRepo(ctx, repo)}
185}
186
187func (s *Server) fileCard(ctx context.Context, repo store.Repo, rel string, q url.Values, fragment, page, img, oem string) card.Render {
188 rel, err := cleanRel(rel)
189 base := s.repoCard(ctx, repo, page, img, oem)
190 if err != nil || rel == "" || s.StoragePath() == "" {
191 return base
192 }
193 rev := safeRev(q.Get("rev"), repo.DefaultBranch)
194 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
195 text, binary, err := gitrepo.Blob(ctx, s.Git, dir, rev, rel, 80_000)
196 name := rel
197 if i := strings.LastIndex(rel, "/"); i >= 0 {
198 name = rel[i+1:]
199 }
200 from, to, ranged := lineBounds(q, fragment)
201 desc := "Binary file"
202 if err == nil && !binary {
203 desc = card.Snippet(text, from, to, ranged)
204 }
205 facts := []string{repo.OwnerPath + "/" + repo.Name}
206 if lang := langByName(name); lang != "" {
207 facts = append(facts, lang)
208 }
209 if ranged {
210 if from == to {
211 facts = append(facts, fmt.Sprintf("L%d", from))
212 } else {
213 facts = append(facts, fmt.Sprintf("L%d-%d", from, to))
214 }
215 }
216 return card.Render{Meta: card.Meta{
217 Title: name, Description: desc, URL: page, Image: img, OEmbed: oem,
218 Theme: "#1f6feb", Kind: "file", Facts: facts, Initial: name,
219 }, Avatar: base.Avatar}
220}
221
222func (s *Server) commitCard(ctx context.Context, repo store.Repo, sha, page, img, oem string) card.Render {
223 base := s.repoCard(ctx, repo, page, img, oem)
224 if !safeSHA(sha) || s.StoragePath() == "" {
225 return base
226 }
227 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
228 c, err := gitrepo.Show(ctx, s.Git, dir, sha)
229 if err != nil {
230 return base
231 }
232 short := c.SHA
233 if len(short) > 8 {
234 short = short[:8]
235 }
236 facts := []string{c.Author, short}
237 if changes, err := gitrepo.CommitFileChanges(ctx, s.Git, dir, c.SHA); err == nil {
238 add, del := 0, 0
239 for _, ch := range changes {
240 add += ch.Added
241 del += ch.Deleted
242 }
243 facts = append(facts, fmt.Sprintf("+%d −%d", add, del), fmt.Sprintf("%d files", len(changes)))
244 }
245 return card.Render{Meta: card.Meta{
246 Title: card.Clip(c.Subject, 90), Description: card.Excerpt(c.Body, 180), URL: page, Image: img, OEmbed: oem,
247 Theme: "#0c3f86", Kind: "commit", Facts: facts, Initial: c.Author,
248 }, Avatar: base.Avatar}
249}
250
251func (s *Server) mrCard(ctx context.Context, repo store.Repo, number int64, page, img, oem string) card.Render {
252 base := s.repoCard(ctx, repo, page, img, oem)
253 mr, err := s.Store.MergeRequestByNumber(ctx, repo.ID, number)
254 if err != nil {
255 return base
256 }
257 status := mr.Status
258 if mr.Draft && status == "open" {
259 status = "draft"
260 }
261 facts := []string{status, mr.SourceBranch + " → " + mr.TargetBranch, mr.Author}
262 if s.StoragePath() != "" {
263 dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name)
264 if changes, err := gitrepo.CompareChanges(ctx, s.Git, dir, mr.TargetBranch, mr.SourceBranch); err == nil {
265 add, del := 0, 0
266 for _, ch := range changes {
267 add += ch.Added
268 del += ch.Deleted
269 }
270 facts = append(facts, fmt.Sprintf("+%d −%d", add, del))
271 }
272 }
273 if p, err := s.Store.LatestMergePipeline(ctx, repo.ID, mr.ID); err == nil {
274 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
275 facts = append(facts, displayStatus(p.Status, jobs))
276 }
277 theme := "#1f6feb"
278 switch status {
279 case "merged":
280 theme = "#146c43"
281 case "closed":
282 theme = "#5c6b80"
283 }
284 return card.Render{Meta: card.Meta{
285 Title: "!" + strconv.FormatInt(mr.Number, 10) + " " + mr.Title,
286 Description: card.Excerpt(mr.Description, 180), URL: page, Image: img, OEmbed: oem,
287 Theme: theme, Kind: "merge_request", Facts: facts, Initial: mr.Author,
288 }, Avatar: base.Avatar}
289}
290
291func (s *Server) issueCard(ctx context.Context, repo store.Repo, number int64, page, img, oem string) card.Render {
292 iss, err := s.Store.IssueByNumber(ctx, repo.ID, number)
293 if err != nil {
294 return s.repoCard(ctx, repo, page, img, oem)
295 }
296 if iss.Confidential {
297 return s.lockedRender()
298 }
299 var labels []string
300 for _, l := range iss.Labels {
301 labels = append(labels, l.Name)
302 }
303 facts := []string{iss.Status}
304 if len(labels) > 0 {
305 facts = append(facts, strings.Join(labels, ", "))
306 }
307 facts = append(facts, iss.Author)
308 return card.Render{Meta: card.Meta{
309 Title: "#" + strconv.FormatInt(iss.Number, 10) + " " + iss.Title,
310 Description: card.Excerpt(iss.Description, 180), URL: page, Image: img, OEmbed: oem,
311 Theme: "#9a6700", Kind: "issue", Facts: facts, Initial: iss.Author,
312 }, Avatar: s.avatarForRepo(ctx, repo)}
313}
314
315func (s *Server) pipeCard(ctx context.Context, repo store.Repo, id int64, page, img, oem string) card.Render {
316 base := s.repoCard(ctx, repo, page, img, oem)
317 p, err := s.Store.PipelineByID(ctx, repo.ID, id)
318 if err != nil {
319 return base
320 }
321 jobs, _ := s.Store.JobsByPipeline(ctx, p.ID)
322 status := displayStatus(p.Status, jobs)
323 theme := "#1f6feb"
324 switch status {
325 case "passed":
326 theme = "#146c43"
327 case "failed":
328 theme = "#9f2d2d"
329 case "running":
330 theme = "#9a6700"
331 }
332 facts := []string{status}
333 if p.Ref != "" {
334 facts = append(facts, strings.TrimPrefix(p.Ref, "refs/heads/"))
335 }
336 if p.CommitTitle != "" {
337 facts = append(facts, card.Clip(p.CommitTitle, 72))
338 }
339 if p.Actor != "" {
340 facts = append(facts, p.Actor)
341 }
342 return card.Render{Meta: card.Meta{
343 Title: "#" + strconv.FormatInt(p.ID, 10) + " " + status, Description: card.Excerpt(p.CommitTitle, 180),
344 URL: page, Image: img, OEmbed: oem, Theme: theme, Kind: "pipeline", Facts: facts, Initial: repo.Name,
345 }, Avatar: base.Avatar}
346}
347
348func (s *Server) releaseCard(ctx context.Context, repo store.Repo, page, img, oem string) card.Render {
349 base := s.repoCard(ctx, repo, page, img, oem)
350 list, err := s.Store.ListReleases(ctx, repo.ID)
351 if err != nil || len(list) == 0 {
352 return card.Render{Meta: card.Meta{
353 Title: "Releases", Description: repo.OwnerPath + "/" + repo.Name, URL: page, Image: img, OEmbed: oem,
354 Theme: "#146c43", Kind: "release", Initial: repo.Name,
355 }, Avatar: base.Avatar}
356 }
357 rel := list[0]
358 title := rel.Tag
359 if rel.Name != "" {
360 title = rel.Tag + " · " + rel.Name
361 }
362 return card.Render{Meta: card.Meta{
363 Title: title, Description: card.Excerpt(rel.Body, 180), URL: page, Image: img, OEmbed: oem,
364 Theme: "#146c43", Kind: "release", Facts: []string{repo.OwnerPath + "/" + repo.Name, rel.Tag}, Initial: repo.Name,
365 }, Avatar: base.Avatar}
366}
367
368func (s *Server) groupCard(ctx context.Context, rest, page, img, oem string) card.Render {
369 name := rest
370 if i := strings.Index(name, "/"); i >= 0 {
371 name = name[:i]
372 }
373 g, err := s.Store.GroupByPath(ctx, name)
374 if err != nil {
375 return card.Site(page, img, oem)
376 }
377 if g.Visibility != "public" {
378 return s.lockedRender()
379 }
380 n, _ := s.Store.CountRepos(ctx, "group", g.ID, true)
381 return card.Render{Meta: card.Meta{
382 Title: g.Name, Description: card.Excerpt(g.Description, 180), URL: page, Image: img, OEmbed: oem,
383 Theme: "#6f42c1", Kind: "group", Facts: []string{repoLabel(n)}, Initial: g.Name,
384 }, Avatar: s.readLogo("group", g.ID, g.LogoMIME)}
385}
386
387func (s *Server) userCard(ctx context.Context, name, page, img, oem string) card.Render {
388 u, err := s.Store.UserByUsername(ctx, name)
389 if err != nil {
390 return card.Site(page, img, oem)
391 }
392 n, _ := s.Store.CountRepos(ctx, "user", u.ID, true)
393 return card.Render{Meta: card.Meta{
394 Title: u.Username, Description: "Public profile", URL: page, Image: img, OEmbed: oem,
395 Theme: "#1f6feb", Kind: "user", Facts: []string{repoLabel(n)}, Initial: u.Username,
396 }}
397}
398
399func (s *Server) avatarForRepo(ctx context.Context, repo store.Repo) []byte {
400 if b := s.readLogo("repo", repo.ID, repo.LogoMIME); len(b) > 0 {
401 return b
402 }
403 if repo.OwnerKind != "group" {
404 return nil
405 }
406 g, err := s.Store.GroupByID(ctx, repo.OwnerID)
407 if err != nil {
408 return nil
409 }
410 return s.readLogo("group", g.ID, g.LogoMIME)
411}
412
413func (s *Server) readLogo(kind string, id int64, mime string) []byte {
414 if mime != "image/png" && mime != "image/jpeg" && mime != "image/gif" {
415 return nil
416 }
417 path, err := s.logoPath(kind, id)
418 if err != nil {
419 return nil
420 }
421 data, err := os.ReadFile(path)
422 if err != nil {
423 return nil
424 }
425 img, _, err := image.Decode(bytes.NewReader(data))
426 if err != nil {
427 return nil
428 }
429 var buf bytes.Buffer
430 if err := png.Encode(&buf, img); err != nil {
431 return nil
432 }
433 return buf.Bytes()
434}
435
436func (s *Server) repoLanguage(ctx context.Context, dir, rev string) string {
437 if s.StoragePath() == "" {
438 return ""
439 }
440 entries, err := gitrepo.TreeAll(ctx, s.Git, dir, rev)
441 if err != nil {
442 return ""
443 }
444 counts := map[string]int{}
445 for _, e := range entries {
446 if e.Type != "blob" {
447 continue
448 }
449 if strings.Contains(e.Name, "vendor/") || strings.Contains(e.Name, "node_modules/") {
450 continue
451 }
452 if lang := langByName(e.Name); lang != "" {
453 counts[lang]++
454 }
455 }
456 best, n := "", 0
457 for lang, c := range counts {
458 if c > n || (c == n && lang < best) {
459 best, n = lang, c
460 }
461 }
462 return best
463}
464
465func langByName(name string) string {
466 low := strings.ToLower(name)
467 if i := strings.LastIndex(low, "/"); i >= 0 {
468 low = low[i+1:]
469 }
470 ext := ""
471 if i := strings.LastIndex(low, "."); i >= 0 {
472 ext = low[i:]
473 }
474 if lang, ok := extLang[ext]; ok {
475 return lang
476 }
477 switch low {
478 case "dockerfile":
479 return "Dockerfile"
480 case "makefile":
481 return "Makefile"
482 default:
483 return ""
484 }
485}
486
487var extLang = map[string]string{
488 ".go": "Go", ".py": "Python", ".js": "JavaScript", ".jsx": "JavaScript", ".ts": "TypeScript", ".tsx": "TypeScript",
489 ".rs": "Rust", ".java": "Java", ".rb": "Ruby", ".php": "PHP", ".c": "C", ".h": "C", ".cpp": "C++", ".cc": "C++",
490 ".cs": "C#", ".swift": "Swift", ".kt": "Kotlin", ".html": "HTML", ".css": "CSS", ".scss": "SCSS", ".vue": "Vue",
491 ".sh": "Shell", ".yml": "YAML", ".yaml": "YAML", ".sql": "SQL", ".lua": "Lua",
492}
493
494func starLabel(n int) string {
495 if n == 1 {
496 return "1 star"
497 }
498 return strconv.Itoa(n) + " stars"
499}
500
501func repoLabel(n int) string {
502 if n == 1 {
503 return "1 repository"
504 }
505 return strconv.Itoa(n) + " repositories"
506}
507
508func lineBounds(q url.Values, fragment string) (from, to int, ok bool) {
509 if from, to, ok = card.ParseLines(fragment); ok {
510 return from, to, true
511 }
512 return card.ParseLines(q.Get("L"))
513}
514
515func (s *Server) cardPNG(w http.ResponseWriter, r *http.Request) {
516 var rend card.Render
517 if r.URL.Query().Get("locked") == "1" {
518 rend = s.lockedRender()
519 } else {
520 path := r.URL.Query().Get("path")
521 if !strings.HasPrefix(path, "/") || strings.Contains(path, "..") || strings.Contains(path, "\\") {
522 http.NotFound(w, r)
523 return
524 }
525 q := url.Values{}
526 if l := r.URL.Query().Get("L"); l != "" {
527 q.Set("L", l)
528 }
529 if rev := r.URL.Query().Get("rev"); rev != "" {
530 q.Set("rev", rev)
531 }
532 rend = s.cardForPath(r.Context(), path, q.Encode(), "")
533 }
534 svg := card.SVG(rend)
535 pngBytes, hit := s.cardCache().Get(svg)
536 if !hit {
537 var err error
538 pngBytes, err = card.Raster(svg)
539 if err != nil {
540 http.Error(w, "card", http.StatusInternalServerError)
541 return
542 }
543 s.cardCache().Put(svg, pngBytes)
544 }
545 w.Header().Set("Content-Type", "image/png")
546 w.Header().Set("Cache-Control", "public, max-age=300")
547 w.Header().Set("X-Content-Type-Options", "nosniff")
548 if hit {
549 w.Header().Set("X-Card-Cache", "hit")
550 } else {
551 w.Header().Set("X-Card-Cache", "miss")
552 }
553 _, _ = w.Write(pngBytes)
554}
555
556func (s *Server) oembed(w http.ResponseWriter, r *http.Request) {
557 var rend card.Render
558 if r.URL.Query().Get("locked") == "1" {
559 rend = s.lockedRender()
560 } else {
561 raw := r.URL.Query().Get("url")
562 u, err := url.Parse(raw)
563 if err != nil || u.Path == "" {
564 http.NotFound(w, r)
565 return
566 }
567 rend = s.cardForPath(r.Context(), u.Path, u.RawQuery, u.Fragment)
568 }
569 w.Header().Set("Content-Type", "application/json; charset=utf-8")
570 w.Header().Set("Cache-Control", "no-store")
571 _ = json.NewEncoder(w).Encode(map[string]any{
572 "version": "1.0",
573 "type": "link",
574 "title": rend.Title,
575 "description": rend.Blurb(),
576 "provider_name": "BlueGit",
577 "provider_url": s.absURL("/"),
578 "thumbnail_url": rend.Image,
579 "thumbnail_width": card.Width,
580 "thumbnail_height": card.Height,
581 })
582}
583
584func (s *Server) writeLockedCard(w http.ResponseWriter, r *http.Request) {
585 m := s.lockedRender().Meta
586 w.Header().Set("Content-Type", "text/html; charset=utf-8")
587 w.Header().Set("Cache-Control", "no-store")
588 w.Header().Set("X-Robots-Tag", "noindex")
589 w.WriteHeader(http.StatusOK)
590 title := html.EscapeString(m.Title)
591 fmt.Fprintf(w, `<!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><title>%s</title>%s<link rel="icon" href="/assets/logo.svg" type="image/svg+xml"></head><body><p>%s</p></body></html>`,
592 title, metaHTML(m), title)
593}
594
595func metaHTML(m card.Meta) string {
596 esc := html.EscapeString
597 var b strings.Builder
598 fmt.Fprintf(&b, `<meta name="theme-color" content="%s">`, esc(m.Theme))
599 fmt.Fprintf(&b, `<meta name="description" content="%s">`, esc(m.Blurb()))
600 b.WriteString(`<meta property="og:site_name" content="BlueGit">`)
601 b.WriteString(`<meta property="og:type" content="website">`)
602 fmt.Fprintf(&b, `<meta property="og:title" content="%s">`, esc(m.Title))
603 fmt.Fprintf(&b, `<meta property="og:description" content="%s">`, esc(m.Blurb()))
604 fmt.Fprintf(&b, `<meta property="og:url" content="%s">`, esc(m.URL))
605 fmt.Fprintf(&b, `<meta property="og:image" content="%s">`, esc(m.Image))
606 b.WriteString(`<meta property="og:image:type" content="image/png">`)
607 fmt.Fprintf(&b, `<meta property="og:image:width" content="%d">`, card.Width)
608 fmt.Fprintf(&b, `<meta property="og:image:height" content="%d">`, card.Height)
609 b.WriteString(`<meta name="twitter:card" content="summary_large_image">`)
610 fmt.Fprintf(&b, `<meta name="twitter:title" content="%s">`, esc(m.Title))
611 fmt.Fprintf(&b, `<meta name="twitter:description" content="%s">`, esc(m.Blurb()))
612 fmt.Fprintf(&b, `<meta name="twitter:image" content="%s">`, esc(m.Image))
613 fmt.Fprintf(&b, `<link rel="alternate" type="application/json+oembed" href="%s" title="BlueGit">`, esc(m.OEmbed))
614 return b.String()
615}
616
617func isCrawler(r *http.Request) bool {
618 ua := strings.ToLower(r.Header.Get("User-Agent"))
619 for _, name := range []string{
620 "discordbot", "slackbot", "twitterbot", "facebookexternalhit", "linkedinbot",
621 "telegrambot", "whatsapp", "embedly", "iframely", "redditbot", "pinterestbot",
622 "skypeuripreview",
623 } {
624 if strings.Contains(ua, name) {
625 return true
626 }
627 }
628 return false
629}