diff --git a/internal/app/filePath.go b/internal/app/file.go similarity index 100% rename from internal/app/filePath.go rename to internal/app/file.go diff --git a/internal/app/routes.go b/internal/app/routes.go index 07404c6..264d28d 100644 --- a/internal/app/routes.go +++ b/internal/app/routes.go @@ -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) diff --git a/internal/handlers/file.go b/internal/handlers/file.go index ca9b78e..e2a0236 100644 --- a/internal/handlers/file.go +++ b/internal/handlers/file.go @@ -2,13 +2,15 @@ package handlers import ( "encoding/json" + "fmt" "lash" - "strconv" "net/http" + "strconv" ) type FileData struct { Contents []byte + FileName string } type ValidateRequest struct { @@ -30,7 +32,7 @@ func (h FileData) APIHandler(w http.ResponseWriter, r *http.Request) { return } - w.Header().Set("Content-Disposition", "attachment; filename=file.bin") + 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))) diff --git a/templates/share.html b/templates/share.html index 41bc86d..f0a5138 100644 --- a/templates/share.html +++ b/templates/share.html @@ -22,26 +22,57 @@

+ + 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.'; + } + }); + diff --git a/todo b/todo new file mode 100644 index 0000000..058ebe9 --- /dev/null +++ b/todo @@ -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 diff --git a/version b/version index 6e8bf73..3eefcb9 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.1.0 +1.0.0