优化一些功能

This commit is contained in:
yml2213
2026-07-29 14:02:56 +08:00
parent 2c88edb741
commit f64838f8db
19 changed files with 313 additions and 85 deletions
+96 -3
View File
@@ -128,6 +128,9 @@ func (a *App) OrganizeAccounts(commodityID, commodityName string, startTime, end
var exportResult types.ExportAccountsResult
if len(hitAccounts) > 0 {
exportResult = a.ExportAccountsWithProduct(hitAccounts, commodityID, commodityName)
if !exportResult.Success {
return types.OrganizeAccountsResult{Error: "导出已抢到账号失败,已取消整理: " + exportResult.Error, ExportResult: exportResult}
}
}
// 更新原文件,只保留未抢到的
@@ -153,11 +156,13 @@ func (a *App) OrganizeAccounts(commodityID, commodityName string, startTime, end
}
}
// 写回原文件
newContent := strings.Join(keepLines, "\n") + "\n"
if err := os.WriteFile(accountFile, []byte(newContent), 0644); err != nil {
// 写回原文件前先备份,再用同目录临时文件替换,避免半写入损坏账号文件。
newContent := strings.Join(keepLines, "\r\n") + "\r\n"
backupPath, err := writeFileWithBackup(accountFile, []byte(newContent))
if err != nil {
return types.OrganizeAccountsResult{Error: "更新账号文件失败: " + err.Error()}
}
logger.LogEvent("整理", "success", "账号文件已安全更新,备份=%s", backupPath)
// 重新加载账号
a.scheduler.LoadAccounts()
@@ -173,3 +178,91 @@ func (a *App) OrganizeAccounts(commodityID, commodityName string, startTime, end
ExportResult: exportResult,
}
}
func writeFileWithBackup(path string, content []byte) (string, error) {
info, err := os.Stat(path)
if err != nil {
return "", err
}
mode := info.Mode().Perm()
if mode == 0 {
mode = 0644
}
original, err := os.ReadFile(path)
if err != nil {
return "", err
}
backupPath, err := createFileBackup(path, original, mode)
if err != nil {
return "", err
}
if err := replaceFileWithTemp(path, content, mode); err != nil {
return backupPath, err
}
return backupPath, nil
}
func createFileBackup(path string, content []byte, mode os.FileMode) (string, error) {
timestamp := time.Now().Format("20060102_150405.000")
basePath := filepath.Join(filepath.Dir(path), filepath.Base(path)+"."+timestamp+".bak")
for i := 0; i < 100; i++ {
backupPath := basePath
if i > 0 {
backupPath = fmt.Sprintf("%s.%d", basePath, i)
}
file, err := os.OpenFile(backupPath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, mode)
if os.IsExist(err) {
continue
}
if err != nil {
return "", err
}
if _, err := file.Write(content); err != nil {
_ = file.Close()
_ = os.Remove(backupPath)
return "", err
}
if err := file.Close(); err != nil {
_ = os.Remove(backupPath)
return "", err
}
return backupPath, nil
}
return "", fmt.Errorf("备份文件名连续冲突: %s", basePath)
}
func replaceFileWithTemp(path string, content []byte, mode os.FileMode) error {
dir := filepath.Dir(path)
tmp, err := os.CreateTemp(dir, "."+filepath.Base(path)+".*.tmp")
if err != nil {
return err
}
tmpPath := tmp.Name()
defer func() {
if tmpPath != "" {
_ = os.Remove(tmpPath)
}
}()
if _, err := tmp.Write(content); err != nil {
_ = tmp.Close()
return err
}
if err := tmp.Chmod(mode); err != nil {
_ = tmp.Close()
return err
}
if err := tmp.Sync(); err != nil {
_ = tmp.Close()
return err
}
if err := tmp.Close(); err != nil {
return err
}
if err := os.Rename(tmpPath, path); err != nil {
return err
}
tmpPath = ""
return nil
}