refactor: single loop for import, skip duplicate files and DB records

This commit is contained in:
tian 2026-05-08 12:55:04 +08:00
parent f8d1083b2d
commit 8a335b6f73

View File

@ -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)
}
}
}