Skip to content

Commit

Permalink
better og images
Browse files Browse the repository at this point in the history
  • Loading branch information
watzon committed Jan 13, 2025
1 parent 785e876 commit 91b70f4
Show file tree
Hide file tree
Showing 16 changed files with 702 additions and 283 deletions.
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/disintegration/imaging v1.6.2 // indirect
github.com/dlclark/regexp2 v1.11.4 // indirect
github.com/fsnotify/fsnotify v1.8.0 // indirect
github.com/glebarez/go-sqlite v1.22.0 // indirect
github.com/gofiber/template v1.8.3 // indirect
github.com/gofiber/utils v1.1.0 // indirect
github.com/gomarkdown/markdown v0.0.0-20241205020045-f7e15b2f3e62 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/dlclark/regexp2 v1.11.4 h1:rPYF9/LECdNymJufQKmri9gV604RvvABwgOA8un7yAo=
github.com/dlclark/regexp2 v1.11.4/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
Expand Down Expand Up @@ -82,6 +84,8 @@ github.com/golang-migrate/migrate/v4 v4.18.1 h1:JML/k+t4tpHCpQTCAD62Nu43NUFzHY4C
github.com/golang-migrate/migrate/v4 v4.18.1/go.mod h1:HAX6m3sQgcdO81tdjn5exv20+3Kb13cmGli1hrD6hks=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
github.com/gomarkdown/markdown v0.0.0-20241205020045-f7e15b2f3e62 h1:pbAFUZisjG4s6sxvRJvf2N7vhpCvx2Oxb3PmS6pDO1g=
github.com/gomarkdown/markdown v0.0.0-20241205020045-f7e15b2f3e62/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd h1:gbpYu9NMq8jhDVbvlGkMFWCjLFlqqEZjEmObmhUy6Vo=
Expand Down Expand Up @@ -193,6 +197,7 @@ golang.org/x/crypto v0.31.0 h1:ihbySMvVjLAeSH1IbfcRTkD/iNscyz8rGzjF/E5hV6U=
golang.org/x/crypto v0.31.0/go.mod h1:kDsLvtWBEx7MV9tJOj9bnXsPbxwJQ6csT/x4KIN4Ssk=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f h1:XdNn9LlyWAhLVp6P/i8QYBW+hlyhrhei9uErw2B5GJo=
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f/go.mod h1:D5SMRVC3C2/4+F/DB1wZsLRnSNimn2Sp/NPsCrsv8ak=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/image v0.22.0 h1:UtK5yLUzilVrkjMAZAZ34DXGpASN8i8pj8g+O+yd10g=
golang.org/x/image v0.22.0/go.mod h1:9hPFhljd4zZ1GNSIZJ49sqbp45GKK9t6w+iXvGqZUz4=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
Expand All @@ -207,6 +212,7 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/time v0.8.0 h1:9i3RxcPv3PZnitoVGMPDKZSq1xW1gK1Xy3ArNOGZfEg=
Expand Down
84 changes: 84 additions & 0 deletions internal/server/handlers/paste.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package handlers
import (
"fmt"
"strings"
"time"

"github.com/gofiber/fiber/v2"
"github.com/gomarkdown/markdown"
"github.com/gomarkdown/markdown/parser"
"github.com/valyala/fasthttp"
"github.com/watzon/0x45/internal/config"
"github.com/watzon/0x45/internal/server/services"
"go.uber.org/zap"
Expand Down Expand Up @@ -167,3 +171,83 @@ func (h *PasteHandlers) HandleGetPasteImage(c *fiber.Ctx) error {

return h.services.Paste.GetPasteImage(c, paste)
}

// HandlePreview renders a markdown preview
func (h *PasteHandlers) HandlePreview(c *fiber.Ctx) error {
id := getPasteID(c)

// Get extension from locals if available and build full ID
var fullID string
if ext := c.Locals("extension"); ext != nil {
fullID = id + "." + ext.(string)
} else {
fullID = id
}

paste, err := h.services.Paste.GetPaste(id)
if err != nil {
return err
}

// Create a new context for rendering the raw content
app := c.App()
ctx := app.AcquireCtx(&fasthttp.RequestCtx{})
defer app.ReleaseCtx(ctx)

// Reset the response to avoid any previous data
ctx.Response().Reset()

// Get the raw content
if err := h.services.Paste.RenderPasteRaw(ctx, paste); err != nil {
return err
}

// Get the content from the response
content := ctx.Response().Body()

// Convert markdown to HTML
extensions := parser.CommonExtensions | parser.AutoHeadingIDs
p := parser.NewWithExtensions(extensions)
renderedContent := string(markdown.ToHTML(content, p, nil))

// Format the expiry time
var expiryTime string
if paste.ExpiresAt != nil {
expiryTime = formatExpiryTime(paste.ExpiresAt)
}

// Render the preview template
return c.Render("preview", fiber.Map{
"id": fullID, // Use the ID with extension
"filename": paste.Filename,
"created": paste.CreatedAt.Format("2006-01-02 15:04:05"),
"expires": expiryTime,
"metadata": fiber.Map{
"size": formatSize(paste.Size),
"mimeType": paste.MimeType,
},
"renderedContent": renderedContent,
}, "layouts/main")
}

// formatExpiryTime formats a time pointer into a string
func formatExpiryTime(t *time.Time) string {
if t == nil {
return "Never"
}
return t.Format("2006-01-02 15:04:05")
}

// formatSize formats a size in bytes into a human readable string
func formatSize(size int64) string {
const unit = 1024
if size < unit {
return fmt.Sprintf("%d B", size)
}
div, exp := int64(unit), 0
for n := size / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %ciB", float64(size)/float64(div), "KMGTPE"[exp])
}
16 changes: 14 additions & 2 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"strings"

"github.com/gabriel-vasile/mimetype"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/logger"
Expand Down Expand Up @@ -32,8 +33,18 @@ type Server struct {
}

func New(config *config.Config, logger *zap.Logger) *Server {
// gormLogger := zapgorm2.New(logger)
// gormLogger.SetAsDefault()
// Register markdown MIME types
mimetype.Extend(func(raw []byte, limit uint32) bool {
// Check for common markdown headers
content := string(raw)
if len(content) > 0 {
firstLine := strings.Split(content, "\n")[0]
if strings.HasPrefix(firstLine, "# ") || strings.HasPrefix(firstLine, "## ") {
return true
}
}
return false
}, "text/markdown", ".md", ".markdown")

// Custom parsers for fiber
fiber.SetParserDecoder(fiber.ParserConfig{
Expand Down Expand Up @@ -194,6 +205,7 @@ func (s *Server) SetupRoutes() {
s.app.Get("/p/:id/raw", s.handlers.Paste.HandleRawView)
s.app.Get("/p/:id/download", s.handlers.Paste.HandleDownload)
s.app.Get("/p/:id/image", s.handlers.Paste.HandleGetPasteImage)
s.app.Get("/p/:id/preview", s.handlers.Paste.HandlePreview)
s.app.Delete("/p/:id/:key", s.handlers.Paste.HandleDeleteWithKey)
s.app.Get("/p/:id/:key", s.handlers.Paste.HandleDeleteWithKey)
}
Expand Down
Loading

0 comments on commit 91b70f4

Please sign in to comment.