create raw endpoint, link to it for attachments
parent
8fefc60f6b
commit
479caef353
|
|
@ -4,8 +4,10 @@ import (
|
|||
"fmt"
|
||||
"local/notes-server/config"
|
||||
"local/notes-server/filetree"
|
||||
"log"
|
||||
"net/http"
|
||||
"path"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
|
|
@ -83,7 +85,7 @@ func (s *Server) htmlAttachments(w http.ResponseWriter, urlPath string) {
|
|||
dir := path.Dir(urlPath)
|
||||
f := "." + path.Base(urlPath) + ".attachments"
|
||||
_, files, _ := s.Notes.Dir(path.Join(dir, f))
|
||||
// TODO: direct DL via link
|
||||
// TODO replace <a with <a download UNLESS img, then... hrm
|
||||
form := fmt.Sprintf(`
|
||||
<form enctype="multipart/form-data" action="/attach/%s" method="post">
|
||||
<input type="file" name="file" required/>
|
||||
|
|
@ -93,6 +95,16 @@ func (s *Server) htmlAttachments(w http.ResponseWriter, urlPath string) {
|
|||
if config.ReadOnly {
|
||||
form = ""
|
||||
}
|
||||
lines := strings.Split(files, "\n")
|
||||
for i := range lines {
|
||||
pattern := regexp.MustCompile(`\.(png|jpg|jpeg|gif)">`)
|
||||
if !pattern.MatchString(lines[i]) {
|
||||
lines[i] = strings.ReplaceAll(lines[i], "<a", "<a download")
|
||||
}
|
||||
lines[i] = strings.ReplaceAll(lines[i], `href="/notes`, `href="/raw`)
|
||||
}
|
||||
files = strings.Join(lines, "\n")
|
||||
log.Println(files)
|
||||
fmt.Fprintf(w, `<div style='display:inline-block' class="attachments">
|
||||
<details>
|
||||
<summary style="display: flex">
|
||||
|
|
|
|||
|
|
@ -0,0 +1,33 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"io"
|
||||
"local/notes-server/filetree"
|
||||
"local/simpleserve/simpleserve"
|
||||
"net/http"
|
||||
"os"
|
||||
)
|
||||
|
||||
func (s *Server) raw(w http.ResponseWriter, r *http.Request) {
|
||||
if err := s._raw(w, r); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) _raw(w http.ResponseWriter, r *http.Request) error {
|
||||
simpleserve.SetContentTypeIfMedia(w, r)
|
||||
p := filetree.NewPathFromURL(r.URL.Path)
|
||||
if !p.IsFile() {
|
||||
http.NotFound(w, r)
|
||||
return nil
|
||||
}
|
||||
f, err := os.Open(p.Local)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
if _, err := io.Copy(w, f); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
@ -18,6 +18,9 @@ func (s *Server) Routes() error {
|
|||
"/": {
|
||||
handler: s.root,
|
||||
},
|
||||
fmt.Sprintf("raw/%s%s", wildcard, wildcard): {
|
||||
handler: s.gzip(s.authenticate(s.raw)),
|
||||
},
|
||||
fmt.Sprintf("notes/%s%s", wildcard, wildcard): {
|
||||
handler: s.gzip(s.authenticate(s.notes)),
|
||||
},
|
||||
|
|
|
|||
Loading…
Reference in New Issue