Skip to content

Commit

Permalink
chore(core): rename "payload" to "body" (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
claudiodangelis authored Dec 27, 2024
1 parent 9ab90c4 commit 9d597cb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 21 deletions.
16 changes: 8 additions & 8 deletions payload/payload.go → body/payload.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package payload
package body

import (
"os"
Expand All @@ -7,27 +7,27 @@ import (
"github.com/claudiodangelis/qrcp/util"
)

// Payload to transfer
type Payload struct {
// Body to transfer
type Body struct {
Filename string
Path string
DeleteAfterTransfer bool
}

// Delete the payload from disk
func (p Payload) Delete() error {
func (p Body) Delete() error {
return os.RemoveAll(p.Path)
}

// FromArgs returns a payload from args
func FromArgs(args []string, zipFlag bool) (Payload, error) {
func FromArgs(args []string, zipFlag bool) (Body, error) {
shouldzip := len(args) > 1 || zipFlag
var files []string
// Check if content exists
for _, arg := range args {
file, err := os.Stat(arg)
if err != nil {
return Payload{}, err
return Body{}, err
}
// If at least one argument is dir, the content will be zipped
if file.IsDir() {
Expand All @@ -41,13 +41,13 @@ func FromArgs(args []string, zipFlag bool) (Payload, error) {
if shouldzip {
zip, err := util.ZipFiles(files)
if err != nil {
return Payload{}, err
return Body{}, err
}
content = zip
} else {
content = args[0]
}
return Payload{
return Body{
Path: content,
Filename: filepath.Base(content),
DeleteAfterTransfer: shouldzip,
Expand Down
8 changes: 4 additions & 4 deletions cmd/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package cmd
import (
"fmt"

"github.com/claudiodangelis/qrcp/body"
"github.com/claudiodangelis/qrcp/config"
"github.com/claudiodangelis/qrcp/logger"
"github.com/claudiodangelis/qrcp/payload"
"github.com/claudiodangelis/qrcp/qr"
"github.com/eiannone/keyboard"

Expand All @@ -15,7 +15,7 @@ import (

func sendCmdFunc(command *cobra.Command, args []string) error {
log := logger.New(app.Flags.Quiet)
payload, err := payload.FromArgs(args, app.Flags.Zip)
body, err := body.FromArgs(args, app.Flags.Zip)
if err != nil {
return err
}
Expand All @@ -24,8 +24,8 @@ func sendCmdFunc(command *cobra.Command, args []string) error {
if err != nil {
return err
}
// Sets the payload
srv.Send(payload)
// Sets the body
srv.Send(body)
log.Print(`Scan the following URL with a QR reader to start the file transfer, press CTRL+C or "q" to exit:`)
log.Print(srv.SendURL)
qr.RenderString(srv.SendURL, cfg.Reversed)
Expand Down
18 changes: 9 additions & 9 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import (

"github.com/claudiodangelis/qrcp/qr"

"github.com/claudiodangelis/qrcp/body"
"github.com/claudiodangelis/qrcp/config"
"github.com/claudiodangelis/qrcp/pages"
"github.com/claudiodangelis/qrcp/payload"
"github.com/claudiodangelis/qrcp/util"
"gopkg.in/cheggaaa/pb.v1"
)
Expand All @@ -35,7 +35,7 @@ type Server struct {
// ReceiveURL is the URL used to Receive the file
ReceiveURL string
instance *http.Server
payload payload.Payload
body body.Body
outputDir string
stopChannel chan bool
// expectParallelRequests is set to true when qrcp sends files, in order
Expand All @@ -62,8 +62,8 @@ func (s *Server) ReceiveTo(dir string) error {
}

// Send adds a handler for sending the file
func (s *Server) Send(p payload.Payload) {
s.payload = p
func (s *Server) Send(p body.Body) {
s.body = p
s.expectParallelRequests = true
}

Expand All @@ -86,8 +86,8 @@ func (s Server) Wait() error {
if err := s.instance.Shutdown(context.Background()); err != nil {
log.Println(err)
}
if s.payload.DeleteAfterTransfer {
if err := s.payload.Delete(); err != nil {
if s.body.DeleteAfterTransfer {
if err := s.body.Delete(); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -227,10 +227,10 @@ func New(cfg *config.Config) (*Server, error) {
defer waitgroup.Done()
}
w.Header().Set("Content-Disposition", "attachment; filename=\""+
app.payload.Filename+
app.body.Filename+
"\"; filename*=UTF-8''"+
url.QueryEscape(app.payload.Filename))
http.ServeFile(w, r, app.payload.Path)
url.QueryEscape(app.body.Filename))
http.ServeFile(w, r, app.body.Path)
})
// Upload handler (serves the upload page)
http.HandleFunc("/receive/"+path, func(w http.ResponseWriter, r *http.Request) {
Expand Down

0 comments on commit 9d597cb

Please sign in to comment.