Render Mermaid diagrams and worktree images in agent Markdown
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"g.fc.run/misc/byre/internal/api"
|
||||
)
|
||||
|
||||
var worktreeImageTypes = map[string]string{
|
||||
".png": "image/png",
|
||||
".jpg": "image/jpeg",
|
||||
".jpeg": "image/jpeg",
|
||||
".gif": "image/gif",
|
||||
".webp": "image/webp",
|
||||
".svg": "image/svg+xml",
|
||||
}
|
||||
|
||||
func (s *Server) GetTaskImage(w http.ResponseWriter, r *http.Request, taskID string, params api.GetTaskImageParams) {
|
||||
t, err := s.store.GetTask(r.Context(), taskID)
|
||||
if err != nil {
|
||||
writeStoreError(w, err)
|
||||
return
|
||||
}
|
||||
if t.Status != api.TaskStatusActive {
|
||||
writeError(w, http.StatusConflict, fmt.Errorf("task %s is %s and has no worktree", t.Id, t.Status))
|
||||
return
|
||||
}
|
||||
|
||||
root, err := filepath.EvalSymlinks(t.WorktreePath)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
path := params.Path
|
||||
if !filepath.IsAbs(path) {
|
||||
path = filepath.Join(t.WorktreePath, path)
|
||||
}
|
||||
real, err := filepath.EvalSymlinks(path)
|
||||
if errors.Is(err, fs.ErrNotExist) {
|
||||
writeError(w, http.StatusNotFound, fmt.Errorf("%s does not exist", params.Path))
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
writeError(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
rel, err := filepath.Rel(root, real)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+string(filepath.Separator)) {
|
||||
writeError(w, http.StatusForbidden, fmt.Errorf("%s is outside the task's worktree", params.Path))
|
||||
return
|
||||
}
|
||||
|
||||
contentType := worktreeImageTypes[strings.ToLower(filepath.Ext(real))]
|
||||
if contentType == "" {
|
||||
writeError(w, http.StatusBadRequest, fmt.Errorf("%s is not a PNG, JPEG, GIF, WebP or SVG file", params.Path))
|
||||
return
|
||||
}
|
||||
|
||||
f, err := os.Open(real)
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
writeError(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
if !info.Mode().IsRegular() {
|
||||
writeError(w, http.StatusBadRequest, fmt.Errorf("%s is not a file", params.Path))
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", contentType)
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Content-Security-Policy", "default-src 'none'; style-src 'unsafe-inline'; sandbox")
|
||||
w.Header().Set("Cache-Control", "private, no-cache")
|
||||
http.ServeContent(w, r, "", info.ModTime(), f)
|
||||
}
|
||||
@@ -6,8 +6,10 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
@@ -116,22 +118,25 @@ func TestServesEmbeddedSPA(t *testing.T) {
|
||||
t.Cleanup(hs.Close)
|
||||
|
||||
for path, contentType := range map[string]string{
|
||||
"/": "text/html",
|
||||
"/style.css": "text/css",
|
||||
"/dist/app.js": "text/javascript",
|
||||
"/dist/app.js.map": "",
|
||||
"/src/app.ts": "",
|
||||
"/dist/inbox.js": "text/javascript",
|
||||
"/dist/api.gen.js": "text/javascript",
|
||||
"/dist/tasks.js": "text/javascript",
|
||||
"/dist/navbar.js": "text/javascript",
|
||||
"/dist/panes.js": "text/javascript",
|
||||
"/dist/task.js": "text/javascript",
|
||||
"/dist/state.js": "text/javascript",
|
||||
"/dist/dom.js": "text/javascript",
|
||||
"/dist/api.js": "text/javascript",
|
||||
"/dist/settings.js": "text/javascript",
|
||||
"/sw.js": "text/javascript",
|
||||
"/": "text/html",
|
||||
"/style.css": "text/css",
|
||||
"/dist/app.js": "text/javascript",
|
||||
"/dist/app.js.map": "",
|
||||
"/src/app.ts": "",
|
||||
"/dist/inbox.js": "text/javascript",
|
||||
"/dist/api.gen.js": "text/javascript",
|
||||
"/dist/tasks.js": "text/javascript",
|
||||
"/dist/navbar.js": "text/javascript",
|
||||
"/dist/panes.js": "text/javascript",
|
||||
"/dist/task.js": "text/javascript",
|
||||
"/dist/state.js": "text/javascript",
|
||||
"/dist/dom.js": "text/javascript",
|
||||
"/dist/api.js": "text/javascript",
|
||||
"/dist/settings.js": "text/javascript",
|
||||
"/sw.js": "text/javascript",
|
||||
"/dist/mermaid.js": "text/javascript",
|
||||
"/vendor/mermaid.min.js": "text/javascript",
|
||||
"/vendor/mermaid.LICENSE": "",
|
||||
"/fonts/atkinson-hyperlegible-mono-latin-wght-normal.woff2": "font/woff2",
|
||||
"/fonts/LICENSE": "",
|
||||
} {
|
||||
@@ -313,6 +318,64 @@ func TestMergeTask(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestTaskImage(t *testing.T) {
|
||||
h := newHarness(t)
|
||||
p := h.createProject("demo")
|
||||
|
||||
task := api.Task{}
|
||||
h.do("POST", "/api/projects/"+p.Id+"/tasks", api.TaskCreate{Title: "images"}, &task)
|
||||
|
||||
err := os.MkdirAll(filepath.Join(task.WorktreePath, "shots"), 0o755)
|
||||
if err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
writeFile(t, filepath.Join(task.WorktreePath, "shots", "a.png"), "\x89PNG\r\n\x1a\nfake")
|
||||
writeFile(t, filepath.Join(task.WorktreePath, "notes.txt"), "text")
|
||||
outside := filepath.Join(t.TempDir(), "secret.png")
|
||||
writeFile(t, outside, "secret")
|
||||
err = os.Symlink(outside, filepath.Join(task.WorktreePath, "link.png"))
|
||||
if err != nil {
|
||||
t.Fatalf("symlink: %v", err)
|
||||
}
|
||||
sibling := task.WorktreePath + "x"
|
||||
err = os.MkdirAll(sibling, 0o755)
|
||||
if err != nil {
|
||||
t.Fatalf("mkdir: %v", err)
|
||||
}
|
||||
writeFile(t, filepath.Join(sibling, "a.png"), "sibling")
|
||||
|
||||
for path, want := range map[string]int{
|
||||
"shots/a.png": http.StatusOK,
|
||||
filepath.Join(task.WorktreePath, "shots", "a.png"): http.StatusOK,
|
||||
"shots/../shots/a.png": http.StatusOK,
|
||||
"missing.png": http.StatusNotFound,
|
||||
"notes.txt": http.StatusBadRequest,
|
||||
"shots": http.StatusBadRequest,
|
||||
"link.png": http.StatusForbidden,
|
||||
outside: http.StatusForbidden,
|
||||
"../" + filepath.Base(sibling) + "/a.png": http.StatusForbidden,
|
||||
} {
|
||||
req, err := http.NewRequest("GET", h.url+"/api/tasks/"+task.Id+"/image?path="+url.QueryEscape(path), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("new request: %v", err)
|
||||
}
|
||||
req.Header.Set("Tailscale-User-Login", login)
|
||||
req.Header.Set("Tailscale-User-Name", "Alice")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("get %s: %v", path, err)
|
||||
}
|
||||
body, _ := io.ReadAll(resp.Body)
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode != want {
|
||||
t.Fatalf("get %s: %d %s, want %d", path, resp.StatusCode, body, want)
|
||||
}
|
||||
if want == http.StatusOK && (resp.Header.Get("Content-Type") != "image/png" || !bytes.HasPrefix(body, []byte("\x89PNG"))) {
|
||||
t.Fatalf("get %s: %q %q", path, resp.Header.Get("Content-Type"), body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func writeFile(t *testing.T, path, content string) {
|
||||
t.Helper()
|
||||
err := os.WriteFile(path, []byte(content), 0o644)
|
||||
|
||||
Reference in New Issue
Block a user