handleUpdatePerform handles POST requests to perform an update
(w http.ResponseWriter, r *http.Request)
| 2933 | |
| 2934 | // handleUpdatePerform handles POST requests to perform an update |
| 2935 | func handleUpdatePerform(w http.ResponseWriter, r *http.Request) { |
| 2936 | if r.Method != "POST" { |
| 2937 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 2938 | return |
| 2939 | } |
| 2940 | |
| 2941 | if isSafeModeEnabled { |
| 2942 | http.Error(w, "Updates are disabled in safe mode", http.StatusForbidden) |
| 2943 | return |
| 2944 | } |
| 2945 | |
| 2946 | // Check for update first |
| 2947 | status, err := checkForUpdate() |
| 2948 | if err != nil { |
| 2949 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 2950 | return |
| 2951 | } |
| 2952 | |
| 2953 | if !status.HasUpdate { |
| 2954 | http.Error(w, "No update available", http.StatusBadRequest) |
| 2955 | return |
| 2956 | } |
| 2957 | |
| 2958 | // Set up SSE headers for streaming progress |
| 2959 | w.Header().Set("Content-Type", "text/event-stream") |
| 2960 | w.Header().Set("Cache-Control", "no-cache") |
| 2961 | w.Header().Set("Connection", "keep-alive") |
| 2962 | |
| 2963 | // Function to send progress updates |
| 2964 | sendProgress := func(message string) { |
| 2965 | defer func() { |
| 2966 | if r := recover(); r != nil { |
| 2967 | // Connection was likely closed during update, silently ignore |
| 2968 | log(fmt.Sprintf("Progress update failed (connection closed): %v", r)) |
| 2969 | } |
| 2970 | }() |
| 2971 | |
| 2972 | // Check if w is nil |
| 2973 | if w == nil { |
| 2974 | return |
| 2975 | } |
| 2976 | |
| 2977 | progressData, _ := json.Marshal(map[string]string{"progress": message}) |
| 2978 | if _, err := fmt.Fprintf(w, "data: %s\n\n", progressData); err != nil { |
| 2979 | return |
| 2980 | } |
| 2981 | |
| 2982 | // Simple flush without nested defer |
| 2983 | if flusher, ok := w.(http.Flusher); ok && flusher != nil { |
| 2984 | flusher.Flush() // This will be caught by the main defer/recover if it panics |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | sendProgress("Starting update process...") |
| 2989 | |
| 2990 | // Perform the update in a goroutine |
| 2991 | go func() { |
| 2992 | if err := performUpdateWithRestart(status, sendProgress); err != nil { |
nothing calls this directly
no test coverage detected