refactor of topic handling and image serving

This commit is contained in:
2026-01-18 22:29:20 -07:00
parent 0c596d65da
commit 567a774282
4 changed files with 38 additions and 35 deletions
+3 -18
View File
@@ -478,14 +478,7 @@ func ReadPosts(ctxt ui.AmContext) (string, any, error) {
comm := ctxt.CurrentCommunity() comm := ctxt.CurrentCommunity()
conf := ctxt.GetScratch("currentConference").(*database.Conference) conf := ctxt.GetScratch("currentConference").(*database.Conference)
myLevel := ctxt.GetScratch("levelInConference").(uint16) myLevel := ctxt.GetScratch("levelInConference").(uint16)
var topic *database.Topic = nil topic := ctxt.GetScratch("currentTopic").(*database.Topic)
if rawTopic, err := strconv.ParseInt(ctxt.URLParam("topic"), 10, 16); err == nil {
topic, err = database.AmGetTopicByNumber(ctxt.Ctx(), conf, int16(rawTopic))
}
if topic == nil {
ctxt.SetRC(http.StatusNotFound)
return ui.ErrorPage(ctxt, fmt.Errorf("topic not found: %s", ctxt.URLParam("topic")))
}
// Determine the range of posts to display. The "pin" is the post number after which we display the horizontal line separating old and new posts. // Determine the range of posts to display. The "pin" is the post number after which we display the horizontal line separating old and new posts.
lastRead, err := topic.GetLastRead(ctxt.Ctx(), ctxt.CurrentUser()) lastRead, err := topic.GetLastRead(ctxt.Ctx(), ctxt.CurrentUser())
@@ -642,17 +635,9 @@ func PostInTopic(ctxt ui.AmContext) (string, any, error) {
comm := ctxt.CurrentCommunity() comm := ctxt.CurrentCommunity()
conf := ctxt.GetScratch("currentConference").(*database.Conference) conf := ctxt.GetScratch("currentConference").(*database.Conference)
level := ctxt.GetScratch("levelInConference").(uint16) level := ctxt.GetScratch("levelInConference").(uint16)
topic := ctxt.GetScratch("currentTopic").(*database.Topic)
var topic *database.Topic = nil urlStem := fmt.Sprintf("/comm/%s/conf/%s/r/%d", comm.Alias, ctxt.GetScratch("currentAlias"), topic.Number)
if rawTopic, err := strconv.ParseInt(ctxt.URLParam("topic"), 10, 16); err == nil {
topic, err = database.AmGetTopicByNumber(ctxt.Ctx(), conf, int16(rawTopic))
}
if topic == nil {
ctxt.SetRC(http.StatusNotFound)
return ui.ErrorPage(ctxt, fmt.Errorf("topic not found: %s", ctxt.URLParam("topic")))
}
urlStem := fmt.Sprintf("/comm/%s/conf/%s/r/%d", comm.Alias, ctxt.URLParam("confid"), topic.Number)
if ctxt.FormFieldIsSet("cancel") { if ctxt.FormFieldIsSet("cancel") {
return "redirect", urlStem, nil return "redirect", urlStem, nil
} }
+3 -3
View File
@@ -49,7 +49,7 @@ func setupEcho() *echo.Echo {
fn := ui.AmWrap(NotImplPage) fn := ui.AmWrap(NotImplPage)
e.GET("/TODO/*", fn) e.GET("/TODO/*", fn)
e.POST("/TODO/*", fn) e.POST("/TODO/*", fn)
e.GET("/img/*", ui.AmWrap(ui.AmServeImage)) e.GET("/img/*", ui.AmServeImage)
e.GET("/static/*", ui.AmStaticFileHandler()) e.GET("/static/*", ui.AmStaticFileHandler())
e.GET("/go/:postlink", fn) e.GET("/go/:postlink", fn)
@@ -98,8 +98,8 @@ func setupEcho() *echo.Echo {
confGroup.GET("", ui.AmWrap(Topics)) confGroup.GET("", ui.AmWrap(Topics))
confGroup.GET("/new_topic", ui.AmWrap(NewTopicForm)) confGroup.GET("/new_topic", ui.AmWrap(NewTopicForm))
confGroup.POST("/new_topic", ui.AmWrap(NewTopic)) confGroup.POST("/new_topic", ui.AmWrap(NewTopic))
confGroup.GET("/r/:topic", ui.AmWrap(ReadPosts)) confGroup.GET("/r/:topic", ui.AmWrap(ReadPosts), ui.SetTopic)
confGroup.POST("/r/:topic", ui.AmWrap(PostInTopic)) confGroup.POST("/r/:topic", ui.AmWrap(PostInTopic), ui.SetTopic)
return e return e
} }
+10 -14
View File
@@ -28,6 +28,7 @@ import (
"git.erbosoft.com/amy/amsterdam/database" "git.erbosoft.com/amy/amsterdam/database"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"github.com/labstack/echo/v4"
) )
//go:embed static_images/* //go:embed static_images/*
@@ -55,14 +56,12 @@ func mimeTypeFromFilename(filename string) string {
/* AmServeImage serves an image from internal storage. /* AmServeImage serves an image from internal storage.
* Parameters: * Parameters:
* ctxt - The AmContext for the request. * c - The Echo context for this request.
* Returns: * Returns:
* Type of content to be rendered * Standard Go error return.
* Content to be rendered
* Standard Go error return
*/ */
func AmServeImage(ctxt AmContext) (string, any, error) { func AmServeImage(c echo.Context) error {
components := strings.SplitAfter(ctxt.URLPath(), "/") components := strings.SplitAfter(c.Request().URL.Path, "/")
var err error = nil var err error = nil
if len(components) == 4 { if len(components) == 4 {
switch components[2] { switch components[2] {
@@ -70,27 +69,24 @@ func AmServeImage(ctxt AmContext) (string, any, error) {
var b []byte var b []byte
b, err = static_images.ReadFile(filepath.Join("static_images", components[3])) b, err = static_images.ReadFile(filepath.Join("static_images", components[3]))
if err == nil { if err == nil {
ctxt.SetOutputType(mimeTypeFromFilename(components[3])) return c.Blob(http.StatusOK, mimeTypeFromFilename(components[3]), b)
return "bytes", b, nil
} }
case "store/": case "store/":
var id int var id int
id, err = strconv.Atoi(components[3]) id, err = strconv.Atoi(components[3])
if err == nil { if err == nil {
var img *database.ImageStore var img *database.ImageStore
img, err = database.AmLoadImage(ctxt.Ctx(), int32(id)) img, err = database.AmLoadImage(c.Request().Context(), int32(id))
if err == nil { if err == nil {
ctxt.SetOutputType(img.MimeType) return c.Blob(http.StatusOK, img.MimeType, img.Data)
return "bytes", img.Data, nil
} }
} }
} }
} }
ctxt.SetRC(http.StatusNotFound)
if err == nil { if err == nil {
err = fmt.Errorf("image not found: %s", ctxt.URLPath()) err = fmt.Errorf("image not found: %s", c.Request().URL.Path)
} }
return ErrorPage(ctxt, err) return c.String(http.StatusNotFound, err.Error())
} }
/* AmProcessUploadedImage takes an image and resizes it to a specified size, returning its data. /* AmProcessUploadedImage takes an image and resizes it to a specified size, returning its data.
+22
View File
@@ -12,8 +12,10 @@ package ui
import ( import (
"errors" "errors"
"fmt"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"git.erbosoft.com/amy/amsterdam/config" "git.erbosoft.com/amy/amsterdam/config"
"git.erbosoft.com/amy/amsterdam/database" "git.erbosoft.com/amy/amsterdam/database"
@@ -119,6 +121,7 @@ func ValidateConference(next echo.HandlerFunc) echo.HandlerFunc {
} }
} }
// SetConference is middleware that sets the conference context based on the URL.
func SetConference(next echo.HandlerFunc) echo.HandlerFunc { func SetConference(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error { return func(c echo.Context) error {
ctxt := AmContextFromEchoContext(c) ctxt := AmContextFromEchoContext(c)
@@ -140,3 +143,22 @@ func SetConference(next echo.HandlerFunc) echo.HandlerFunc {
return next(c) return next(c)
} }
} }
// SetTopic is middleware that sets the topic context based on the URL.
func SetTopic(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
ctxt := AmContextFromEchoContext(c)
conf := ctxt.GetScratch("currentConference").(*database.Conference)
var topic *database.Topic = nil
if rawTopic, err := strconv.ParseInt(ctxt.URLParam("topic"), 10, 16); err == nil {
topic, err = database.AmGetTopicByNumber(ctxt.Ctx(), conf, int16(rawTopic))
}
if topic == nil {
ctxt.SetRC(http.StatusNotFound)
return middlewareErrorPage(c, ctxt, fmt.Errorf("topic not found: %s", ctxt.URLParam("topic")))
}
ctxt.SetScratch("currentTopic", topic)
return next(c)
}
}