feat(profile): implement protected skin upload endpoint
This commit is contained in:
@@ -38,3 +38,32 @@ func (h *ProfileHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(profile)
|
||||
}
|
||||
|
||||
func (h *ProfileHandler) UploadSkin(w http.ResponseWriter, r *http.Request) {
|
||||
// Получаем userID из контекста, который был добавлен middleware
|
||||
userID, ok := r.Context().Value(UserIDContextKey).(int)
|
||||
if !ok {
|
||||
http.Error(w, "Could not get user ID from context", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Ограничиваем размер загружаемого файла (например, 16KB)
|
||||
r.ParseMultipartForm(16 << 10) // 16KB
|
||||
|
||||
file, header, err := r.FormFile("skin") // "skin" - это имя поля в форме
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid file upload", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
err = h.Service.UpdateUserSkin(r.Context(), userID, file, header)
|
||||
if err != nil {
|
||||
// Можно добавить более детальную обработку ошибок
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Skin updated successfully"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user