Compare commits
2 Commits
6da1bb9fc1
...
1234288e03
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1234288e03 | ||
|
|
7ee19a2883 |
@@ -5,6 +5,7 @@ import (
|
|||||||
"lash/internal/errx"
|
"lash/internal/errx"
|
||||||
"lash/internal/handlers"
|
"lash/internal/handlers"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
func New() http.Handler {
|
func New() http.Handler {
|
||||||
@@ -15,12 +16,19 @@ func New() http.Handler {
|
|||||||
errx.FatalPerror(err)
|
errx.FatalPerror(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
share := handlers.ShareData{
|
contents, err := os.ReadFile(fp)
|
||||||
Version: lash.Version,
|
if err != nil {
|
||||||
FileName: fp,
|
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("/", share.Handler)
|
||||||
mux.HandleFunc("/api/receive-token", file.APIHandler)
|
mux.HandleFunc("/api/receive-token", file.APIHandler)
|
||||||
|
|||||||
@@ -2,13 +2,15 @@ package handlers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"lash"
|
"lash"
|
||||||
"lash/internal/errx"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type FileData struct {
|
type FileData struct {
|
||||||
Contents []byte
|
Contents []byte
|
||||||
|
FileName string
|
||||||
}
|
}
|
||||||
|
|
||||||
type ValidateRequest struct {
|
type ValidateRequest struct {
|
||||||
@@ -17,19 +19,23 @@ type ValidateRequest struct {
|
|||||||
|
|
||||||
func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) {
|
func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
decoder := json.NewDecoder(r.Body)
|
decoder := json.NewDecoder(r.Body)
|
||||||
|
|
||||||
var t ValidateRequest
|
var t ValidateRequest
|
||||||
err := decoder.Decode(&t)
|
err := decoder.Decode(&t)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
errx.FatalPerror(err)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if t.Token == lash.Token {
|
if t.Token != lash.Token {
|
||||||
errx.Log("Got token")
|
http.Error(w, "Invalid Token", http.StatusUnauthorized)
|
||||||
} else {
|
return
|
||||||
errx.Log("No Token")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
w.Write(h.Contents)
|
||||||
}
|
}
|
||||||
|
|||||||
5
lash.go
5
lash.go
@@ -3,7 +3,7 @@ package lash
|
|||||||
import (
|
import (
|
||||||
"embed"
|
"embed"
|
||||||
|
|
||||||
// "github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
//go:embed templates/*.html
|
//go:embed templates/*.html
|
||||||
@@ -12,5 +12,4 @@ var Templates embed.FS
|
|||||||
//go:embed version
|
//go:embed version
|
||||||
var Version string
|
var Version string
|
||||||
|
|
||||||
// var Token string = uuid.New().String()
|
var Token string = uuid.New().String()
|
||||||
var Token string = "foo"
|
|
||||||
|
|||||||
@@ -1,20 +1,78 @@
|
|||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8" />
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||||
<title>Lash v{{.Version}}</title>
|
<title>Lash v{{.Version}}</title>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
</body>
|
<h1>Enter Token</h1>
|
||||||
|
|
||||||
|
<input
|
||||||
|
type="text"
|
||||||
|
id="tokenInput"
|
||||||
|
placeholder="Enter token"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<button id="submitBtn">
|
||||||
|
Submit
|
||||||
|
</button>
|
||||||
|
|
||||||
|
<p id="status"></p>
|
||||||
|
|
||||||
<script>
|
<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',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: {
|
||||||
body: JSON.stringify({ token: "foo" })
|
'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>
|
</script>
|
||||||
|
</body>
|
||||||
</html>
|
</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