Compare commits
8 Commits
a93fa2f61f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
645fdc37d8 | ||
|
|
35d908cd73 | ||
|
|
f188264f39 | ||
|
|
1436b8cf2f | ||
|
|
459fe380db | ||
|
|
ec19585e9b | ||
|
|
9ced6600e3 | ||
|
|
b78cc02d5c |
2
Makefile
2
Makefile
@@ -5,7 +5,7 @@ GO?= go
|
||||
all: build
|
||||
|
||||
build:
|
||||
$(GO) build -o lash ./cmd/lash/
|
||||
$(GO) build -o lash ./bin/main.go
|
||||
|
||||
lint:
|
||||
$(GO) fmt ./...
|
||||
|
||||
20
README.md
20
README.md
@@ -1 +1,21 @@
|
||||
# LASH (Local Area Share HTTP)
|
||||
|
||||
## Usage
|
||||
Using lash is pretty simple. You just provide any amount of command line
|
||||
arguments and it will generate a share link, and a code. You either go-to
|
||||
http://<your-local-ip>:port or goto http://<your-share-link> to access the
|
||||
files.
|
||||
|
||||
## Building from Source
|
||||
Lash only has one dependency and that it is Go >= 1.26.3. To build it you just
|
||||
run either of the two following commands.
|
||||
|
||||
```sh
|
||||
$ make
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```sh
|
||||
$ go build -o lash ./bin/main.go
|
||||
```
|
||||
|
||||
8
TODO
8
TODO
@@ -1,7 +1 @@
|
||||
maint: clean up source code
|
||||
maint: document more
|
||||
maint: make the colors more cross platform
|
||||
feat: improve flags
|
||||
feat: replace uuid dep with custom id generator
|
||||
feat: multiple files
|
||||
fix: remove `_` prefix
|
||||
feat: sumbit code with press of enter key
|
||||
|
||||
@@ -8,10 +8,7 @@ import (
|
||||
"lash/internal/app"
|
||||
"lash/internal/errx"
|
||||
share "lash/internal/shareLink"
|
||||
"net"
|
||||
"net/http"
|
||||
|
||||
"github.com/hashicorp/mdns"
|
||||
)
|
||||
|
||||
var versionFlag = flag.Bool("version", false, "Print out version and exit.")
|
||||
@@ -38,22 +35,9 @@ func main() {
|
||||
Handler: srv,
|
||||
}
|
||||
|
||||
info := []string{"lash"}
|
||||
service, err := mdns.NewMDNSService("lash", "__foobar.__tcp", "", "", *port, []net.IP{net.ParseIP(share.GetLocalIP())}, info)
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
|
||||
mdns_server, err := mdns.NewServer(&mdns.Config{Zone: service})
|
||||
defer mdns_server.Shutdown()
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
|
||||
// TODO(vxc): Make this more portable
|
||||
errx.Log("Your share link is %s", share.GenerateShareLink(*port))
|
||||
errx.Log("Your token is \033[1;92m%s\033[0m", lash.Token)
|
||||
errx.Log("starting server at http://0.0.0.0:%d", *port)
|
||||
errx.Log("starting server at http://%s:%d", share.GetLocalIP(), *port)
|
||||
if err := server.ListenAndServe(); err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
11
go.mod
11
go.mod
@@ -1,14 +1,3 @@
|
||||
module lash
|
||||
|
||||
go 1.26.3
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/hashicorp/mdns v1.0.6 // indirect
|
||||
github.com/miekg/dns v1.1.55 // indirect
|
||||
golang.org/x/mod v0.17.0 // indirect
|
||||
golang.org/x/net v0.34.0 // indirect
|
||||
golang.org/x/sync v0.10.0 // indirect
|
||||
golang.org/x/sys v0.29.0 // indirect
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d // indirect
|
||||
)
|
||||
|
||||
82
go.sum
82
go.sum
@@ -1,82 +0,0 @@
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/hashicorp/mdns v1.0.6 h1:SV8UcjnQ/+C7KeJ/QeVD/mdN2EmzYfcGfufcuzxfCLQ=
|
||||
github.com/hashicorp/mdns v1.0.6/go.mod h1:X4+yWh+upFECLOki1doUPaKpgNQII9gy4bUdCYKNhmM=
|
||||
github.com/miekg/dns v1.1.55 h1:GoQ4hpsj0nFLYe+bWiCToyrBEJXkQfOOIvFGFy0lEgo=
|
||||
github.com/miekg/dns v1.1.55/go.mod h1:uInx36IzPl7FYnDcMeVWxj9byh7DutNykX4G9Sj60FY=
|
||||
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
|
||||
golang.org/x/crypto v0.13.0/go.mod h1:y6Z2r+Rw4iayiXXAIxJIDAJ1zMW4yaTpebo8fPOliYc=
|
||||
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
|
||||
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
|
||||
golang.org/x/crypto v0.32.0/go.mod h1:ZnnJkOaASj8g0AjIduWNlq2NRxL0PlBrbKVyZ6V/Ugc=
|
||||
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
|
||||
golang.org/x/mod v0.7.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
|
||||
golang.org/x/mod v0.15.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
|
||||
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
|
||||
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
|
||||
golang.org/x/net v0.2.0/go.mod h1:KqCZLdyyvdV855qA2rE3GC2aiw5xGR5TEjj8smXukLY=
|
||||
golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
|
||||
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
|
||||
golang.org/x/net v0.15.0/go.mod h1:idbUs1IY1+zTqbi8yxTbhexhEEk5ur9LInksu6HrEpk=
|
||||
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
|
||||
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
|
||||
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
|
||||
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y=
|
||||
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sync v0.10.0 h1:3NQrjDixjgGwUOCaF8w2+VYHv0Ve/vGYSbdkTa98gmQ=
|
||||
golang.org/x/sync v0.10.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
|
||||
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/telemetry v0.0.0-20240228155512-f48c80bd79b2/go.mod h1:TeRTkGYfJXctD9OcfyVLyj2J3IxLnKwHJR8f4D8a3YE=
|
||||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
|
||||
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
|
||||
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
|
||||
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
|
||||
golang.org/x/term v0.8.0/go.mod h1:xPskH00ivmX89bAKVGSKKtLOWNx2+17Eiy94tnKShWo=
|
||||
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
|
||||
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
|
||||
golang.org/x/term v0.20.0/go.mod h1:8UkIAJTvZgivsXaD6/pH6U9ecQzZ45awqEOzuCvwpFY=
|
||||
golang.org/x/term v0.28.0/go.mod h1:Sw/lC2IAUZ92udQNf3WodGtn4k/XoLyZoh8v/8uiwek=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
|
||||
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
|
||||
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
|
||||
golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
|
||||
golang.org/x/tools v0.3.0/go.mod h1:/rWhSS2+zyEVwoJf8YAX6L2f0ntZ7Kn/mGgAWcipA5k=
|
||||
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
|
||||
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d h1:vU5i/LfpvrRCpgM/VPfJLg5KjxD3E+hfT1SH+d9zLwg=
|
||||
golang.org/x/tools v0.21.1-0.20240508182429-e35e4ccd0d2d/go.mod h1:aiJjzUbINMkxbQROHiO6hDPo2LHcIPhhQsa9DLh0yGk=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
||||
@@ -1,19 +0,0 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"flag"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetFilePath() (string, error) {
|
||||
fp := ""
|
||||
|
||||
for _, v := range flag.Args() {
|
||||
if !strings.HasPrefix(v, "-") {
|
||||
fp = v
|
||||
return fp, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("not enough arguments")
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"lash"
|
||||
"lash/internal/errx"
|
||||
"lash/internal/handlers"
|
||||
@@ -11,26 +12,29 @@ import (
|
||||
func New(ctx *lash.LashContext) http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
fp, err := GetFilePath()
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
|
||||
contents, err := os.ReadFile(fp)
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
|
||||
share := handlers.ShareData{
|
||||
Version: lash.Version,
|
||||
}
|
||||
|
||||
file := handlers.FileHandler{
|
||||
Ctx: ctx,
|
||||
FileData: handlers.FileData{
|
||||
}
|
||||
|
||||
fps := flag.Args()
|
||||
if len(fps) < 1 {
|
||||
errx.FatalPerror(errx.ErrNotEnoughArgs)
|
||||
}
|
||||
|
||||
for _, fp := range fps {
|
||||
contents, err := os.ReadFile(fp)
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
|
||||
file.FileData = append(file.FileData, handlers.FileData{
|
||||
Contents: contents,
|
||||
FileName: fp,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
mux.HandleFunc("/", share.Handler)
|
||||
|
||||
5
internal/errx/define.go
Normal file
5
internal/errx/define.go
Normal file
@@ -0,0 +1,5 @@
|
||||
package errx
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrNotEnoughArgs error = errors.New("not enough arguments")
|
||||
55
internal/generator/generator.go
Normal file
55
internal/generator/generator.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package generator
|
||||
|
||||
import (
|
||||
"math/rand/v2"
|
||||
)
|
||||
|
||||
var keys []string = []string{
|
||||
"octo",
|
||||
"cool",
|
||||
"whip",
|
||||
"keys",
|
||||
"cats",
|
||||
"part",
|
||||
"rate",
|
||||
"face",
|
||||
"lard",
|
||||
"larp",
|
||||
"dogs",
|
||||
"cash",
|
||||
"city",
|
||||
"cold",
|
||||
"desk",
|
||||
"down",
|
||||
"dirt",
|
||||
"long",
|
||||
"mean",
|
||||
"news",
|
||||
"only",
|
||||
"open",
|
||||
"year",
|
||||
"wood",
|
||||
"wing",
|
||||
"work",
|
||||
"wash",
|
||||
"vote",
|
||||
"onyx",
|
||||
"snow",
|
||||
"john",
|
||||
"json",
|
||||
"fang",
|
||||
}
|
||||
|
||||
func Generate(n int) (code string) {
|
||||
codeLen := n
|
||||
|
||||
start := rand.IntN(len(keys))
|
||||
|
||||
for i := range codeLen {
|
||||
code += keys[(start+i)%len(keys)]
|
||||
if i != codeLen-1 {
|
||||
code += "-"
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -1,17 +1,18 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"archive/zip"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"lash"
|
||||
"lash/internal/errx"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type FileHandler struct {
|
||||
Ctx *lash.LashContext
|
||||
FileData FileData
|
||||
FileData []FileData
|
||||
}
|
||||
|
||||
type FileData struct {
|
||||
@@ -26,11 +27,6 @@ type ValidateRequest struct {
|
||||
var sent int = 0
|
||||
|
||||
func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if sent >= h.Ctx.N && h.Ctx.N != -1 {
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
os.Exit(0)
|
||||
return
|
||||
}
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
|
||||
var t ValidateRequest
|
||||
@@ -45,27 +41,43 @@ func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileData.FileName))
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData.Contents)))
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(h.FileData.Contents)
|
||||
sent++
|
||||
http.Redirect(w, r, fmt.Sprintf("/%s", lash.ShareLinkToken), http.StatusTemporaryRedirect)
|
||||
}
|
||||
|
||||
func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
|
||||
if sent >= h.Ctx.N && h.Ctx.N != -1 {
|
||||
w.WriteHeader(http.StatusTooManyRequests)
|
||||
os.Exit(0)
|
||||
http.Error(w, "Too many requests", http.StatusTooManyRequests)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileData.FileName))
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData.Contents)))
|
||||
filename := h.FileData[0].FileName
|
||||
if len(h.FileData) > 1 {
|
||||
filename = "lash.zip"
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, filename))
|
||||
w.Header().Set("Content-Type", "application/octet-stream")
|
||||
|
||||
if len(h.FileData) == 1 {
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData[0].Contents)))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(h.FileData[0].Contents)
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
zw := zip.NewWriter(w)
|
||||
defer zw.Close()
|
||||
|
||||
for _, f := range h.FileData {
|
||||
fw, err := zw.Create(f.FileName)
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
_, err = fw.Write(f.Contents)
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write(h.FileData.Contents)
|
||||
sent++
|
||||
}
|
||||
|
||||
@@ -13,7 +13,6 @@ type ShareData struct {
|
||||
}
|
||||
|
||||
func (h *ShareData) Handler(w http.ResponseWriter, r *http.Request) {
|
||||
// Although `Must` can fail since `Templates` is embeded these files will always exist.
|
||||
tmpl := template.Must(template.ParseFS(lash.Templates, "templates/share.html"))
|
||||
|
||||
if err := tmpl.ExecuteTemplate(w, "share.html", h); err != nil {
|
||||
|
||||
8
lash.go
8
lash.go
@@ -2,9 +2,8 @@ package lash
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"lash/internal/generator"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type LashContext struct {
|
||||
@@ -18,7 +17,6 @@ var Templates embed.FS
|
||||
//go:embed version
|
||||
var Version string
|
||||
|
||||
// TODO(vxc): Replace this with custom token generator
|
||||
var Token string = uuid.New().String()
|
||||
var Token string = generator.Generate(5)
|
||||
|
||||
var ShareLinkToken string = uuid.New().String()
|
||||
var ShareLinkToken string = generator.Generate(8)
|
||||
|
||||
Reference in New Issue
Block a user