Compare commits
2 Commits
6da1bb9fc1
...
1234288e03
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1234288e03 | ||
|
|
7ee19a2883 |
@@ -5,6 +5,7 @@ import (
|
||||
"lash/internal/errx"
|
||||
"lash/internal/handlers"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func New() http.Handler {
|
||||
@@ -15,12 +16,19 @@ func New() http.Handler {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
|
||||
share := handlers.ShareData{
|
||||
Version: lash.Version,
|
||||
FileName: fp,
|
||||
contents, err := os.ReadFile(fp)
|
||||
if err != nil {
|
||||
errx.FatalPerror(err)
|
||||
}
|
||||
|
||||
file := handlers.FileData{}
|
||||
share := handlers.ShareData{
|
||||
Version: lash.Version,
|
||||
}
|
||||
|
||||
file := handlers.FileData{
|
||||
Contents: contents,
|
||||
FileName: fp,
|
||||
}
|
||||
|
||||
mux.HandleFunc("/", share.Handler)
|
||||
mux.HandleFunc("/api/receive-token", file.APIHandler)
|
||||
|
||||
@@ -2,13 +2,15 @@ package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"lash"
|
||||
"lash/internal/errx"
|
||||
"net/http"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type FileData struct {
|
||||
Contents []byte
|
||||
FileName string
|
||||
}
|
||||
|
||||
type ValidateRequest struct {
|
||||
@@ -17,19 +19,23 @@ type ValidateRequest struct {
|
||||
|
||||
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 {
|
||||
errx.FatalPerror(err)
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if t.Token == lash.Token {
|
||||
errx.Log("Got token")
|
||||
} else {
|
||||
errx.Log("No Token")
|
||||
}
|
||||
if t.Token != lash.Token {
|
||||
http.Error(w, "Invalid Token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
func (h FileData) DownloadHandler(w http.ResponseWriter, r *http.Request) {
|
||||
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)
|
||||
}
|
||||
|
||||
5
lash.go
5
lash.go
@@ -3,7 +3,7 @@ package lash
|
||||
import (
|
||||
"embed"
|
||||
|
||||
// "github.com/google/uuid"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
//go:embed templates/*.html
|
||||
@@ -12,5 +12,4 @@ var Templates embed.FS
|
||||
//go:embed version
|
||||
var Version string
|
||||
|
||||
// var Token string = uuid.New().String()
|
||||
var Token string = "foo"
|
||||
var Token string = uuid.New().String()
|
||||
|
||||
@@ -1,20 +1,78 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Lash v{{.Version}}</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
</body>
|
||||
<h1>Enter Token</h1>
|
||||
|
||||
<input
|
||||
type="text"
|
||||
id="tokenInput"
|
||||
placeholder="Enter token"
|
||||
/>
|
||||
|
||||
<button id="submitBtn">
|
||||
Submit
|
||||
</button>
|
||||
|
||||
<p id="status"></p>
|
||||
|
||||
<script>
|
||||
await fetch('/api/receive-token', {
|
||||
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;
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch('/api/receive-token', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ token: "foo" })
|
||||
headers: {
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
body: JSON.stringify({ token })
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
status.textContent = 'Download failed.';
|
||||
return;
|
||||
}
|
||||
|
||||
const blob = await response.blob();
|
||||
const url = window.URL.createObjectURL(blob);
|
||||
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;
|
||||
document.body.appendChild(a);
|
||||
a.click();
|
||||
a.remove();
|
||||
window.URL.revokeObjectURL(url);
|
||||
status.textContent = 'Download started.';
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
status.textContent = 'An error occurred.';
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
5
todo
Normal file
5
todo
Normal file
@@ -0,0 +1,5 @@
|
||||
maint: clean up source code
|
||||
maint: comment source code
|
||||
maint: document more
|
||||
feat: improve flags
|
||||
feat: replace uuid dep with custom id generator
|
||||
Reference in New Issue
Block a user