receive.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package cmd
  2. import (
  3. "fmt"
  4. "github.com/claudiodangelis/qrcp/config"
  5. "github.com/claudiodangelis/qrcp/logger"
  6. "github.com/claudiodangelis/qrcp/qr"
  7. "github.com/claudiodangelis/qrcp/server"
  8. "github.com/eiannone/keyboard"
  9. "github.com/spf13/cobra"
  10. )
  11. func receiveCmdFunc(command *cobra.Command, args []string) error {
  12. log := logger.New(quietFlag)
  13. // Load configuration
  14. configOptions := config.Options{
  15. Interface: interfaceFlag,
  16. Port: portFlag,
  17. Path: pathFlag,
  18. FQDN: fqdnFlag,
  19. KeepAlive: keepaliveFlag,
  20. ListAllInterfaces: listallinterfacesFlag,
  21. Secure: secureFlag,
  22. TLSCert: tlscertFlag,
  23. TLSKey: tlskeyFlag,
  24. Output: outputFlag,
  25. }
  26. cfg, err := config.New(configFlag, configOptions)
  27. if err != nil {
  28. return err
  29. }
  30. // Create the server
  31. srv, err := server.New(&cfg)
  32. if err != nil {
  33. return err
  34. }
  35. // Sets the output directory
  36. if err := srv.ReceiveTo(cfg.Output); err != nil {
  37. return err
  38. }
  39. // Prints the URL to scan to screen
  40. log.Print(`Scan the following URL with a QR reader to start the file transfer, press CTRL+C or "q" to exit:`)
  41. log.Print(srv.ReceiveURL)
  42. // Renders the QR
  43. qr.RenderString(srv.ReceiveURL)
  44. if browserFlag {
  45. srv.DisplayQR(srv.ReceiveURL)
  46. }
  47. if err := keyboard.Open(); err == nil {
  48. defer func() {
  49. keyboard.Close()
  50. }()
  51. go func() {
  52. for {
  53. char, key, _ := keyboard.GetKey()
  54. if string(char) == "q" || key == keyboard.KeyCtrlC {
  55. srv.Shutdown()
  56. }
  57. }
  58. }()
  59. } else {
  60. log.Print(fmt.Sprintf("Warning: keyboard not detected: %v", err))
  61. }
  62. if err := srv.Wait(); err != nil {
  63. return err
  64. }
  65. return nil
  66. }
  67. var receiveCmd = &cobra.Command{
  68. Use: "receive",
  69. Aliases: []string{"r"},
  70. Short: "Receive one or more files",
  71. Long: "Receive one or more files. The destination directory can be set with the config wizard, or by passing the --output flag. If none of the above are set, the current working directory will be used as a destination directory.",
  72. Example: `# Receive files in the current directory
  73. qrcp receive
  74. # Receive files in a specific directory
  75. qrcp receive --output /tmp
  76. `,
  77. RunE: receiveCmdFunc,
  78. }