fix: auto accept block ask process
This commit is contained in:
@@ -44,10 +44,11 @@ func (s *Service) SendFile(target *discovery.Peer, targetIP string, filePath str
|
||||
|
||||
task := NewTransfer(
|
||||
taskID,
|
||||
Sender{
|
||||
ID: s.discoveryService.GetID(),
|
||||
Name: s.config.GetHostName(),
|
||||
},
|
||||
NewSender(
|
||||
s.discoveryService.GetID(),
|
||||
s.config.GetHostName(),
|
||||
WithReceiverIP(targetIP, s.discoveryService),
|
||||
),
|
||||
WithFileName(filepath.Base(filePath)),
|
||||
WithFileSize(stat.Size()),
|
||||
WithType(TransferTypeSend),
|
||||
@@ -105,10 +106,11 @@ func (s *Service) SendFolder(target *discovery.Peer, targetIP string, folderPath
|
||||
|
||||
task := NewTransfer(
|
||||
taskID,
|
||||
Sender{
|
||||
ID: s.discoveryService.GetID(),
|
||||
Name: s.config.GetHostName(),
|
||||
},
|
||||
NewSender(
|
||||
s.discoveryService.GetID(),
|
||||
s.config.GetHostName(),
|
||||
WithReceiverIP(targetIP, s.discoveryService),
|
||||
),
|
||||
WithFileName(filepath.Base(folderPath)),
|
||||
WithFileSize(size),
|
||||
WithType(TransferTypeSend),
|
||||
@@ -152,10 +154,11 @@ func (s *Service) SendText(target *discovery.Peer, targetIP string, text string)
|
||||
r := bytes.NewReader([]byte(text))
|
||||
task := NewTransfer(
|
||||
taskID,
|
||||
Sender{
|
||||
ID: s.discoveryService.GetID(),
|
||||
Name: s.config.GetHostName(),
|
||||
},
|
||||
NewSender(
|
||||
s.discoveryService.GetID(),
|
||||
s.config.GetHostName(),
|
||||
WithReceiverIP(targetIP, s.discoveryService),
|
||||
),
|
||||
WithFileSize(int64(len(text))),
|
||||
WithType(TransferTypeSend),
|
||||
WithContentType(ContentTypeText),
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package transfer
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"mesh-drop/internal/discovery"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -53,10 +55,11 @@ type TransferOption func(*Transfer)
|
||||
|
||||
func NewTransfer(id string, sender Sender, opts ...TransferOption) *Transfer {
|
||||
t := &Transfer{
|
||||
ID: id,
|
||||
CreateTime: time.Now().UnixMilli(),
|
||||
Sender: sender,
|
||||
Status: TransferStatusPending, // Default status
|
||||
ID: id,
|
||||
CreateTime: time.Now().UnixMilli(),
|
||||
Sender: sender,
|
||||
Status: TransferStatusPending, // Default status
|
||||
DecisionChan: make(chan Decision, 1),
|
||||
}
|
||||
|
||||
for _, opt := range opts {
|
||||
@@ -123,6 +126,36 @@ func WithToken(token string) TransferOption {
|
||||
type Sender struct {
|
||||
ID string `json:"id" binding:"required"` // 发送者 ID
|
||||
Name string `json:"name" binding:"required"` // 发送者名称
|
||||
IP string `json:"ip" binding:"required"` // 发送者 IP
|
||||
}
|
||||
|
||||
type NewSenderOption func(*Sender)
|
||||
|
||||
func NewSender(id string, name string, opts ...NewSenderOption) Sender {
|
||||
s := &Sender{
|
||||
ID: id,
|
||||
Name: name,
|
||||
}
|
||||
for _, opt := range opts {
|
||||
opt(s)
|
||||
}
|
||||
return *s
|
||||
}
|
||||
|
||||
func WithIP(ip string) NewSenderOption {
|
||||
return func(s *Sender) {
|
||||
s.IP = ip
|
||||
}
|
||||
}
|
||||
|
||||
func WithReceiverIP(ip string, discoveryService *discovery.Service) NewSenderOption {
|
||||
return func(s *Sender) {
|
||||
ip, ok := discoveryService.GetLocalIPInSameSubnet(ip)
|
||||
if !ok {
|
||||
slog.Error("Failed to get local IP in same subnet", "ip", ip, "component", "transfer-client")
|
||||
}
|
||||
s.IP = ip
|
||||
}
|
||||
}
|
||||
|
||||
// Progress 用户前端传输进度
|
||||
|
||||
@@ -40,7 +40,6 @@ func (s *Service) handleAsk(c *gin.Context) {
|
||||
// 存储请求
|
||||
task.Type = TransferTypeReceive
|
||||
task.Status = TransferStatusPending
|
||||
task.DecisionChan = make(chan Decision)
|
||||
s.StoreTransferToList(&task)
|
||||
|
||||
if s.config.GetAutoAccept() {
|
||||
@@ -50,10 +49,11 @@ func (s *Service) handleAsk(c *gin.Context) {
|
||||
SavePath: s.config.GetSavePath(),
|
||||
}
|
||||
} else {
|
||||
// 发送系统通知
|
||||
_ = s.notifier.SendNotification(notifications.NotificationOptions{
|
||||
ID: uuid.New().String(),
|
||||
Title: "File Transfer Request",
|
||||
Body: fmt.Sprintf("%s wants to transfer %s", task.Sender, task.FileName),
|
||||
Body: fmt.Sprintf("%s(%s) wants to transfer %s", task.Sender.Name, task.Sender.IP, task.FileName),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user