Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(core): rename "payload" to "body" #348

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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