2026-10-15 07:27:17 -07:00
|
|
|
package server
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/fs"
|
|
|
|
|
"net/http"
|
|
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
|
2026-10-18 15:32:07 -07:00
|
|
|
"g.fc.run/ai/byre/internal/api"
|
2026-10-15 07:27:17 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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) {
|
2026-10-20 23:31:16 -07:00
|
|
|
t, ok := s.activeTask(w, r, taskID)
|
|
|
|
|
if !ok {
|
2026-10-15 07:27:17 -07:00
|
|
|
return
|
|
|
|
|
}
|
2026-10-22 19:17:08 -07:00
|
|
|
serveImage(w, r, t.WorktreePath, params.Path)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Server) GetProjectImage(w http.ResponseWriter, r *http.Request, projectID string, params api.GetProjectImageParams) {
|
|
|
|
|
p, err := s.store.GetProject(r.Context(), projectID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeStoreError(w, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
serveImage(w, r, p.RepoPath, params.Path)
|
|
|
|
|
}
|
2026-10-15 07:27:17 -07:00
|
|
|
|
2026-10-22 19:17:08 -07:00
|
|
|
func serveImage(w http.ResponseWriter, r *http.Request, root, rel string) {
|
|
|
|
|
path := rel
|
2026-10-15 07:27:17 -07:00
|
|
|
if !filepath.IsAbs(path) {
|
2026-10-22 19:17:08 -07:00
|
|
|
path = filepath.Join(root, path)
|
2026-10-15 07:27:17 -07:00
|
|
|
}
|
|
|
|
|
real, err := filepath.EvalSymlinks(path)
|
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
2026-10-22 19:17:08 -07:00
|
|
|
writeError(w, http.StatusNotFound, fmt.Errorf("%s does not exist", rel))
|
2026-10-15 07:27:17 -07:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
writeError(w, http.StatusBadRequest, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
contentType := worktreeImageTypes[strings.ToLower(filepath.Ext(real))]
|
|
|
|
|
if contentType == "" {
|
2026-10-22 19:17:08 -07:00
|
|
|
writeError(w, http.StatusBadRequest, fmt.Errorf("%s is not a PNG, JPEG, GIF, WebP or SVG file", rel))
|
2026-10-15 07:27:17 -07:00
|
|
|
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() {
|
2026-10-22 19:17:08 -07:00
|
|
|
writeError(w, http.StatusBadRequest, fmt.Errorf("%s is not a file", rel))
|
2026-10-15 07:27:17 -07:00
|
|
|
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)
|
|
|
|
|
}
|