mirror of
https://github.com/Dvorinka/MyClubServer.git
synced 2026-06-04 02:32:57 +00:00
de day #74
This commit is contained in:
@@ -15,9 +15,19 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"fotbal-club/internal/config"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func uploadsBaseDir() string {
|
||||
dir := config.AppConfig.UploadDir
|
||||
if strings.TrimSpace(dir) == "" {
|
||||
dir = "./uploads"
|
||||
}
|
||||
return dir
|
||||
}
|
||||
|
||||
// sanitizeAndWriteLogo trims white/transparent borders and resizes to fixed height (64px), then writes PNG to outPath.
|
||||
func sanitizeAndWriteLogo(data []byte, outPath string) error {
|
||||
img, _, err := image.Decode(bytes.NewReader(data))
|
||||
@@ -102,7 +112,8 @@ func ensureUniqueFilename(dir, name string) string {
|
||||
|
||||
// ListSponsors returns list of sponsor logo URLs under /uploads/sponsors
|
||||
func (c *ScoreboardController) ListSponsors(ctx *gin.Context) {
|
||||
entries, err := os.ReadDir(filepath.Join("uploads", "sponsors"))
|
||||
sponsorDir := filepath.Join(uploadsBaseDir(), "sponsors")
|
||||
entries, err := os.ReadDir(sponsorDir)
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusOK, []string{})
|
||||
return
|
||||
@@ -125,7 +136,8 @@ func (c *ScoreboardController) UploadSponsors(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "invalid upload"})
|
||||
return
|
||||
}
|
||||
_ = os.MkdirAll(filepath.Join("uploads", "sponsors"), 0o755)
|
||||
sponsorDir := filepath.Join(uploadsBaseDir(), "sponsors")
|
||||
_ = os.MkdirAll(sponsorDir, 0o755)
|
||||
|
||||
saved := 0
|
||||
if ctx.Request.MultipartForm != nil {
|
||||
@@ -145,8 +157,8 @@ func (c *ScoreboardController) UploadSponsors(ctx *gin.Context) {
|
||||
if name == "" { name = fmt.Sprintf("sponsor-%d", time.Now().UnixNano()) }
|
||||
base := name
|
||||
if i := strings.LastIndex(name, "."); i >= 0 { base = name[:i] }
|
||||
outName := ensureUniqueFilename(filepath.Join("uploads", "sponsors"), base+".png")
|
||||
outPath := filepath.Join("uploads", "sponsors", outName)
|
||||
outName := ensureUniqueFilename(sponsorDir, base+".png")
|
||||
outPath := filepath.Join(sponsorDir, outName)
|
||||
|
||||
var buf bytes.Buffer
|
||||
if _, err := io.Copy(&buf, src); err == nil {
|
||||
@@ -154,8 +166,8 @@ func (c *ScoreboardController) UploadSponsors(ctx *gin.Context) {
|
||||
saved++
|
||||
} else {
|
||||
// Fallback: write original bytes with original extension
|
||||
rawName := ensureUniqueFilename(filepath.Join("uploads", "sponsors"), name)
|
||||
rawPath := filepath.Join("uploads", "sponsors", rawName)
|
||||
rawName := ensureUniqueFilename(sponsorDir, name)
|
||||
rawPath := filepath.Join(sponsorDir, rawName)
|
||||
_ = os.WriteFile(rawPath, buf.Bytes(), 0o644)
|
||||
saved++
|
||||
}
|
||||
@@ -173,7 +185,7 @@ func (c *ScoreboardController) DeleteSponsor(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "missing name"})
|
||||
return
|
||||
}
|
||||
p := filepath.Join("uploads", "sponsors", name)
|
||||
p := filepath.Join(uploadsBaseDir(), "sponsors", name)
|
||||
if _, err := os.Stat(p); os.IsNotExist(err) {
|
||||
ctx.JSON(http.StatusNotFound, gin.H{"error": "not found"})
|
||||
return
|
||||
@@ -187,7 +199,7 @@ func (c *ScoreboardController) DeleteSponsor(ctx *gin.Context) {
|
||||
|
||||
// GetQR returns the current QR image URL if present
|
||||
func (c *ScoreboardController) GetQR(ctx *gin.Context) {
|
||||
path := filepath.Join("uploads", "qr.png")
|
||||
path := filepath.Join(uploadsBaseDir(), "qr.png")
|
||||
if _, err := os.Stat(path); err == nil {
|
||||
ctx.JSON(http.StatusOK, gin.H{"qr": "/uploads/qr.png"})
|
||||
return
|
||||
@@ -203,8 +215,9 @@ func (c *ScoreboardController) UploadQR(ctx *gin.Context) {
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
_ = os.MkdirAll("uploads", 0o755)
|
||||
out, err := os.Create(filepath.Join("uploads", "qr.png"))
|
||||
dir := uploadsBaseDir()
|
||||
_ = os.MkdirAll(dir, 0o755)
|
||||
out, err := os.Create(filepath.Join(dir, "qr.png"))
|
||||
if err != nil {
|
||||
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "cannot save"})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user