diff options
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 |
