From 981731e2ceea146070b4e67e6c6eddf9bf266d9f Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Fri, 8 May 2026 09:58:22 +0800 Subject: [PATCH] feat: bulk import photos into face gallery --- internal/web/ui.go | 41 +++++++++++++++++++++ internal/web/ui/templates/face_gallery.html | 4 ++ 2 files changed, 45 insertions(+) diff --git a/internal/web/ui.go b/internal/web/ui.go index b8db8fc..8be7ba9 100644 --- a/internal/web/ui.go +++ b/internal/web/ui.go @@ -663,6 +663,7 @@ func (u *UI) Routes() (chi.Router, error) { r.Get("/diagnostics", u.pageDiagnostics) r.Get("/alarms", u.pageAlarms) r.Get("/face-gallery", u.pageFaceGallery) + r.Post("/face-gallery/import", u.actionFaceGalleryImport) r.Post("/face-gallery/add", u.actionFaceGalleryAdd) r.Post("/face-gallery/delete", u.actionFaceGalleryDelete) r.Post("/face-gallery/rename", u.actionFaceGalleryRename) @@ -4021,3 +4022,43 @@ func (u *UI) actionFaceGalleryRename(w http.ResponseWriter, r *http.Request) { repo.RenamePerson(id, name) http.Redirect(w, r, "/ui/face-gallery", http.StatusFound) } + +func (u *UI) actionFaceGalleryImport(w http.ResponseWriter, r *http.Request) { + if err := r.ParseMultipartForm(100 << 20); err != nil { + http.Error(w, "form too large", http.StatusBadRequest) + return + } + datasetDir := filepath.Join("dataset") + count := 0 + for _, files := range r.MultipartForm.File { + for _, hdr := range files { + // webkitdirectory sends files with relative paths like "person_name/photo.jpg" + parts := strings.SplitN(filepath.ToSlash(hdr.Filename), "/", 2) + if len(parts) < 2 { + continue + } + personName := strings.TrimSpace(parts[0]) + fileName := filepath.Base(parts[1]) + if personName == "" || fileName == "" { + continue + } + personDir := filepath.Join(datasetDir, personName) + os.MkdirAll(personDir, 0o755) + file, err := hdr.Open() + if err != nil { + continue + } + dst := filepath.Join(personDir, fileName) + f, err := os.Create(dst) + if err != nil { + file.Close() + continue + } + io.Copy(f, file) + f.Close() + file.Close() + count++ + } + } + http.Redirect(w, r, fmt.Sprintf("/ui/face-gallery?msg=已导入 %d 张照片", count), http.StatusFound) +} diff --git a/internal/web/ui/templates/face_gallery.html b/internal/web/ui/templates/face_gallery.html index 69e9df7..59d72bb 100644 --- a/internal/web/ui/templates/face_gallery.html +++ b/internal/web/ui/templates/face_gallery.html @@ -6,6 +6,10 @@