Skip to content

Shared task board where people and AI coding agents work as teammates. Source-available, self-hostable.

Go

fix(upload): SVG content-type fallback and Content-Disposition for non-media files

Naiyuan Qing8526f013da8d
diff --git a/server/internal/handler/file.go b/server/internal/handler/file.go
index 75069558f2..a8becbd3fe 100644
--- a/server/internal/handler/file.go
+++ b/server/internal/handler/file.go
@@ -7,6 +7,7 @@ import (
"log/slog"
"net/http"
"path"
+ "strings"
"time"
"github.com/go-chi/chi/v5"
@@ -15,6 +16,17 @@ import (
db "github.com/multica-ai/multica/server/pkg/db/generated"
)
+// extContentTypes overrides http.DetectContentType for extensions it gets wrong.
+// Go's sniffer returns text/xml for SVG, text/plain for CSS/JS, etc.
+var extContentTypes = map[string]string{
+ ".svg": "image/svg+xml",
+ ".css": "text/css",
+ ".js": "application/javascript",
+ ".mjs": "application/javascript",
+ ".json": "application/json",
+ ".wasm": "application/wasm",
+}
+
const maxUploadSize = 100 << 20 // 100 MB
// ---------------------------------------------------------------------------
@@ -121,6 +133,10 @@ func (h *Handler) UploadFile(w http.ResponseWriter, r *http.Request) {
return
}
contentType := http.DetectContentType(buf[:n])
+ // Override with extension-based type when the sniffer gets it wrong.
+ if ct, ok := extContentTypes[strings.ToLower(path.Ext(header.Filename))]; ok {
+ contentType = ct
+ }
// Seek back so the full file is uploaded.
if _, err := file.Seek(0, io.SeekStart); err != nil {
writeError(w, http.StatusInternalServerError, "failed to read file")
diff --git a/server/internal/storage/s3.go b/server/internal/storage/s3.go
index b04a95610f..91c23d2502 100644
--- a/server/internal/storage/s3.go
+++ b/server/internal/storage/s3.go
@@ -123,14 +123,28 @@ func (s *S3Storage) DeleteKeys(ctx context.Context, keys []string) {
}
}
+// isInlineContentType returns true for media types that browsers should
+// display inline (images, video, audio, PDF). Everything else triggers a
+// download via Content-Disposition: attachment.
+func isInlineContentType(ct string) bool {
+ return strings.HasPrefix(ct, "image/") ||
+ strings.HasPrefix(ct, "video/") ||
+ strings.HasPrefix(ct, "audio/") ||
+ ct == "application/pdf"
+}
+
func (s *S3Storage) Upload(ctx context.Context, key string, data []byte, contentType string, filename string) (string, error) {
safe := sanitizeFilename(filename)
+ disposition := "attachment"
+ if isInlineContentType(contentType) {
+ disposition = "inline"
+ }
_, err := s.client.PutObject(ctx, &s3.PutObjectInput{
Bucket: aws.String(s.bucket),
Key: aws.String(key),
Body: bytes.NewReader(data),
ContentType: aws.String(contentType),
- ContentDisposition: aws.String(fmt.Sprintf(`inline; filename="%s"`, safe)),
+ ContentDisposition: aws.String(fmt.Sprintf(`%s; filename="%s"`, disposition, safe)),
CacheControl: aws.String("max-age=432000,public"),
StorageClass: types.StorageClassIntelligentTiering,
})