feat: share link
This commit is contained in:
@@ -32,6 +32,7 @@ func New() http.Handler {
|
||||
|
||||
mux.HandleFunc("/", share.Handler)
|
||||
mux.HandleFunc("/api/receive-token", file.APIHandler)
|
||||
mux.HandleFunc("/"+lash.ShareLinkToken, file.FileHandler)
|
||||
|
||||
return mux
|
||||
}
|
||||
|
||||
@@ -40,3 +40,13 @@ func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(h.Contents)
|
||||
}
|
||||
|
||||
func (h FileData) FileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
// Send the file over as a stream of bytes
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileName))
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(h.Contents)))
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(h.Contents)
|
||||
}
|
||||
|
||||
29
internal/shareLink/shareLink.go
Normal file
29
internal/shareLink/shareLink.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package sharelink
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"lash"
|
||||
"lash/internal/errx"
|
||||
"net"
|
||||
)
|
||||
|
||||
func GenerateShareLink(port int) string {
|
||||
link := "http://"
|
||||
link += getLocalIP()
|
||||
link += fmt.Sprintf(":%d", port)
|
||||
link += "/"
|
||||
link += lash.ShareLinkToken
|
||||
return link
|
||||
}
|
||||
|
||||
func getLocalIP() string {
|
||||
conn, err := net.Dial("udp", "8.8.8.8:80")
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
localAddress := conn.LocalAddr().(*net.UDPAddr)
|
||||
|
||||
return localAddress.IP.String()
|
||||
}
|
||||
Reference in New Issue
Block a user