Files
sale/backend/internal/api/handlers/upload.go
T

139 lines
3.5 KiB
Go

package handlers
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
)
type UploadHandler struct{}
func NewUploadHandler() *UploadHandler {
return &UploadHandler{}
}
func (h *UploadHandler) UploadImage(c *gin.Context) {
file, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "No file uploaded"})
return
}
ext := strings.ToLower(filepath.Ext(file.Filename))
allowedExts := map[string]bool{".jpg": true, ".jpeg": true, ".png": true, ".gif": true, ".webp": true}
if !allowedExts[ext] {
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid file type. Only jpg, jpeg, png, gif, webp are allowed"})
return
}
uploadDir := "uploads/images"
if err := os.MkdirAll(uploadDir, 0755); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create upload directory"})
return
}
filename := fmt.Sprintf("%s%s", uuid.New().String(), ext)
filepath := filepath.Join(uploadDir, filename)
if err := c.SaveUploadedFile(file, filepath); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save file"})
return
}
url := fmt.Sprintf("/uploads/images/%s", filename)
c.JSON(http.StatusOK, gin.H{
"url": url,
"filename": filename,
})
}
func (h *UploadHandler) UploadMultiple(c *gin.Context) {
form, err := c.MultipartForm()
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "No files uploaded"})
return
}
files := form.File["files"]
if len(files) == 0 {
c.JSON(http.StatusBadRequest, gin.H{"error": "No files uploaded"})
return
}
uploadDir := "uploads/images"
if err := os.MkdirAll(uploadDir, 0755); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create upload directory"})
return
}
allowedExts := map[string]bool{".jpg": true, ".jpeg": true, ".png": true, ".gif": true, ".webp": true}
var urls []string
for _, file := range files {
ext := strings.ToLower(filepath.Ext(file.Filename))
if !allowedExts[ext] {
continue
}
filename := fmt.Sprintf("%s%s", uuid.New().String(), ext)
filepath := filepath.Join(uploadDir, filename)
if err := c.SaveUploadedFile(file, filepath); err != nil {
continue
}
urls = append(urls, fmt.Sprintf("/uploads/images/%s", filename))
}
c.JSON(http.StatusOK, gin.H{
"urls": urls,
})
}
func (h *UploadHandler) DeleteImage(c *gin.Context) {
filename := c.Param("filename")
if filename == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "Filename is required"})
return
}
filepath := filepath.Join("uploads/images", filename)
if err := os.Remove(filepath); err != nil {
c.JSON(http.StatusNotFound, gin.H{"error": "File not found"})
return
}
c.JSON(http.StatusOK, gin.H{"message": "File deleted successfully"})
}
func (h *UploadHandler) ExportOrders(c *gin.Context) {
filename := fmt.Sprintf("orders_%s.csv", time.Now().Format("20060102150405"))
filepath := filepath.Join("uploads", filename)
file, err := os.Create(filepath)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create file"})
return
}
defer file.Close()
c.Header("Content-Description", "File Transfer")
c.Header("Content-Transfer-Encoding", "binary")
c.Header("Content-Disposition", "attachment; filename="+filename)
c.Header("Content-Type", "text/csv")
file.Seek(0, 0)
_, err = io.Copy(c.Writer, file)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to send file"})
return
}
}