From eaf5e2fb9531ffd36b34261daeb776c19fb65e98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20=C3=85hall?= Date: Thu, 22 Jun 2023 17:42:34 +0200 Subject: [PATCH] File downloading --- main.go | 12 ++++++------ sql/0006.sql | 5 +++++ static/js/node.mjs | 5 +++-- 3 files changed, 14 insertions(+), 8 deletions(-) create mode 100644 sql/0006.sql diff --git a/main.go b/main.go index 8e43d10..aab3b37 100644 --- a/main.go +++ b/main.go @@ -20,9 +20,9 @@ import ( _ "embed" ) -const VERSION = "v0.1.4"; +const VERSION = "v0.1.5"; const LISTEN_HOST = "0.0.0.0"; -const DB_SCHEMA = 5 +const DB_SCHEMA = 6 var ( flagPort int @@ -495,6 +495,10 @@ func nodeDownload(w http.ResponseWriter, r *http.Request) {// {{{ return } + w.Header().Add("Content-Type", files[0].MIME) + w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, files[0].Filename)) + w.Header().Add("Content-Length", strconv.Itoa(int(finfo.Size()))) + read := 1 var buf []byte for read > 0 { @@ -505,10 +509,6 @@ func nodeDownload(w http.ResponseWriter, r *http.Request) {// {{{ } } - w.Header().Add("Content-Type", files[0].MIME) - w.Header().Add("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, files[0].Filename)) - w.Header().Add("Content-Length", strconv.Itoa(int(finfo.Size()))) - }// }}} func nodeFiles(w http.ResponseWriter, r *http.Request) {// {{{ var err error diff --git a/sql/0006.sql b/sql/0006.sql new file mode 100644 index 0000000..8d886ad --- /dev/null +++ b/sql/0006.sql @@ -0,0 +1,5 @@ +ALTER TABLE public.file DROP CONSTRAINT file_node_fk; +ALTER TABLE public.file ADD CONSTRAINT file_node_fk FOREIGN KEY (node_id) REFERENCES public.node(id) ON DELETE CASCADE ON UPDATE CASCADE; + +ALTER TABLE public.file DROP CONSTRAINT file_fk; +ALTER TABLE public.file ADD CONSTRAINT file_user_fk FOREIGN KEY (user_id) REFERENCES public."user"(id) ON DELETE CASCADE ON UPDATE CASCADE; diff --git a/static/js/node.mjs b/static/js/node.mjs index 1b711a8..a72139e 100644 --- a/static/js/node.mjs +++ b/static/js/node.mjs @@ -326,14 +326,15 @@ class Node { }), }) .then(response=>{ - console.log(...response.headers); + let match = response.headers.get('content-disposition').match(/filename="([^"]*)"/) + fname = match[1] return response.blob() }) .then(blob=>{ let url = window.URL.createObjectURL(blob) let a = document.createElement('a'); a.href = url; - a.download = "filename.xlsx"; + a.download = fname; document.body.appendChild(a); // we need to append the element to the dom -> otherwise it will not work in firefox a.click(); a.remove(); //afterwards we remove the element again