This commit is contained in:
julien 2025-06-19 16:50:30 +02:00
parent 662d7b7fcb
commit 6b884ce24b

View File

@ -15,6 +15,27 @@ import (
"github.com/gorilla/mux" "github.com/gorilla/mux"
"gorm.io/gorm" "gorm.io/gorm"
) )
func renderPathHTML(path models.PathDownload) string {
return fmt.Sprintf(`
<div class="column">
<form>
<div id="path-%d" class="path-update grid is-col-min-1">
<input class="input is-primary cell" name="id" disabled value="%d"></input>
<input class="input is-primary fff cell" name="pathName" value="%s" disabled></span>
<button class="button is-primary is-dark" id="btn-path-edit-%d" hx-trigger="click" hx-target="#path-%d" hx-swap="outerHTML" onclick="enableAllInputPath(%d)">Edit</button>
<button class="button is-danger" type="button" id="btn-path-annuler-%d" onclick="disableAllInputPath(%d)" style="display:none" >Annuler</button>
<button class="button is-primary is-dark" id="btn-path-valider-%d" hx-put="/api/pathDownload/update/%d" hx-trigger="click[checkGlobalState()]" hx-target="#path-%d" hx-swap="outerHTML" hx-ext="json-enc" style="display:none">Valider</button>
<button class="button is-danger" hx-delete="/api/pathDownload/delete/%d" hx-trigger="click" hx-target="#path-%d" hx-swap="outerHTML" hx-confirm="Are you sure?" hx-swap="outerHTML swap:1s">Delete</button>
</div>
</form>
</div>`,
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)
}
func GetFirstActiveAccount(client *debridlink.Client) *debridlink.DebridAccount { func GetFirstActiveAccount(client *debridlink.Client) *debridlink.DebridAccount {
ctx := context.Background() // ✅ on remplace ici ctx := context.Background() // ✅ on remplace ici
@ -35,10 +56,9 @@ func GetFirstActiveAccount(client *debridlink.Client) *debridlink.DebridAccount
func CreateSavePath(db *gorm.DB) http.HandlerFunc { func CreateSavePath(db *gorm.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "text/html")
var pathDownload models.PathDownload var pathDownload models.PathDownload
if err := json.NewDecoder(r.Body).Decode(&pathDownload); err != nil { if err := json.NewDecoder(r.Body).Decode(&pathDownload); err != nil {
http.Error(w, `{"error": "Invalid JSON format"}`, http.StatusBadRequest) http.Error(w, `{"error": "Invalid JSON format"}`, http.StatusBadRequest)
return return
@ -49,7 +69,6 @@ func CreateSavePath(db *gorm.DB) http.HandlerFunc {
return return
} }
// Création du dossier physique
fullPath := "/app/upload/" + pathDownload.PathName fullPath := "/app/upload/" + pathDownload.PathName
if _, err := os.Stat(fullPath); os.IsNotExist(err) { if _, err := os.Stat(fullPath); os.IsNotExist(err) {
if err := os.MkdirAll(fullPath, 0755); err != nil { if err := os.MkdirAll(fullPath, 0755); err != nil {
@ -58,16 +77,14 @@ func CreateSavePath(db *gorm.DB) http.HandlerFunc {
} }
} }
// Enregistrement en base
pathDownload.Path = fullPath pathDownload.Path = fullPath
if err := db.Create(&pathDownload).Error; err != nil { if err := db.Create(&pathDownload).Error; err != nil {
http.Error(w, `{"error": "Failed to save path"}`, http.StatusInternalServerError) http.Error(w, `{"error": "Failed to save path"}`, http.StatusInternalServerError)
return return
} }
// Recharge la liste // 👉 On renvoie uniquement la ligne HTML nouvellement créée
ReadAllSavePath(db)(w, r) fmt.Fprint(w, renderPathHTML(pathDownload))
} }
} }
@ -110,7 +127,7 @@ func ReadAllSavePath(db *gorm.DB) http.HandlerFunc {
func UpdateSavePath(db *gorm.DB) http.HandlerFunc { func UpdateSavePath(db *gorm.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "text/html")
id := mux.Vars(r)["id"] id := mux.Vars(r)["id"]
var pathDownload models.PathDownload var pathDownload models.PathDownload
@ -121,15 +138,10 @@ func UpdateSavePath(db *gorm.DB) http.HandlerFunc {
var existingPath models.PathDownload var existingPath models.PathDownload
if err := db.First(&existingPath, "id = ?", id).Error; err != nil { if err := db.First(&existingPath, "id = ?", id).Error; err != nil {
if err == gorm.ErrRecordNotFound { http.Error(w, `{"error": "Path not found"}`, http.StatusNotFound)
http.Error(w, `{"error": "Path not found"}`, http.StatusNotFound)
return
}
http.Error(w, `{"error": "Failed to retrieve path"}`, http.StatusInternalServerError)
return return
} }
// Renommage physique du dossier
oldFullPath := "/app/upload/" + existingPath.PathName oldFullPath := "/app/upload/" + existingPath.PathName
newFullPath := "/app/upload/" + pathDownload.PathName newFullPath := "/app/upload/" + pathDownload.PathName
if oldFullPath != newFullPath { if oldFullPath != newFullPath {
@ -141,13 +153,13 @@ func UpdateSavePath(db *gorm.DB) http.HandlerFunc {
existingPath.PathName = pathDownload.PathName existingPath.PathName = pathDownload.PathName
existingPath.Path = newFullPath existingPath.Path = newFullPath
if err := db.Save(&existingPath).Error; err != nil { if err := db.Save(&existingPath).Error; err != nil {
http.Error(w, `{"error": "Failed to update path"}`, http.StatusInternalServerError) http.Error(w, `{"error": "Failed to update path"}`, http.StatusInternalServerError)
return return
} }
ReadAllSavePath(db)(w, r) // 👉 On renvoie uniquement la ligne HTML mise à jour
fmt.Fprint(w, renderPathHTML(existingPath))
} }
} }