From 8a335b6f73e6cc108395d9e570de4caacbee0184 Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Fri, 8 May 2026 12:55:04 +0800 Subject: [PATCH] refactor: single loop for import, skip duplicate files and DB records --- internal/web/ui.go | 45 +++++++++++++-------------------------------- 1 file changed, 13 insertions(+), 32 deletions(-) diff --git a/internal/web/ui.go b/internal/web/ui.go index 63056b7..b03c1fa 100644 --- a/internal/web/ui.go +++ b/internal/web/ui.go @@ -4045,51 +4045,32 @@ func (u *UI) actionFaceGalleryImport(w http.ResponseWriter, r *http.Request) { count := 0 for _, files := range r.MultipartForm.File { for _, hdr := range files { - fullPath := fullFilename(hdr) - parts := strings.Split(strings.ReplaceAll(fullPath, "\\", "/"), "/") - if len(parts) < 2 { - continue - } - personName := strings.TrimSpace(parts[len(parts)-2]) - fileName := filepath.Base(parts[len(parts)-1]) + fp := fullFilename(hdr) + personName := personNameFromPath(fp) + fileName := filepath.Base(fp) 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) + // Skip if file already exists if _, err := os.Stat(dst); err == nil { - continue // skip duplicates - } - f, err := os.Create(dst) - if err != nil { - file.Close() continue } + file, err := hdr.Open() + if err != nil { continue } + f, err := os.Create(dst) + if err != nil { file.Close(); continue } io.Copy(f, file) f.Close() file.Close() count++ - } - } - // Register all persons in app DB and rebuild - if strings.TrimSpace(u.dbPath) != "" { - repo := storage.NewFaceGalleryRepo(u.dbPath) - for _, files := range r.MultipartForm.File { - for _, hdr := range files { - fp := fullFilename(hdr) - parts := strings.Split(strings.ReplaceAll(fp, "\\", "/"), "/") - if len(parts) < 2 { continue } - personName := strings.TrimSpace(parts[len(parts)-2]) - if personName != "" { - pid, _ := repo.FindOrCreatePerson(personName) - // Store relative path for display - repo.AddPhoto(pid, personName+"/"+filepath.Base(fp)) - } + // Register in DB + if strings.TrimSpace(u.dbPath) != "" { + repo := storage.NewFaceGalleryRepo(u.dbPath) + pid, _ := repo.FindOrCreatePerson(personName) + repo.AddPhoto(pid, personName+"/"+fileName) } } }