| File was renamed from VoiceSnapGo/internal/paths/paths.go |
| | |
| | | "path/filepath" |
| | | ) |
| | | |
| | | const appDirName = "VoiceSnap" |
| | | const appDirName = "PrivateVoice Input" |
| | | |
| | | var legacyAppDirNames = []string{"VoiceSnap"} |
| | | |
| | | // AppSupportDir returns the per-user persistent data directory. |
| | | func AppSupportDir() string { |
| | | return appSupportDir(appDirName) |
| | | } |
| | | |
| | | func appSupportDir(dirName string) string { |
| | | base, err := os.UserConfigDir() |
| | | if err != nil || base == "" { |
| | | home, homeErr := os.UserHomeDir() |
| | | if homeErr != nil || home == "" { |
| | | return appDirName |
| | | return dirName |
| | | } |
| | | base = filepath.Join(home, "Library", "Application Support") |
| | | } |
| | | return filepath.Join(base, appDirName) |
| | | return filepath.Join(base, dirName) |
| | | } |
| | | |
| | | // File returns a path inside the persistent app data directory. |
| | |
| | | return filepath.Join(AppSupportDir(), name) |
| | | } |
| | | |
| | | // ModelDir returns the persistent SenseVoice model directory. |
| | | // ModelDir returns the legacy persistent SenseVoice model directory. |
| | | func ModelDir() string { |
| | | return filepath.Join(AppSupportDir(), "models", "sensevoice") |
| | | } |
| | |
| | | return filepath.Join(AppSupportDir(), "models") |
| | | } |
| | | |
| | | // ModelInstallDir returns the persistent install directory for a model profile. |
| | | func ModelInstallDir(name string) string { |
| | | return filepath.Join(ModelsRoot(), name) |
| | | } |
| | | |
| | | // ModelStatePath returns the persistent model install state file path. |
| | | func ModelStatePath() string { |
| | | return filepath.Join(ModelsRoot(), "state.json") |
| | | } |
| | | |
| | | // Ensure creates the app data directory. |
| | | func Ensure() error { |
| | | return os.MkdirAll(AppSupportDir(), 0755) |
| | | } |
| | | |
| | | // MigrateFromLegacyExecutableDir copies old user data from beside the executable |
| | | // into Application Support. Existing new-location files are never overwritten. |
| | | // MigrateFromLegacyExecutableDir copies old user data into the current app |
| | | // support directory. Existing new-location files are never overwritten. |
| | | func MigrateFromLegacyExecutableDir() error { |
| | | if err := Ensure(); err != nil { |
| | | return err |
| | | } |
| | | |
| | | for _, dirName := range legacyAppDirNames { |
| | | legacyDir := appSupportDir(dirName) |
| | | if samePath(legacyDir, AppSupportDir()) { |
| | | continue |
| | | } |
| | | if err := copyUserDataFromDir(legacyDir); err != nil { |
| | | return fmt.Errorf("migrate %s: %w", dirName, err) |
| | | } |
| | | } |
| | | |
| | | legacyDir, err := legacyExecutableDir() |
| | |
| | | return nil |
| | | } |
| | | |
| | | if err := copyUserDataFromDir(legacyDir); err != nil { |
| | | return err |
| | | } |
| | | |
| | | return nil |
| | | } |
| | | |
| | | func copyUserDataFromDir(sourceDir string) error { |
| | | for _, name := range []string{"config.json", "userdict.json", "history.json"} { |
| | | if err := copyFileIfMissing(filepath.Join(legacyDir, name), File(name)); err != nil { |
| | | if err := copyFileIfMissing(filepath.Join(sourceDir, name), File(name)); err != nil { |
| | | return fmt.Errorf("migrate %s: %w", name, err) |
| | | } |
| | | } |
| | | |
| | | if err := copyDirIfMissing(filepath.Join(legacyDir, "models"), ModelsRoot()); err != nil { |
| | | if err := copyDirIfMissing(filepath.Join(sourceDir, "models"), ModelsRoot()); err != nil { |
| | | return fmt.Errorf("migrate models: %w", err) |
| | | } |
| | | |