new file: begushiybashkir/bbvue/src/components/AvatarUpload.vue
modified: begushiybashkir/bbvue/src/stores/auth.js modified: begushiybashkir/bbvue/src/views/Profile.vue modified: begushiybashkir/bbvue/src/views/ProfileEdit.vue modified: serv_nginx/api_bb/go.mod modified: serv_nginx/api_bb/go.sum modified: serv_nginx/api_bb/internal/handlers/auth.go new file: serv_nginx/api_bb/internal/handlers/avatar.go modified: serv_nginx/api_bb/internal/handlers/news_handler.go modified: serv_nginx/api_bb/internal/handlers/user.go modified: serv_nginx/api_bb/internal/models/user.go modified: serv_nginx/api_bb/internal/repository/user_repository.go modified: serv_nginx/api_bb/internal/routes/routes.go modified: serv_nginx/api_bb/internal/service/auth_service.go new file: serv_nginx/api_bb/internal/service/avatar_service.go modified: serv_nginx/api_bb/internal/service/news_service.go modified: serv_nginx/api_bb/internal/service/user_service.go modified: serv_nginx/api_bb/pkg/logger/interface.go new file: serv_nginx/api_bb/pkg/logger/route_logger.go add structure fix, page, path, routes, component, authStore for upload, renew and delete avatar
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
// handlers/avatar.go
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"api_bb/internal/service"
|
||||
"api_bb/pkg/logger"
|
||||
"api_bb/pkg/middleware"
|
||||
"api_bb/pkg/utils"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
type AvatarHandler struct {
|
||||
logger logger.LoggerInterface
|
||||
avatarService service.AvatarService
|
||||
}
|
||||
|
||||
func NewAvatarHandler(avatarService service.AvatarService) *AvatarHandler {
|
||||
return &AvatarHandler{
|
||||
logger: logger.NewWrapper(logger.Get().With(zap.String("handler", "avatar"))),
|
||||
avatarService: avatarService,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *AvatarHandler) Routes() chi.Router {
|
||||
r := chi.NewRouter()
|
||||
r.Post("/upload", h.UploadAvatar)
|
||||
r.Delete("/delete", h.DeleteAvatar)
|
||||
return r
|
||||
}
|
||||
|
||||
func (h *AvatarHandler) UploadAvatar(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := middleware.GetUserFromContext(r.Context())
|
||||
if !ok {
|
||||
utils.RespondWithError(w, http.StatusUnauthorized, "Authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
// Парсим multipart форму
|
||||
if err := r.ParseMultipartForm(10 << 20); err != nil { // 10MB limit
|
||||
utils.RespondWithError(w, http.StatusBadRequest, "Failed to parse form: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("avatar")
|
||||
if err != nil {
|
||||
utils.RespondWithError(w, http.StatusBadRequest, "Failed to get file: "+err.Error())
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Проверяем тип файла
|
||||
allowedTypes := map[string]bool{
|
||||
"image/jpeg": true,
|
||||
"image/jpg": true,
|
||||
"image/png": true,
|
||||
"image/gif": true,
|
||||
}
|
||||
if !allowedTypes[header.Header.Get("Content-Type")] {
|
||||
utils.RespondWithError(w, http.StatusBadRequest, "Only JPEG, PNG and GIF images are allowed")
|
||||
return
|
||||
}
|
||||
|
||||
// Загружаем аватар
|
||||
avatarPath, err := h.avatarService.UploadAvatar(user.ID, file, header)
|
||||
if err != nil {
|
||||
h.logger.Error("Failed to upload avatar", zap.Error(err))
|
||||
utils.RespondWithError(w, http.StatusInternalServerError, "Failed to upload avatar: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.RespondWithJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"message": "Avatar uploaded successfully",
|
||||
"avatar": avatarPath,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *AvatarHandler) DeleteAvatar(w http.ResponseWriter, r *http.Request) {
|
||||
user, ok := middleware.GetUserFromContext(r.Context())
|
||||
if !ok {
|
||||
utils.RespondWithError(w, http.StatusUnauthorized, "Authentication required")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.avatarService.DeleteAvatar(user.ID); err != nil {
|
||||
h.logger.Error("Failed to delete avatar", zap.Error(err))
|
||||
utils.RespondWithError(w, http.StatusInternalServerError, "Failed to delete avatar: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
utils.RespondWithJSON(w, http.StatusOK, map[string]interface{}{
|
||||
"message": "Avatar deleted successfully",
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user