implemented attachment downloading

This commit is contained in:
2026-01-20 22:23:46 -07:00
parent 664525ea36
commit 700dbd6726
7 changed files with 191 additions and 59 deletions
+102
View File
@@ -11,12 +11,114 @@
package main
import (
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"strconv"
"strings"
"git.erbosoft.com/amy/amsterdam/database"
"git.erbosoft.com/amy/amsterdam/ui"
)
// slurpFile reads the contrents of a multipart.File into memory.
func slurpFile(file *multipart.FileHeader) ([]byte, error) {
f, err := file.Open()
if err != nil {
return nil, err
}
defer f.Close()
return io.ReadAll(f)
}
/* AttachmentUpload adds an attachment to a post.
* Parameters:
* ctxt - The AmContext for the request.
* Returns:
* Command string dictating what to be rendered.
* Data as a parameter for the command string.
* Standard Go error status.
*/
func AttachmentUpload(ctxt ui.AmContext) (string, any, error) {
target := ctxt.FormField("tgt")
postidStr := ctxt.FormField("post")
postId, err := strconv.ParseInt(postidStr, 10, 64)
if err != nil {
return ui.ErrorPage(ctxt, fmt.Errorf("internal error converting postID: %v", err))
}
if ctxt.FormFieldIsSet("upload") {
file, err := ctxt.FormFile("thefile")
if err == nil {
var post *database.PostHeader
post, err = database.AmGetPost(ctxt.Ctx(), postId)
if err == nil {
var data []byte
data, err = slurpFile(file)
if err == nil {
err = post.SetAttachment(ctxt.Ctx(), ctxt.CurrentUser(), file.Filename, file.Header.Get("Content-Type"), int32(file.Size), data, ctxt.RemoteIP())
if err == nil {
return "redirect", target, nil
}
}
}
}
ctxt.VarMap().Set("target", target)
ctxt.VarMap().Set("post", postId)
ctxt.VarMap().Set("amsterdam_pageTitle", "Upload Attachment")
ctxt.VarMap().Set("errorMessage", err.Error())
return "framed_template", "attachment_upload.jet", nil
}
return ui.ErrorPage(ctxt, errors.New("invalid button clicked on form"))
}
/* AttachmentSend sends the data of an attachment to the browser.
* Parameters:
* ctxt - The AmContext for the request.
* Returns:
* Command string dictating what to be rendered.
* Data as a parameter for the command string.
* Standard Go error status.
*/
func AttachmentSend(ctxt ui.AmContext) (string, any, error) {
postIdStr := ctxt.URLParam("post")
postId, err := strconv.ParseInt(postIdStr, 10, 64)
if err != nil {
return ui.ErrorPage(ctxt, fmt.Errorf("internal error converting postID: %v", err))
}
hdr, err := database.AmGetPost(ctxt.Ctx(), postId)
if err != nil {
return ui.ErrorPage(ctxt, err)
}
// Retrieve attachment info and data.
info, err := hdr.AttachmentInfo(ctxt.Ctx())
if err != nil {
return ui.ErrorPage(ctxt, err)
} else if info == nil {
return ui.ErrorPage(ctxt, errors.New("attachment not found"))
}
data, err := hdr.AttachmentData(ctxt.Ctx())
if err != nil {
return ui.ErrorPage(ctxt, err)
}
// Record a "hit" on this attachment in the background.
ampool.Submit(func(ctx context.Context) {
hdr.HitAttachment(ctx)
})
// Send the attachment data.
ctxt.SetOutputType(info.MIMEType)
if !strings.HasPrefix(info.MIMEType, "image/") {
ctxt.SetHeader("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", info.Filename))
}
return "bytes", data, nil
}
/* HideTopic hides or shows rthe current topic for the current user.
* Parameters:
* ctxt - The AmContext for the request.