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
-57
View File
@@ -14,8 +14,6 @@ import (
"context"
"errors"
"fmt"
"io"
"mime/multipart"
"net/http"
"reflect"
"strconv"
@@ -277,61 +275,6 @@ func NewTopic(ctxt ui.AmContext) (string, any, error) {
return ui.ErrorPage(ctxt, errors.New("invalid button clicked on form"))
}
// 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 {
if file.Size <= (1024 * 1024) { // 1 Mb
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)
if err == nil {
return "redirect", target, nil
}
}
}
} else {
err = errors.New("the file is too large to be attached")
}
}
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"))
}
/* breakRange breaks up a post range into two elements.
* Parameters:
* topic - The topic within which the range is defined.