From 31b4a5bbd2bd87e82b6c804567128ca08421dd6c Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Mon, 19 Jan 2026 23:21:08 -0700 Subject: [PATCH] working on updating SetAttachment to be fully Venice-compatible (in progress) --- conference.go | 2 +- config/config.go | 24 ++++++++++++++++++++++-- config/default.yaml | 7 +++++++ database/post.go | 24 ++++++++++++++++++++++-- 4 files changed, 52 insertions(+), 5 deletions(-) diff --git a/conference.go b/conference.go index cb089c1..e276e08 100644 --- a/conference.go +++ b/conference.go @@ -312,7 +312,7 @@ func AttachmentUpload(ctxt ui.AmContext) (string, any, error) { var data []byte data, err = slurpFile(file) if err == nil { - err = post.SetAttachment(ctxt.Ctx(), file.Filename, file.Header.Get("Content-Type"), int32(file.Size), data) + 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 } diff --git a/config/config.go b/config/config.go index 6b87652..90236af 100644 --- a/config/config.go +++ b/config/config.go @@ -13,10 +13,10 @@ package config import ( _ "embed" "fmt" + "maps" "os" argparse "github.com/alexflint/go-arg" - log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" ) @@ -88,6 +88,10 @@ type AmConfig struct { } `yaml:"rendering"` Posting struct { ExternalDictionary string `yaml:"externalDictionary"` + Uploads struct { + MaxSize string `yaml:"maxSize"` + NoCompressTypes []string `yaml:"noCompressTypes"` + } `yaml:"uploads"` } `yaml:"posting"` } @@ -121,6 +125,21 @@ func overlayString(loaded string, defaulted string) string { return loaded } +func overlayStringArray(loaded, defaulted []string) []string { + m := make(map[string]bool) + for _, s := range defaulted { + m[s] = true + } + for _, s := range loaded { + m[s] = true + } + rc := make([]string, 0, len(m)) + for s := range maps.Keys(m) { + rc = append(rc, s) + } + return rc +} + /* overlayInt is a helper that takes a loaded or defaulted integer and returns it. * Parameters: * loaded - The integer loaded from a configuration file. @@ -168,6 +187,8 @@ func overlayConfig(dest *AmConfig, loaded *AmConfig, defaults *AmConfig) { dest.Rendering.CookieKey = overlayString(loaded.Rendering.CookieKey, defaults.Rendering.CookieKey) dest.Rendering.CountryList.Prioritize = overlayString(loaded.Rendering.CountryList.Prioritize, defaults.Rendering.CountryList.Prioritize) dest.Posting.ExternalDictionary = overlayString(loaded.Posting.ExternalDictionary, defaults.Posting.ExternalDictionary) + dest.Posting.Uploads.MaxSize = overlayString(loaded.Posting.Uploads.MaxSize, defaults.Posting.Uploads.MaxSize) + dest.Posting.Uploads.NoCompressTypes = overlayStringArray(loaded.Posting.Uploads.NoCompressTypes, defaults.Posting.Uploads.NoCompressTypes) } // SetupConfig loads the command line arguments, loads the config file, and prepares GlobalConfig. @@ -188,5 +209,4 @@ func SetupConfig() { } else { GlobalConfig = defaultConfig // just copy over the defaults } - log.Infof("Global config: %v", GlobalConfig) } diff --git a/config/default.yaml b/config/default.yaml index d9c1fff..08b861f 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -45,3 +45,10 @@ rendering: prioritize: US posting: externalDictionary: "" + uploads: + maxSize: "1M" + noCompressTypes: + - "image/gif" + - "image/jpg" + - "image/jpeg" + - "image/png" diff --git a/database/post.go b/database/post.go index d07b270..c902737 100644 --- a/database/post.go +++ b/database/post.go @@ -43,6 +43,11 @@ type PostAttachInfo struct { Length int32 // length in bytes of attached file } +const ( + stgMethodPlain = 0 // attachment stored as raw data + stgMethodGZIP = 1 // attachment stored as GZIP data +) + // ErrNoPostData is returned if post data is missing. var ErrNoPostData = errors.New("no post data") @@ -88,6 +93,7 @@ func (p *PostHeader) AttachmentInfo(ctx context.Context) (*PostAttachInfo, error /* SetAttachment sets the attachment data for a post. * Parameters: * ctx - Standard Go context value. + * u - user attempting to upload attachment data * fileName - Name of the original attachment file. * mimeType - MIME type of the attachment data. * length - Length of the attachment data in bytes. @@ -95,8 +101,22 @@ func (p *PostHeader) AttachmentInfo(ctx context.Context) (*PostAttachInfo, error * Returns: * Standard Go error status. */ -func (p *PostHeader) SetAttachment(ctx context.Context, fileName string, mimeType string, length int32, data []byte) error { - _, err := amdb.ExecContext(ctx, "INSERT INTO postattach (postid, datalen, filename, mimetype, data) VALUES (?, ?, ?, ?, ?)", +func (p *PostHeader) SetAttachment(ctx context.Context, u *User, fileName string, mimeType string, length int32, data []byte) error { + if p.ScribbleDate != nil && p.ScribbleUid != nil { + return errors.New("cannot attach to scribbled post") + } + if u.Uid != p.CreatorUid { + return errors.New("cannot attach to a post that is not yours") + } + ai, err := p.AttachmentInfo(ctx) + if err != nil { + return err + } + if ai != nil { + return errors.New("attachment already present for this post") + } + // TODO + _, err = amdb.ExecContext(ctx, "INSERT INTO postattach (postid, datalen, filename, mimetype, data) VALUES (?, ?, ?, ?, ?)", p.PostId, length, fileName, mimeType, data) return err }