NAM-APJATEL-BACKEND/utils/helper/image_uploader.go

90 lines
2.3 KiB
Go

package helper
import (
"fmt"
"io"
"mime/multipart"
"os"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
)
const (
MaxFileSize = 5 << 20 // 5MB
UploadDir = "./uploads/towers"
)
// SaveTowerImage saves uploaded image and returns the URL
func SaveTowerImage(file *multipart.FileHeader) (string, error) {
// Validate file size
if file.Size > MaxFileSize {
return "", fmt.Errorf("file size exceeds 5MB limit")
}
// Validate file type
allowedTypes := []string{".jpg", ".jpeg", ".png", ".webp"}
ext := strings.ToLower(filepath.Ext(file.Filename))
isAllowed := false
for _, allowedType := range allowedTypes {
if ext == allowedType {
isAllowed = true
break
}
}
if !isAllowed {
return "", fmt.Errorf("file type not allowed. Only jpg, jpeg, png, webp are allowed")
}
// Create upload directory if it doesn't exist
if err := os.MkdirAll(UploadDir, 0755); err != nil {
return "", fmt.Errorf("failed to create upload directory: %v", err)
}
// Generate unique filename
filename := fmt.Sprintf("%s_%d%s", uuid.New().String(), time.Now().Unix(), ext)
filePath := filepath.Join(UploadDir, filename)
// Open uploaded file
src, err := file.Open()
if err != nil {
return "", fmt.Errorf("failed to open uploaded file: %v", err)
}
defer src.Close()
// Create destination file
dst, err := os.Create(filePath)
if err != nil {
return "", fmt.Errorf("failed to create destination file: %v", err)
}
defer dst.Close()
// Copy file
if _, err := io.Copy(dst, src); err != nil {
return "", fmt.Errorf("failed to copy file: %v", err)
}
// Return URL (adjust based on your server setup)
return fmt.Sprintf("/uploads/towers/%s", filename), nil
}
// DeleteTowerImage deletes an image file
func DeleteTowerImage(imageURL string) error {
if imageURL == "" {
return nil
}
// Extract filename from URL
filename := filepath.Base(imageURL)
filePath := filepath.Join(UploadDir, filename)
// Check if file exists
if _, err := os.Stat(filePath); os.IsNotExist(err) {
return nil // File doesn't exist, nothing to delete
}
// Delete file
return os.Remove(filePath)
}