summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-09-10 22:54:37 +0200
committerJulian T <julian@jtle.dk>2020-09-10 22:54:37 +0200
commitb36aaeaac878b6a3d8351d4a941af5cc93341dd3 (patch)
treeb3deab6b0a07abf65b918c837e386033cfe6a110
parentf400c4c9b01e572ed65f133cb3c489c2a150e4ee (diff)
Added file upload
-rw-r--r--main.go11
-rw-r--r--root.template5
2 files changed, 15 insertions, 1 deletions
diff --git a/main.go b/main.go
index 74b38b5..e6c1def 100644
--- a/main.go
+++ b/main.go
@@ -92,6 +92,9 @@ func NewServer(conf *Config) *Server {
s.mux = http.NewServeMux()
s.mux.HandleFunc("/", s.httpRoot)
s.mux.HandleFunc("/upload", s.httpUpload)
+ s.mux.Handle("/data/", http.StripPrefix("/data/",
+ http.FileServer(http.Dir(conf.DataPath))))
+
s.conf = conf
s.filestore = map[string][]string{}
@@ -202,8 +205,10 @@ func (s *Server) httpRoot(w http.ResponseWriter, r *http.Request) {
var page struct {
Notes []note
- Msg string
+ Msg string
+ Files *map[string][]string
}
+ page.Files = &s.filestore
err := s.db.SelectContext(r.Context(), &page.Notes, `
SELECT hash, name, location FROM notes WHERE available = True`)
@@ -212,7 +217,9 @@ func (s *Server) httpRoot(w http.ResponseWriter, r *http.Request) {
return
}
+ s.fslock.RLock()
s.renderTemplate(w, &page, "root.template")
+ s.fslock.RUnlock()
}
func (s *Server) httpUpload(w http.ResponseWriter, r *http.Request) {
@@ -262,6 +269,8 @@ func (s *Server) httpUpload(w http.ResponseWriter, r *http.Request) {
s.saveFile(hash, fname)
s.httpLog(r, "Uploaded file %s", fname)
+
+ http.Redirect(w, r, "/", http.StatusSeeOther)
}
func (s *Server) allocFile(hash string, t string) string {
diff --git a/root.template b/root.template
index e4f5104..74fbc0c 100644
--- a/root.template
+++ b/root.template
@@ -116,11 +116,16 @@
<tr>
<th>Ref</th>
<th>Navn</th>
+ <th>Files</th>
</tr>
{{ range .Notes }}
<tr>
<td style="font-family: monospace;">#{{ .Hash }}/{{ .Location }}</td>
<td>{{ .Name }}</td>
+ {{ $files := index $.Files .Hash }}
+ <td>{{ range $i, $f := $files }}
+ <a href="/data/{{$f}}">[{{ $i }}] </a>
+ {{ end }}</td>
</tr>
{{ end }}
</table>