fix(storage): clean up variable shadowing and dead code (#761)

- Rename `filepath` local var to `dest` in LocalStorage.Upload to avoid
  shadowing the path/filepath package import
- Remove unused detectContentType and overrideContentType functions from
  util.go (no longer needed after ServeFile switched to http.ServeFile)
This commit is contained in:
Bohan Jiang
2026-04-12 14:06:46 +08:00
committed by GitHub
parent d32c419b6d
commit 31eeb00b59
2 changed files with 2 additions and 32 deletions

View File

@@ -80,8 +80,8 @@ func (s *LocalStorage) DeleteKeys(ctx context.Context, keys []string) {
}
func (s *LocalStorage) Upload(ctx context.Context, key string, data []byte, contentType string, filename string) (string, error) {
filepath := filepath.Join(s.uploadDir, key)
if err := os.WriteFile(filepath, data, 0644); err != nil {
dest := filepath.Join(s.uploadDir, key)
if err := os.WriteFile(dest, data, 0644); err != nil {
return "", fmt.Errorf("local storage WriteFile: %w", err)
}

View File

@@ -1,8 +1,6 @@
package storage
import (
"net/http"
"path"
"strings"
)
@@ -21,34 +19,6 @@ func sanitizeFilename(name string) string {
return b.String()
}
// detectContentType determines the content type for a file.
// Uses http.DetectContentType as base, with overrides for common extensions.
func detectContentType(data []byte, filename string) string {
contentType := http.DetectContentType(data)
ext := strings.ToLower(path.Ext(filename))
if ct := overrideContentType(ext); ct != "" {
return ct
}
return contentType
}
// overrideContentType returns content type overrides for extensions that http.DetectContentType gets wrong.
func overrideContentType(ext string) string {
switch ext {
case ".svg":
return "image/svg+xml"
case ".css":
return "text/css"
case ".js", ".mjs":
return "application/javascript"
case ".json":
return "application/json"
case ".wasm":
return "application/wasm"
}
return ""
}
// isInlineContentType returns true for media types that browsers should
// display inline (images, video, audio, PDF). Everything else triggers a
// download via Content-Disposition: attachment.