From cf891428b392cb319bffbe68cdca036d161cd9fc Mon Sep 17 00:00:00 2001 From: julien Date: Thu, 19 Jun 2025 15:26:09 +0200 Subject: [PATCH] up create path --- internal/download/download.go | 227 ++++++++++++---------------------- templates/settings.pages.tmpl | 6 +- 2 files changed, 82 insertions(+), 151 deletions(-) diff --git a/internal/download/download.go b/internal/download/download.go index e8a7848..178b60e 100644 --- a/internal/download/download.go +++ b/internal/download/download.go @@ -40,113 +40,87 @@ func CreateSavePath(db *gorm.DB) http.HandlerFunc { 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) + if pathDownload.PathName == "" { + http.Error(w, `{"error": "PathName cannot be empty"}`, http.StatusBadRequest) return } - // Retourner un élément HTML dynamique pour HTMX + // Création du dossier physique + fullPath := "/app/upload/" + pathDownload.PathName + if _, err := os.Stat(fullPath); os.IsNotExist(err) { + if err := os.MkdirAll(fullPath, 0755); err != nil { + http.Error(w, `{"error": "Failed to create directory"}`, http.StatusInternalServerError) + return + } + } + + // Enregistrement en base + pathDownload.Path = fullPath + + if err := db.Create(&pathDownload).Error; err != nil { + http.Error(w, `{"error": "Failed to save path"}`, http.StatusInternalServerError) + return + } + + // Recharge la liste + ReadAllSavePath(db)(w, r) + } +} + +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 + if err := db.Find(&paths).Error; err != nil { + http.Error(w, `{"error": "Failed to retrieve paths"}`, http.StatusInternalServerError) + return + } + var response strings.Builder - for _, path := range paths { - log.Println(path) + for _, path := range paths { response.WriteString(fmt.Sprintf(` -
+
+
+
+ + + + + + +
+
+
`, + path.ID, path.ID, path.PathName, + path.ID, path.ID, path.ID, + path.ID, path.ID, + path.ID, path.ID, path.ID, + path.ID, path.ID)) + } -
- -
- - - - - - - -
-
- - `, 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 { @@ -157,75 +131,34 @@ func UpdateSavePath(db *gorm.DB) http.HandlerFunc { return } - // Mettre à jour les champs souhaités - existingPath.Path = pathDownload.Path + // Renommage physique du dossier + oldFullPath := "/app/upload/" + existingPath.PathName + newFullPath := "/app/upload/" + pathDownload.PathName + if oldFullPath != newFullPath { + if err := os.Rename(oldFullPath, newFullPath); err != nil { + http.Error(w, `{"error": "Failed to rename directory"}`, http.StatusInternalServerError) + return + } + } + existingPath.PathName = pathDownload.PathName + existingPath.Path = newFullPath 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()) - + ReadAllSavePath(db)(w, r) } } + 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 { @@ -236,38 +169,38 @@ func DeleteSavePath(db *gorm.DB) http.HandlerFunc { return } - // Supprimer la ressource + // Supprimer physiquement le dossier + fullPath := "/app/upload/" + pathDownload.PathName + if err := os.RemoveAll(fullPath); err != nil { + http.Error(w, `{"error": "Failed to delete directory"}`, http.StatusInternalServerError) + return + } + 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 -
`) + ReadAllSavePath(db)(w, r) } } -func IsPathValid(path string) error { - if path == "" { + +func IsPathValid(subPath string) error { + if subPath == "" { return errors.New("path is empty") } - - info, err := os.Stat(path) + fullPath := "/app/upload/" + subPath + info, err := os.Stat(fullPath) 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") + if !info.IsDir() { + return errors.New("path is not a directory") } - - return nil // Path is valid + return nil } // PathValidationHandler handles HTTP requests to validate a path diff --git a/templates/settings.pages.tmpl b/templates/settings.pages.tmpl index e830f73..4dc9cb8 100644 --- a/templates/settings.pages.tmpl +++ b/templates/settings.pages.tmpl @@ -38,12 +38,10 @@
- - +
+