download token: make a token page

- token page helps communicating a single link to multiple assets
- provide an extra layer in case of "miss click"
- ui: just link the page, not every asset of each download token.
This commit is contained in:
Mikaël Cluseau
2025-07-22 11:43:41 +02:00
parent ab6f0b6358
commit d4087d3534
3 changed files with 54 additions and 10 deletions

View File

@ -1,8 +1,10 @@
package main
import (
"bytes"
"crypto/rand"
"encoding/base32"
"fmt"
"log"
"net/http"
"strconv"
@ -149,3 +151,45 @@ func wsDownload(req *restful.Request, resp *restful.Response) {
wsNotFound(resp)
}
}
func wsDownloadPage(req *restful.Request, resp *restful.Response) {
token := req.PathParameter("token")
spec, ok := wState.Get().Downloads[token]
if !ok {
resp.WriteHeader(http.StatusNotFound)
resp.Write([]byte(`<!doctype html>
<html>
<head>
<title>Token not found</title>
<style>
@import url('/ui/style.css');
@import url('/ui/app.css');
</style>
</head>
<body><h1>Token not found</h1></body>
</html>`))
return
}
buf := new(bytes.Buffer)
fmt.Fprintf(buf, `<!doctype html>
<html>
<head>
<title>Token assets: %s %s</title>
<style>
@import url('/ui/style.css');
@import url('/ui/app.css');
</style>
</head>
<body><h1>Token assets: %s %s</h1>
<ul>
`, spec.Kind, spec.Name, spec.Kind, spec.Name)
for _, asset := range spec.Assets {
fmt.Fprintf(buf, "<li><a href=\"%s\" download>%s</a></li>\n", asset, asset)
}
buf.WriteString("</ul></body></html>")
buf.WriteTo(resp)
}

View File

@ -37,6 +37,7 @@ func registerWS(rest *restful.Container) {
Route(ws.POST("/store.tar").To(wsStoreUpload).
Consumes(mime.TAR).
Doc("Upload an existing store")).
Route(ws.GET("/downloads/{token}/").To(wsDownloadPage)).
Route(ws.GET("/downloads/{token}/{asset}").To(wsDownload).
Param(ws.PathParameter("token", "the download token")).
Param(ws.PathParameter("asset", "the requested asset")).