@@ -3,12 +3,18 @@ package handlers
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/gif"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
_ "golang.org/x/image/webp"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3/types"
|
||||
@@ -88,7 +94,7 @@ func UploadFile(d UploadDeps) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
pub := publicObjectURL(d.S3PublicBase, d.S3Bucket, d.AWSRegion, key)
|
||||
writeJSON(w, map[string]any{"url": pub, "filename": name, "storage": "s3"})
|
||||
writeUploadJSON(w, pub, hdr.Filename, name, ct, int64(len(data)), data, "s3")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -107,10 +113,33 @@ func UploadFile(d UploadDeps) http.HandlerFunc {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
writeJSON(w, map[string]any{"url": "/uploads/" + name, "filename": name, "storage": "local"})
|
||||
writeUploadJSON(w, "/uploads/"+name, hdr.Filename, name, ct, int64(len(data)), data, "local")
|
||||
}
|
||||
}
|
||||
|
||||
func writeUploadJSON(w http.ResponseWriter, url, originalName, storedName, mime string, size int64, data []byte, storage string) {
|
||||
kind := classifyAttachmentKind(mime, originalName)
|
||||
if kind == "" {
|
||||
kind = classifyAttachmentKind(mime, storedName)
|
||||
}
|
||||
out := map[string]any{
|
||||
"url": url,
|
||||
"filename": storedName,
|
||||
"storage": storage,
|
||||
"mime": mime,
|
||||
"sizeBytes": size,
|
||||
"kind": kind,
|
||||
}
|
||||
if strings.HasPrefix(strings.ToLower(mime), "image/") {
|
||||
cfg, _, err := image.DecodeConfig(bytes.NewReader(data))
|
||||
if err == nil {
|
||||
out["width"] = cfg.Width
|
||||
out["height"] = cfg.Height
|
||||
}
|
||||
}
|
||||
writeJSON(w, out)
|
||||
}
|
||||
|
||||
// s3PutObjectCannedACL maps env S3_OBJECT_ACL to SDK enum; unknown values are ignored.
|
||||
func s3PutObjectCannedACL(raw string) (types.ObjectCannedACL, bool) {
|
||||
switch strings.TrimSpace(strings.ToLower(raw)) {
|
||||
|
||||
Reference in New Issue
Block a user