Files
mesh-drop/internal/transfer/history.go
nite d9db99946d fix: auto save block ask process
fix: mark all pending transfers canceled when app exit
2026-02-05 15:48:59 +08:00

51 lines
1.1 KiB
Go

package transfer
import (
"encoding/json"
"log/slog"
"mesh-drop/internal/config"
"os"
"path/filepath"
)
func (s *Service) SaveHistory() {
// 将 pending 状态的任务改为 canceled
transferList := s.GetTransferList()
for _, task := range transferList {
if task.Status == TransferStatusPending {
task.Status = TransferStatusCanceled
}
}
configDir := config.GetConfigDir()
historyPath := filepath.Join(configDir, "history.json")
historyJson, err := json.Marshal(transferList)
if err != nil {
return
}
file, err := os.OpenFile(historyPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return
}
defer file.Close()
_, err = file.Write(historyJson)
if err != nil {
slog.Error("Failed to write history", "error", err)
}
}
func (s *Service) LoadHistory() {
configDir := config.GetConfigDir()
historyPath := filepath.Join(configDir, "history.json")
file, err := os.Open(historyPath)
if err != nil {
return
}
defer file.Close()
var history []*Transfer
err = json.NewDecoder(file).Decode(&history)
if err != nil {
return
}
s.StoreTransfersToList(history)
}