Files
lash/internal/handlers/file.go
vxclutch 459fe380db save
2026-06-02 14:53:21 -04:00

83 lines
1.7 KiB
Go

package handlers
import (
"archive/zip"
"encoding/json"
"fmt"
"lash"
"lash/internal/errx"
"net/http"
"os"
"strconv"
)
type FileHandler struct {
Ctx *lash.LashContext
FileData []FileData
}
type FileData struct {
Contents []byte
FileName string
}
type ValidateRequest struct {
Token string
}
var sent int = 0
func (h FileHandler) APIHandler(w http.ResponseWriter, r *http.Request) {
decoder := json.NewDecoder(r.Body)
var t ValidateRequest
err := decoder.Decode(&t)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if t.Token != lash.Token {
http.Error(w, "Invalid Token", http.StatusUnauthorized)
return
}
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)
return
}
w.WriteHeader(http.StatusOK)
if len(h.FileData) == 1 {
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=%s", h.FileData[0].FileName))
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Length", strconv.Itoa(len(h.FileData[0].FileName)))
w.Write(h.FileData[0].Contents)
} else {
w.Header().Set("Content-Disposition", "attachment; filename=lash.zip")
w.Header().Set("Content-Type", "application/octet-stream")
zw := zip.NewWriter(w)
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)
}
}
}
sent++
}