Compare commits

..

4 Commits

Author SHA1 Message Date
vxclutch
35d908cd73 maint: update todo 2026-06-03 08:26:20 -04:00
vxclutch
f188264f39 maint: done with todo 2026-06-03 08:25:50 -04:00
vxclutch
1436b8cf2f save 2026-06-03 08:23:45 -04:00
vxclutch
459fe380db save 2026-06-02 14:53:21 -04:00
5 changed files with 44 additions and 84 deletions

7
TODO
View File

@@ -1,6 +1 @@
maint: document more
feat: improve flags
feat: replace uuid dep with custom id generator
feat: multiple files
fix: remove `_` prefix
fix: make mDNS only build on windows
feat: sumbit code with press of enter key

View File

@@ -34,6 +34,10 @@ var keys []string = []string{
"wash",
"vote",
"onyx",
"snow",
"john",
"json",
"fang",
}
func Generate(n int) (code string) {

View File

@@ -7,7 +7,6 @@ import (
"lash"
"lash/internal/errx"
"net/http"
"os"
"strconv"
)
@@ -25,21 +24,9 @@ type ValidateRequest struct {
Token string
}
type headers map[string]string
var zipHeaders headers = headers{
"Content-Disposition": "attachment; filename=lash.zip",
"Content-Type": "application/octet-stream",
}
var sent int = 0
func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
if h.hasHitMax() {
w.WriteHeader(http.StatusTooManyRequests)
os.Exit(0)
return
}
decoder := json.NewDecoder(r.Body)
var t ValidateRequest
@@ -54,71 +41,43 @@ func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
return
}
w.WriteHeader(http.StatusOK)
if len(h.FileData) == 1 {
headers := headers{
"Content-Disposition": fmt.Sprintf("attachment; filename=%s", h.FileData[0].FileName),
"Content-Type": "application/octet-stream",
"Content-Length": strconv.Itoa(len(h.FileData[0].FileName)),
}
headers.set(w)
w.Write(h.FileData[0].Contents)
} else {
zipHeaders.set(w)
h.writeZip(w)
}
sent++
http.Redirect(w, r, fmt.Sprintf("/%s", lash.ShareLinkToken), http.StatusTemporaryRedirect)
}
func (h FileHandler) FileHandler(w http.ResponseWriter, r *http.Request) {
if h.hasHitMax() {
w.WriteHeader(http.StatusTooManyRequests)
os.Exit(0)
if sent >= h.Ctx.N && h.Ctx.N != -1 {
http.Error(w, "Too many requests", http.StatusTooManyRequests)
return
}
w.WriteHeader(http.StatusOK)
if len(h.FileData) == 1 {
headers := headers{
"Content-Disposition": fmt.Sprintf("attachment; filename=%s", h.FileData[0].FileName),
"Content-Type": "application/octet-stream",
"Content-Length": strconv.Itoa(len(h.FileData[0].FileName)),
}
headers.set(w)
w.Write(h.FileData[0].Contents)
} else {
zipHeaders.set(w)
h.writeZip(w)
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)
}
}
}
sent++
}
func (h FileHandler) writeZip(hw http.ResponseWriter) {
zw := zip.NewWriter(hw)
defer zw.Close()
for _, f := range h.FileData {
w, err := zw.Create(f.FileName)
if err != nil {
errx.FatalPerror(err)
}
_, err = w.Write(f.Contents)
if err != nil {
errx.FatalPerror(err)
}
}
}
func (h headers) set(w http.ResponseWriter) {
for k, v := range h {
w.Header().Set(k, v)
}
}
func (h FileHandler) hasHitMax() bool {
return sent >= h.Ctx.N && h.Ctx.N != -1
}

View File

@@ -17,6 +17,6 @@ var Templates embed.FS
//go:embed version
var Version string
var Token string = generator.Generate(5)
var Token string = generator.Generate(1)
var ShareLinkToken string = generator.Generate(8)
var ShareLinkToken string = generator.Generate(1)

View File

@@ -24,10 +24,11 @@
<script>
const tokenInput = document.getElementById('tokenInput');
const submitBtn = document.getElementById('submitBtn');
const status = document.getElementById('status');
submitBtn.addEventListener('click', async () => {
const token = tokenInput.value.trim();
if (!token) {
status.textContent = 'Please enter a token.';
return;
@@ -41,6 +42,7 @@
},
body: JSON.stringify({ token })
});
if (!response.ok) {
status.textContent = 'Download failed.';
return;
@@ -48,19 +50,19 @@
const blob = await response.blob();
const url = window.URL.createObjectURL(blob);
a.href = url;
const a = document.createElement('a');
a.href = url;
const disposition = response.headers.get('Content-Disposition');
let filename = 'download.bin';
if (disposition && disposition.includes('filename=')) {
filename = disposition
.split('filename=')[1]
.replace(/"/g, '');
}
a.download = filename;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();