diff options
| author | ivar <i@oiee.no> | 2026-04-04 16:49:36 +0200 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2026-04-04 16:49:36 +0200 |
| commit | 7de11c5ca03fcfda6ec3d39c5340a317ae77e2d5 (patch) | |
| tree | 94144d26aeef705dd99be6c9f040b6a9f3cbde96 /internal | |
| parent | f45d85cf59d13d419f18b98d25f8e3b8cf17d9e1 (diff) | |
| download | nebbet.no-7de11c5ca03fcfda6ec3d39c5340a317ae77e2d5.tar.xz nebbet.no-7de11c5ca03fcfda6ec3d39c5340a317ae77e2d5.zip | |
fix: close error propagation, remove GIF, unexport storageDir
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/media/handler.go | 19 |
1 files changed, 11 insertions, 8 deletions
diff --git a/internal/media/handler.go b/internal/media/handler.go index 3cca2bd..24a95a0 100644 --- a/internal/media/handler.go +++ b/internal/media/handler.go @@ -22,18 +22,17 @@ const ( var allowedMIMEs = map[string]string{ "image/jpeg": ".jpg", "image/png": ".png", - "image/gif": ".gif", "image/webp": ".webp", } // MediaHandler handles image uploads and on-the-fly image serving. type MediaHandler struct { - StorageDir string + storageDir string } // NewMediaHandler returns a MediaHandler that stores files in storageDir. func NewMediaHandler(storageDir string) *MediaHandler { - return &MediaHandler{StorageDir: storageDir} + return &MediaHandler{storageDir: storageDir} } // HandleUpload handles POST /admin/upload/image. @@ -67,25 +66,29 @@ func (h *MediaHandler) HandleUpload(c *gin.Context) { } } - if err := os.MkdirAll(h.StorageDir, 0755); err != nil { + if err := os.MkdirAll(h.storageDir, 0755); err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": 0, "error": "storage error"}) return } id := uuid.New().String() - destPath := filepath.Join(h.StorageDir, id+ext) + destPath := filepath.Join(h.storageDir, id+ext) out, err := os.Create(destPath) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{"success": 0, "error": "storage error"}) return } - defer out.Close() if _, err := io.Copy(out, file); err != nil { + out.Close() c.JSON(http.StatusInternalServerError, gin.H{"success": 0, "error": "write error"}) return } + if err := out.Close(); err != nil { + c.JSON(http.StatusInternalServerError, gin.H{"success": 0, "error": "finalize error"}) + return + } c.JSON(http.StatusOK, gin.H{ "success": 1, @@ -126,7 +129,7 @@ func (h *MediaHandler) HandleServe(c *gin.Context) { } cacheName := cacheKey(base, width, format) - cachePath := filepath.Join(h.StorageDir, cacheName) + cachePath := filepath.Join(h.storageDir, cacheName) // Cache hit: serve the existing variant directly if f, err := os.Open(cachePath); err == nil { @@ -137,7 +140,7 @@ func (h *MediaHandler) HandleServe(c *gin.Context) { return } - origPath, err := findOriginal(h.StorageDir, base) + origPath, err := findOriginal(h.storageDir, base) if err != nil { c.AbortWithStatus(http.StatusNotFound) return |
