handlePutCrontabs handles PUT requests to update a crontab
(w http.ResponseWriter, r *http.Request)
| 2572 | |
| 2573 | // handlePutCrontabs handles PUT requests to update a crontab |
| 2574 | func handlePutCrontabs(w http.ResponseWriter, r *http.Request) { |
| 2575 | if r.Method != "PUT" { |
| 2576 | http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 2577 | return |
| 2578 | } |
| 2579 | |
| 2580 | if isSafeModeEnabled { |
| 2581 | http.Error(w, "Crontab editing is disabled in safe mode", http.StatusForbidden) |
| 2582 | return |
| 2583 | } |
| 2584 | |
| 2585 | // Get the crontab filename from the URL path |
| 2586 | parts := strings.Split(r.URL.Path, "/") |
| 2587 | if len(parts) < 4 { |
| 2588 | http.Error(w, "Invalid crontab path", http.StatusBadRequest) |
| 2589 | return |
| 2590 | } |
| 2591 | // Join all parts after "/api/crontabs/" to handle nested directory paths |
| 2592 | filename := strings.Join(parts[3:], "/") |
| 2593 | // Add leading slash for file paths, but not for user crontabs (user:username) |
| 2594 | if !strings.HasPrefix(filename, "user:") { |
| 2595 | filename = "/" + filename |
| 2596 | } |
| 2597 | |
| 2598 | // Parse the request body |
| 2599 | var request struct { |
| 2600 | Lines []struct { |
| 2601 | LineText string `json:"line_text"` |
| 2602 | Name string `json:"name,omitempty"` |
| 2603 | } `json:"lines"` |
| 2604 | } |
| 2605 | |
| 2606 | if err := json.NewDecoder(r.Body).Decode(&request); err != nil { |
| 2607 | http.Error(w, "Invalid request body", http.StatusBadRequest) |
| 2608 | return |
| 2609 | } |
| 2610 | |
| 2611 | // Get the crontab |
| 2612 | crontab, err := lib.GetCrontab(filename) |
| 2613 | if err != nil { |
| 2614 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 2615 | return |
| 2616 | } |
| 2617 | |
| 2618 | // Convert the lines to the format expected by the crontab |
| 2619 | var newLines []*lib.Line |
| 2620 | for _, line := range request.Lines { |
| 2621 | newLine := &lib.Line{ |
| 2622 | FullLine: line.LineText, |
| 2623 | Name: line.Name, |
| 2624 | Crontab: *crontab, |
| 2625 | } |
| 2626 | |
| 2627 | // Set line types and parse content based on line type |
| 2628 | if strings.HasPrefix(line.LineText, "#") { |
| 2629 | newLine.IsComment = true |
| 2630 | } else if strings.Contains(line.LineText, "=") { |
| 2631 | // Environment variable - just use FullLine |
no test coverage detected