53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"lash"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
type FileData struct {
|
|
Contents []byte
|
|
FileName string
|
|
}
|
|
|
|
type ValidateRequest struct {
|
|
Token string
|
|
}
|
|
|
|
func (h FileData) 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
|
|
}
|
|
|
|
// 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)
|
|
}
|
|
|
|
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)
|
|
}
|