diff --git a/internal/storage/face_gallery_repo.go b/internal/storage/face_gallery_repo.go index 4aca418..5e62bc5 100644 --- a/internal/storage/face_gallery_repo.go +++ b/internal/storage/face_gallery_repo.go @@ -3,6 +3,8 @@ package storage import ( "database/sql" "fmt" + "os" + "path/filepath" "time" ) @@ -11,6 +13,7 @@ type PersonRecord struct { Name string `json:"name"` PhotoCount int `json:"photo_count"` Photos []string `json:"photos,omitempty"` + PhotoIDs []int `json:"photo_ids,omitempty"` CreatedAt string `json:"created_at"` } @@ -71,14 +74,16 @@ func (r *FaceGalleryRepo) ListPersons() ([]PersonRecord, error) { } // Load photos for each person for i := range out { - photoRows, err := db.Query(`SELECT photo_path FROM face_photos WHERE person_id = ? ORDER BY id`, out[i].ID) + photoRows, err := db.Query(`SELECT photo_path, id FROM face_photos WHERE person_id = ? ORDER BY id`, out[i].ID) if err != nil { continue } for photoRows.Next() { var path string - if photoRows.Scan(&path) == nil { + var pid int + if photoRows.Scan(&path, &pid) == nil { out[i].Photos = append(out[i].Photos, path) + out[i].PhotoIDs = append(out[i].PhotoIDs, pid) } } photoRows.Close() @@ -156,3 +161,17 @@ func (r *FaceGalleryRepo) RenamePerson(id int, name string) error { _, err = db.Exec(`UPDATE face_persons SET name = ?, updated_at = ? WHERE id = ?`, name, now, id) return err } + +func (r *FaceGalleryRepo) DeletePhoto(photoID int) error { + db, err := r.open() + if err != nil { return err } + defer db.Close() + // Get path before deleting + var path string + db.QueryRow(`SELECT photo_path FROM face_photos WHERE id = ?`, photoID).Scan(&path) + _, err = db.Exec(`DELETE FROM face_photos WHERE id = ?`, photoID) + if err != nil { return err } + // Remove file from disk + if path != "" { os.Remove(filepath.Join("dataset", path)) } + return nil +} diff --git a/internal/web/ui.go b/internal/web/ui.go index 45cf6bd..78be9a9 100644 --- a/internal/web/ui.go +++ b/internal/web/ui.go @@ -672,6 +672,7 @@ func (u *UI) Routes() (chi.Router, error) { r.Post("/face-gallery/delete", u.actionFaceGalleryDelete) r.Post("/face-gallery/rename", u.actionFaceGalleryRename) r.Post("/face-gallery/add-photo", u.actionFaceGalleryAddPhoto) + r.Post("/face-gallery/delete-photo", u.actionFaceGalleryDeletePhoto) r.Get("/monitor", u.pageMonitor) r.Get("/hls/*", u.proxyHLS) r.Get("/api/monitor/channels", u.apiMonitorChannels) @@ -4175,3 +4176,10 @@ func (u *UI) actionFaceGalleryAddPhoto(w http.ResponseWriter, r *http.Request) { repo.AddPhoto(id, personName+"/"+hdr.Filename) http.Redirect(w, r, "/ui/face-gallery", http.StatusFound) } +func (u *UI) actionFaceGalleryDeletePhoto(w http.ResponseWriter, r *http.Request) { + id, _ := strconv.Atoi(r.FormValue("id")) + if id > 0 && strings.TrimSpace(u.dbPath) != "" { + storage.NewFaceGalleryRepo(u.dbPath).DeletePhoto(id) + } + http.Redirect(w, r, "/ui/face-gallery", http.StatusFound) +} diff --git a/internal/web/ui/templates/face_gallery.html b/internal/web/ui/templates/face_gallery.html index a06933f..83d48a2 100644 --- a/internal/web/ui/templates/face_gallery.html +++ b/internal/web/ui/templates/face_gallery.html @@ -35,7 +35,7 @@