modified: serv_nginx/api_bb/internal/handlers/user.go

chang serveAvatar function
This commit is contained in:
2025-10-14 12:47:37 +05:00
parent bbf470617b
commit a5548178da
+49 -8
View File
@@ -4,10 +4,12 @@ package handlers
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"fmt"
"io" "io"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"strings"
"time" "time"
"api_bb/internal/models" "api_bb/internal/models"
@@ -188,24 +190,63 @@ func (h *UserHandler) ServeAvatar(w http.ResponseWriter, r *http.Request) {
zap.String("remote_addr", r.RemoteAddr), zap.String("remote_addr", r.RemoteAddr),
) )
// Используем http.ServeFile для эффективной отдачи файла // Валидация
if filename == "" || strings.Contains(filename, "..") || strings.Contains(filename, "/") {
utils.RespondWithError(w, http.StatusBadRequest, "Invalid filename")
return
}
avatarPath := filepath.Join("./uploads/avatars", filename) avatarPath := filepath.Join("./uploads/avatars", filename)
// Проверяем существование файла // Проверяем существование файла
if _, err := os.Stat(avatarPath); os.IsNotExist(err) { fileInfo, err := os.Stat(avatarPath)
if os.IsNotExist(err) {
h.logger.Warn("avatar file not found", zap.String("path", avatarPath)) h.logger.Warn("avatar file not found", zap.String("path", avatarPath))
utils.RespondWithError(w, http.StatusNotFound, "Avatar not found") utils.RespondWithError(w, http.StatusNotFound, "Avatar not found")
return return
} }
if err != nil {
h.logger.Error("failed to access file", zap.Error(err))
utils.RespondWithError(w, http.StatusInternalServerError, "File access error")
return
}
// Устанавливаем заголовки кэширования // Открываем файл
w.Header().Set("Cache-Control", "public, max-age=31536000") file, err := os.Open(avatarPath)
w.Header().Set("Expires", time.Now().Add(365*24*time.Hour).Format(http.TimeFormat)) if err != nil {
h.logger.Error("failed to open file", zap.Error(err))
utils.RespondWithError(w, http.StatusInternalServerError, "File open error")
return
}
defer file.Close()
// Отдаем файл // Определяем Content-Type
http.ServeFile(w, r, avatarPath) ext := strings.ToLower(filepath.Ext(filename))
contentType := "application/octet-stream"
switch ext {
case ".png": contentType = "image/png"
case ".jpg", ".jpeg": contentType = "image/jpeg"
case ".gif": contentType = "image/gif"
case ".webp": contentType = "image/webp"
}
h.logger.Info("avatar served via ServeFile", // ✅ Устанавливаем ВСЕ заголовки
w.Header().Set("Content-Type", contentType)
w.Header().Set("Content-Length", fmt.Sprintf("%d", fileInfo.Size()))
w.Header().Set("Cache-Control", "public, max-age=31536000") // 1 год
w.Header().Set("Last-Modified", fileInfo.ModTime().Format(http.TimeFormat))
// Копируем файл в ResponseWriter
_, err = io.Copy(w, file)
if err != nil {
h.logger.Error("failed to send file", zap.Error(err))
// Не отправляем ошибку - соединение уже испорчено
return
}
h.logger.Info("avatar served successfully",
zap.String("filename", filename), zap.String("filename", filename),
zap.String("content_type", contentType),
zap.Int64("file_size", fileInfo.Size()),
) )
} }