Skip to content

Commit

Permalink
create a sender struct with conf and logger
Browse files Browse the repository at this point in the history
  • Loading branch information
ilius committed Oct 31, 2024
1 parent 36a5195 commit 49fcb52
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 9 deletions.
3 changes: 2 additions & 1 deletion cmd/localsend-go/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ func main() {
logger.Info("Waiting to receive files...")
select {} // Blocking program waiting to receive file
} else {
err := send.SendFile(conf, _flags.ToDevice, _flags.FilePath)
sender := send.New(conf, logger)
err := sender.SendFile(_flags.ToDevice, _flags.FilePath)
if err != nil {
logger.Error("Send failed", "err", err)
}
Expand Down
28 changes: 20 additions & 8 deletions pkg/send/send_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,19 @@ import (
"github.com/ilius/localsend-go/pkg/utils"
)

func sendFileToOtherDevicePrepare(conf *config.Config, ip string, path string) (*models.PrepareReceiveResponse, error) {
type senderImp struct {
conf *config.Config
log *slog.Logger
}

func New(conf *config.Config, logger *slog.Logger) *senderImp {
return &senderImp{
conf: conf,
log: logger,
}
}

func (s *senderImp) sendFileToOtherDevicePrepare(ip string, path string) (*models.PrepareReceiveResponse, error) {
// Prepare metadata for all files
files := make(map[string]models.FileInfo)
err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
Expand All @@ -45,7 +57,7 @@ func sendFileToOtherDevicePrepare(conf *config.Config, ip string, path string) (
return nil, fmt.Errorf("error walking the path: %w", err)
}

msg := shared.GetMesssage(conf)
msg := shared.GetMesssage(s.conf)

// Create and fill the PrepareReceiveRequest structure
request := models.PrepareReceiveRequest{
Expand Down Expand Up @@ -108,7 +120,7 @@ func sendFileToOtherDevicePrepare(conf *config.Config, ip string, path string) (
return &prepareReceiveResponse, nil
}

func uploadFile(ip, sessionId, fileId, token, filePath string) error {
func (s *senderImp) uploadFile(ip, sessionId, fileId, token, filePath string) error {
// Open the file you want to send
file, err := os.Open(filePath)
if err != nil {
Expand Down Expand Up @@ -158,13 +170,13 @@ func uploadFile(ip, sessionId, fileId, token, filePath string) error {
return fmt.Errorf("file upload failed: received status code %d", resp.StatusCode)
}

slog.Info("File uploaded successfully")
s.log.Info("File uploaded successfully")
return nil
}

func SendFile(conf *config.Config, ip string, path string) error {
response, err := sendFileToOtherDevicePrepare(conf, ip, path)
slog.Info("SendFile: got response", "response", response)
func (s *senderImp) SendFile(ip string, path string) error {
response, err := s.sendFileToOtherDevicePrepare(ip, path)
s.log.Info("SendFile: got response", "response", response)
if err != nil {
return err
}
Expand All @@ -181,7 +193,7 @@ func SendFile(conf *config.Config, ip string, path string) error {
if !ok {
return fmt.Errorf("token not found for file: %s", fileId)
}
err = uploadFile(ip, response.SessionID, fileId, token, filePath)
err = s.uploadFile(ip, response.SessionID, fileId, token, filePath)
if err != nil {
return fmt.Errorf("error uploading file: %w", err)
}
Expand Down

0 comments on commit 49fcb52

Please sign in to comment.