diff --git a/payload/payload.go b/body/payload.go similarity index 79% rename from payload/payload.go rename to body/payload.go index 7ec426d..6d6dade 100644 --- a/payload/payload.go +++ b/body/payload.go @@ -1,4 +1,4 @@ -package payload +package body import ( "os" @@ -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() { @@ -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, diff --git a/cmd/send.go b/cmd/send.go index ddd791c..38db20a 100644 --- a/cmd/send.go +++ b/cmd/send.go @@ -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" @@ -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 } @@ -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) diff --git a/server/server.go b/server/server.go index 99d8658..958d127 100644 --- a/server/server.go +++ b/server/server.go @@ -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" ) @@ -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 @@ -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 } @@ -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) } } @@ -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) {