Skip to content

Instantly share code, notes, and snippets.

@abhaikollara
Last active December 2, 2018 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save abhaikollara/1a2f1b7c5e2e4bacaabec5c89ded9047 to your computer and use it in GitHub Desktop.
Save abhaikollara/1a2f1b7c5e2e4bacaabec5c89ded9047 to your computer and use it in GitHub Desktop.

goSend

A simple command line file sharing tool

Usage

goSend -f ~/pathToFile/theFile

Anyone in the local network can go to the host address (default:192.168.1.101:8000) to receive the file.

Options

  • -f The path to file
  • -h host ip address
  • -p port
  • -download If set to false, the browser will try to display the file
package main
import (
"flag"
"fmt"
"log"
"net/http"
"net/url"
"os"
"path/filepath"
)
func main() {
filePath := flag.String("f", "", "path to file")
host := flag.String("h", "192.168.1.101", "host")
port := flag.String("p", "8000", "port")
download := flag.Bool("download", true, "if set to False the browser will display the file if possible")
flag.Parse()
if *filePath == "" {
fmt.Println("Usage: goSend -f path_to_file")
os.Exit(1)
}
// fmt.Println(net.LookupHost("en0"))
http.HandleFunc("/", baseHandler(filePath, download))
fmt.Printf("Serving on http://%v:%v\n", *host, *port)
log.Fatal(http.ListenAndServe(*host+":"+*port, nil))
}
func baseHandler(filePath *string, download *bool) func(http.ResponseWriter, *http.Request) {
fileName := filepath.Base(*filePath)
contentDisposition := "attachment"
if *download == false {
contentDisposition = "inline"
}
contDispos := fmt.Sprintf("%v;filename=%v", contentDisposition, url.QueryEscape(fileName))
return func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
w.Header().Set("Content-Disposition", contDispos)
http.ServeFile(w, r, *filePath)
fmt.Printf("Served %v to %v\n", fileName, r.RemoteAddr)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment