Skip to content

Instantly share code, notes, and snippets.

@abhaikollara abhaikollara/README.md
Last active Dec 2, 2018

Embed
What would you like to do?

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
You can’t perform that action at this time.