add: settings page

This commit is contained in:
2026-02-05 00:00:29 +08:00
parent f7a881358f
commit e862f8deec
18 changed files with 345 additions and 121 deletions

View File

@@ -53,7 +53,7 @@ func (s *Service) SendFile(target *discovery.Peer, targetIP string, filePath str
taskID,
Sender{
ID: s.discoveryService.GetID(),
Name: s.discoveryService.GetName(),
Name: s.config.GetHostName(),
},
WithFileName(filepath.Base(filePath)),
WithFileSize(stat.Size()),
@@ -105,7 +105,7 @@ func (s *Service) SendFolder(target *discovery.Peer, targetIP string, folderPath
taskID,
Sender{
ID: s.discoveryService.GetID(),
Name: s.discoveryService.GetName(),
Name: s.config.GetHostName(),
},
WithFileName(filepath.Base(folderPath)),
WithFileSize(size),
@@ -159,7 +159,7 @@ func (s *Service) SendText(target *discovery.Peer, targetIP string, text string)
taskID,
Sender{
ID: s.discoveryService.GetID(),
Name: s.discoveryService.GetName(),
Name: s.config.GetHostName(),
},
WithFileSize(int64(len(text))),
WithType(TransferTypeSend),

View File

@@ -0,0 +1,45 @@
package transfer
import (
"encoding/json"
"log/slog"
"mesh-drop/internal/config"
"os"
"path/filepath"
)
func (s *Service) SaveHistory() {
configDir := config.GetConfigDir()
historyPath := filepath.Join(configDir, "history.json")
historyJson, err := json.Marshal(s.GetTransferList())
if err != nil {
return
}
file, err := os.OpenFile(historyPath, os.O_CREATE|os.O_RDWR, os.FileMode(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
}
for _, item := range history {
s.StoreTransferToList(&item)
}
}

View File

@@ -42,7 +42,13 @@ func (s *Service) handleAsk(c *gin.Context) {
task.DecisionChan = make(chan Decision)
s.StoreTransferToList(&task)
// 通知 Wails 前端
if s.config.GetAutoAccept() {
task.DecisionChan <- Decision{
ID: task.ID,
Accepted: true,
SavePath: s.config.GetSavePath(),
}
}
// 等待用户决策或发送端放弃
select {
@@ -53,15 +59,14 @@ func (s *Service) handleAsk(c *gin.Context) {
task.SavePath = decision.SavePath
token := uuid.New().String()
task.Token = token
c.JSON(http.StatusOK, TransferAskResponse{
ID: task.ID,
Accepted: decision.Accepted,
Token: task.Token,
})
} else {
task.Status = TransferStatusRejected
}
c.JSON(http.StatusOK, TransferAskResponse{
ID: task.ID,
Accepted: decision.Accepted,
Token: task.Token,
})
case <-c.Request.Context().Done():
// 发送端放弃
task.Status = TransferStatusCanceled