From 479caef35332ad19380f817dc12e7ab06127f7d0 Mon Sep 17 00:00:00 2001 From: Bel LaPointe Date: Thu, 25 Feb 2021 19:28:44 -0600 Subject: [PATCH] create raw endpoint, link to it for attachments --- server/notes.go | 14 +++++++++++++- server/raw.go | 33 +++++++++++++++++++++++++++++++++ server/routes.go | 3 +++ 3 files changed, 49 insertions(+), 1 deletion(-) create mode 100755 server/raw.go diff --git a/server/notes.go b/server/notes.go index 48efd6d..6eadf03 100755 --- a/server/notes.go +++ b/server/notes.go @@ -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 @@ -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], "
diff --git a/server/raw.go b/server/raw.go new file mode 100755 index 0000000..655a23b --- /dev/null +++ b/server/raw.go @@ -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 +} diff --git a/server/routes.go b/server/routes.go index c075401..d110a59 100755 --- a/server/routes.go +++ b/server/routes.go @@ -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)), },