working on updating SetAttachment to be fully Venice-compatible (in progress)

This commit is contained in:
2026-01-19 23:21:08 -07:00
parent 652cdf8477
commit 31b4a5bbd2
4 changed files with 52 additions and 5 deletions
+1 -1
View File
@@ -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
}
+22 -2
View File
@@ -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)
}
+7
View File
@@ -45,3 +45,10 @@ rendering:
prioritize: US
posting:
externalDictionary: ""
uploads:
maxSize: "1M"
noCompressTypes:
- "image/gif"
- "image/jpg"
- "image/jpeg"
- "image/png"
+22 -2
View File
@@ -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
}