119 lines
3.6 KiB
Go
119 lines
3.6 KiB
Go
// Package storage wraps S3 upload/download/delete plus the content-type
|
|
// sniffing used to validate uploaded photos and PNG/PDF attachments. The
|
|
// bucket is treated as fully private — everything is proxied through the Go
|
|
// backend, so there are no presigned URLs or bucket CORS rules to manage.
|
|
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
|
|
"github.com/aws/aws-sdk-go-v2/aws"
|
|
"github.com/aws/aws-sdk-go-v2/config"
|
|
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
|
|
"github.com/aws/aws-sdk-go-v2/service/s3"
|
|
)
|
|
|
|
// Storage is what handlers depend on — satisfied by S3Storage (production)
|
|
// and LocalStorage (STORAGE_DRIVER=local, for testing without a real S3
|
|
// bucket). Handlers never need to know which one they're talking to.
|
|
type Storage interface {
|
|
Upload(ctx context.Context, key, contentType string, r io.Reader) error
|
|
Get(ctx context.Context, key string) (io.ReadCloser, error)
|
|
Delete(ctx context.Context, key string) error
|
|
}
|
|
|
|
type S3Storage struct {
|
|
client *s3.Client
|
|
bucket string
|
|
}
|
|
|
|
func NewS3(ctx context.Context, region, bucket string) (*S3Storage, error) {
|
|
cfg, err := config.LoadDefaultConfig(ctx, config.WithRegion(region))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("load aws config: %w", err)
|
|
}
|
|
return &S3Storage{
|
|
client: s3.NewFromConfig(cfg),
|
|
bucket: bucket,
|
|
}, nil
|
|
}
|
|
|
|
// Upload streams r to S3 under key with the given content type. Uses the
|
|
// managed uploader so large files aren't buffered fully in memory.
|
|
func (s *S3Storage) Upload(ctx context.Context, key, contentType string, r io.Reader) error {
|
|
uploader := manager.NewUploader(s.client)
|
|
_, err := uploader.Upload(ctx, &s3.PutObjectInput{
|
|
Bucket: aws.String(s.bucket),
|
|
Key: aws.String(key),
|
|
Body: r,
|
|
ContentType: aws.String(contentType),
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("upload %s: %w", key, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Get returns a stream of the object's body. Caller must Close it.
|
|
func (s *S3Storage) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
|
out, err := s.client.GetObject(ctx, &s3.GetObjectInput{
|
|
Bucket: aws.String(s.bucket),
|
|
Key: aws.String(key),
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get %s: %w", key, err)
|
|
}
|
|
return out.Body, nil
|
|
}
|
|
|
|
// Delete removes the object. Callers should treat failures as non-fatal
|
|
// (log and move on) since the DB row is the source of truth.
|
|
func (s *S3Storage) Delete(ctx context.Context, key string) error {
|
|
_, err := s.client.DeleteObject(ctx, &s3.DeleteObjectInput{
|
|
Bucket: aws.String(s.bucket),
|
|
Key: aws.String(key),
|
|
})
|
|
if err != nil {
|
|
return fmt.Errorf("delete %s: %w", key, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// DetectAndValidate sniffs the real content type of an uploaded file from
|
|
// its first bytes (never trusts the client-declared type/extension) and
|
|
// checks it against the allowed set. Returns the sniffed content type, or an
|
|
// error if it's not in allowed. The file's read position is reset to the
|
|
// start afterwards so it can still be uploaded.
|
|
func DetectAndValidate(file multipart.File, allowed map[string]bool) (string, error) {
|
|
buf := make([]byte, 512)
|
|
n, err := file.Read(buf)
|
|
if err != nil && err != io.EOF {
|
|
return "", fmt.Errorf("read file header: %w", err)
|
|
}
|
|
contentType := http.DetectContentType(buf[:n])
|
|
|
|
if _, err := file.Seek(0, io.SeekStart); err != nil {
|
|
return "", fmt.Errorf("reset file position: %w", err)
|
|
}
|
|
|
|
if !allowed[contentType] {
|
|
return "", fmt.Errorf("unsupported file type %q", contentType)
|
|
}
|
|
return contentType, nil
|
|
}
|
|
|
|
var (
|
|
AllowedPhotoTypes = map[string]bool{
|
|
"image/png": true,
|
|
"image/jpeg": true,
|
|
}
|
|
AllowedAttachmentTypes = map[string]bool{
|
|
"image/png": true,
|
|
"application/pdf": true,
|
|
}
|
|
)
|