package download import ( "app/shelfly/internal/models" "app/shelfly/query" "encoding/json" "errors" "fmt" "log" "net/http" "os" "strings" "github.com/gorilla/mux" "gorm.io/gorm" ) func CreateSavePath(db *gorm.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") var pathDownload models.PathDownload // Décoder les données de la requête if err := json.NewDecoder(r.Body).Decode(&pathDownload); err != nil { http.Error(w, `{"error": "Invalid JSON format"}`, http.StatusBadRequest) return } q := query.Use(db) if err := q.PathDownload.Create(&pathDownload); err != nil { http.Error(w, `{"error": "Failed to create path"}`, http.StatusInternalServerError) return } // Retourner un élément HTML dynamique pour HTMX // Récupérer tous les chemins var paths []models.PathDownload if err := db.Find(&paths).Error; err != nil { http.Error(w, `{"error": "Failed to retrieve paths"}`, http.StatusInternalServerError) return } // Construire les lignes HTML var response strings.Builder for _, path := range paths { log.Println(path) response.WriteString(fmt.Sprintf(`
`, path.ID, path.ID, path.Path, path.PathName,path.ID,path.ID,path.ID,path.ID ,path.ID,path.ID,path.ID,path.ID, path.ID,path.ID)) } w.WriteHeader(http.StatusOK) fmt.Fprint(w, response.String()) } } func ReadAllSavePath(db *gorm.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/html") // Récupérer tous les chemins var paths []models.PathDownload if err := db.Find(&paths).Error; err != nil { http.Error(w, `{"error": "Failed to retrieve paths"}`, http.StatusInternalServerError) return } // Construire les lignes HTML var response strings.Builder for _, path := range paths { response.WriteString(fmt.Sprintf(`
`, path.ID, path.ID, path.Path, path.PathName,path.ID,path.ID,path.ID,path.ID, path.ID,path.ID,path.ID,path.ID, path.ID,path.ID)) } // Retourner les lignes HTML w.WriteHeader(http.StatusOK) fmt.Fprint(w, response.String()) } } func UpdateSavePath(db *gorm.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") // Récupérer l'ID depuis les paramètres de l'URL id := mux.Vars(r)["id"] var pathDownload models.PathDownload log.Println(pathDownload); // Décoder les données de la requête if err := json.NewDecoder(r.Body).Decode(&pathDownload); err != nil { http.Error(w, `{"error": "Invalid JSON format"}`, http.StatusBadRequest) return } // Vérifier si la ressource existe var existingPath models.PathDownload if err := db.First(&existingPath, "id = ?", id).Error; err != nil { if err == gorm.ErrRecordNotFound { http.Error(w, `{"error": "Path not found"}`, http.StatusNotFound) return } http.Error(w, `{"error": "Failed to retrieve path"}`, http.StatusInternalServerError) return } // Mettre à jour les champs souhaités existingPath.Path = pathDownload.Path existingPath.PathName = pathDownload.PathName if err := db.Save(&existingPath).Error; err != nil { http.Error(w, `{"error": "Failed to update path"}`, http.StatusInternalServerError) return } response := fmt.Sprintf(`
`, existingPath.ID, existingPath.ID, existingPath.Path, existingPath.PathName, existingPath.ID, existingPath.ID, existingPath.ID, existingPath.ID, existingPath.ID, existingPath.ID, existingPath.ID, existingPath.ID, existingPath.ID, existingPath.ID) w.WriteHeader(http.StatusOK) fmt.Fprint(w, response) // // Retourner le nouvel élément HTML // var paths []models.PathDownload // if err := db.Find(&paths).Error; err != nil { // http.Error(w, `{"error": "Failed to retrieve paths"}`, http.StatusInternalServerError) // return // } // // Construire les lignes HTML // var response strings.Builder // for _, path := range paths { // log.Println(path) // response.WriteString(fmt.Sprintf(` //
//
// // // // // // // //
//
// `, path.ID, path.ID, path.Path, path.PathName,path.ID,path.ID,path.ID, path.ID,path.ID ,path.ID,path.ID,path.ID, path.ID,path.ID)) // } w.WriteHeader(http.StatusOK) //fmt.Fprint(w, response.String()) } } func DeleteSavePath(db *gorm.DB) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") // Récupérer l'ID depuis les paramètres de l'URL id := mux.Vars(r)["id"] // Vérifier si la ressource existe var pathDownload models.PathDownload if err := db.First(&pathDownload, "id = ?", id).Error; err != nil { if err == gorm.ErrRecordNotFound { http.Error(w, `{"error": "Path not found"}`, http.StatusNotFound) return } http.Error(w, `{"error": "Failed to retrieve path"}`, http.StatusInternalServerError) return } // Supprimer la ressource if err := db.Delete(&pathDownload).Error; err != nil { http.Error(w, `{"error": "Failed to delete path"}`, http.StatusInternalServerError) return } // Répondre avec succès w.WriteHeader(http.StatusOK) fmt.Fprintf(w, `
Delete ok
`) } } func IsPathValid(path string) error { if path == "" { return errors.New("path is empty") } info, err := os.Stat(path) if os.IsNotExist(err) { return errors.New("path does not exist") } if err != nil { return errors.New("unable to access path: " + err.Error()) } if !info.IsDir() && !info.Mode().IsRegular() { return errors.New("path is neither a file nor a directory") } return nil // Path is valid } // PathValidationHandler handles HTTP requests to validate a path func PathValidationHandler(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) return } var requestBody struct { Path string `json:"path"` } if err := json.NewDecoder(r.Body).Decode(&requestBody); err != nil { http.Error(w, "Invalid request body", http.StatusBadRequest) return } err := IsPathValid(requestBody.Path) response := map[string]string{ "path": requestBody.Path, "status": "valid", } if err != nil { response["status"] = "invalid" response["error"] = err.Error() w.WriteHeader(http.StatusBadRequest) } w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(response); err != nil { http.Error(w, "Failed to encode response", http.StatusInternalServerError) } }