owpengram-server/internal/app/langpack/seed.go

55 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package langpack
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
)
// SeedDirectory 将导出的 .strings 文件导入 LangPackStore。
// root 可直接指向 data/langpack也可指向包含 .strings 的具体平台目录。
func (s *Service) SeedDirectory(ctx context.Context, root string) (int, error) {
if s == nil || s.packs == nil || root == "" {
return 0, nil
}
dir := filepath.Clean(root)
if _, err := os.Stat(dir); err != nil {
if errors.Is(err, os.ErrNotExist) {
return 0, nil
}
return 0, fmt.Errorf("stat langpack seed dir: %w", err)
}
seeded := 0
err := filepath.WalkDir(dir, func(path string, entry os.DirEntry, err error) error {
if err != nil {
return err
}
if entry.IsDir() || !strings.EqualFold(filepath.Ext(entry.Name()), ".strings") {
return nil
}
pack, err := ParseTDesktopFile(path)
if err != nil {
return err
}
existing, err := s.packs.GetPack(ctx, pack.LangPack, pack.LangCode, pack.Version)
if err != nil {
return err
}
if existing.Version >= pack.Version {
return nil
}
if err := s.packs.UpsertPack(ctx, pack); err != nil {
return err
}
seeded += len(pack.Strings)
return nil
})
if err != nil {
return seeded, fmt.Errorf("walk langpack seed dir: %w", err)
}
return seeded, nil
}