v0.15.0 9006957

scheme.go RawHistoryFolder

1package api
2
3import (
4	"embed"
5	"fmt"
6	"mime"
7	neturl "net/url"
8	"os"
9	"path/filepath"
10	"strings"
11
12	"github.com/bnema/dumber/internal/app/constants"
13	"github.com/bnema/dumber/internal/config"
14	"github.com/bnema/dumber/internal/logging"
15	"github.com/bnema/dumber/internal/services"
16	webkit "github.com/diamondburned/gotk4-webkitgtk/pkg/webkit/v6"
17	"github.com/diamondburned/gotk4/pkg/gio/v2"
18	"github.com/diamondburned/gotk4/pkg/glib/v2"
19)
20
21// SchemeHandler handles custom dumb:// scheme resolution
22type SchemeHandler struct {
23	assets         embed.FS
24	parserService  *services.ParserService
25	browserService *services.BrowserService
26	cfg            *config.Config
27}
28
29// NewSchemeHandler creates a new scheme handler
30func NewSchemeHandler(
31	assets embed.FS,
32	parserService *services.ParserService,
33	browserService *services.BrowserService,
34) *SchemeHandler {
35	return &SchemeHandler{
36		assets:         assets,
37		parserService:  parserService,
38		browserService: browserService,
39	}
40}
41
42// SetConfig sets the configuration for the scheme handler
43func (s *SchemeHandler) SetConfig(cfg *config.Config) {
44	s.cfg = cfg
45}
46
47// Handle processes dumb:// scheme requests using the new URISchemeRequest API
48func (s *SchemeHandler) Handle(req *webkit.URISchemeRequest) {
49	uri := req.URI()
50	logging.Debug(fmt.Sprintf("[scheme] request: %s", uri))
51
52	// Known forms:
53	// - dumb://homepage or dumb:homepage → index.html
54	// - dumb://app/index.html, dumb://app/<path> → serve from gui/<path>
55	// - dumb://favicon/<hash>.png → serve cached favicon
56	// - dumb://<anything> without path → index.html
57	u, err := neturl.Parse(uri)
58	if err != nil || u.Scheme != "dumb" {
59		req.FinishError(fmt.Errorf("invalid URI: %s", uri))
60		return
61	}
62
63	// Check if this is a favicon request
64	if u.Host == "favicon" || (u.Host == "" && strings.HasPrefix(u.Path, "/favicon/")) {
65		s.handleFavicon(req, u)
66		return
67	}
68
69	// Static assets
70	s.handleAsset(req, u)
71}
72
73// handleAsset serves static assets from embedded filesystem
74func (s *SchemeHandler) handleAsset(req *webkit.URISchemeRequest, u *neturl.URL) {
75	// Resolve target path inside embed FS
76	var rel string
77	if u.Opaque == constants.HomepagePath || (u.Host == constants.HomepagePath && (u.Path == "" || u.Path == "/")) || (u.Host == "" && (u.Path == "" || u.Path == "/")) {
78		rel = constants.IndexHTML
79	} else {
80		host := u.Host
81		p := strings.TrimPrefix(u.Path, "/")
82		switch {
83		case host == "app" && p == "":
84			rel = constants.IndexHTML
85		case host == "app":
86			rel = p
87		case host == constants.HomepagePath && p != "":
88			// dumb://homepage/<asset>
89			rel = p
90		case p != "":
91			rel = p
92		default:
93			rel = constants.IndexHTML
94		}
95	}
96
97	// Special-case homepage favicon: map .ico request to embedded PNG file
98	if (u.Host == constants.HomepagePath || u.Opaque == constants.HomepagePath) && strings.EqualFold(rel, "favicon.ico") {
99		logging.Debug(fmt.Sprintf("[scheme] asset: rel=%s (host=%s path=%s) → mapping to favicon.png", rel, u.Host, u.Path))
100		data, rerr := s.assets.ReadFile(filepath.ToSlash(filepath.Join("assets", "gui", "favicon.png")))
101		if rerr == nil {
102			s.finishRequest(req, "image/png", data, "favicon.png")
103			return
104		}
105	}
106
107	logging.Debug(fmt.Sprintf("[scheme] asset: rel=%s (host=%s path=%s)", rel, u.Host, u.Path))
108
109	// Try to read the requested asset
110	data, rerr := s.assets.ReadFile(filepath.ToSlash(filepath.Join("assets", "gui", rel)))
111	if rerr != nil {
112		logging.Debug(fmt.Sprintf("[scheme] not found: %s", rel))
113		req.FinishError(fmt.Errorf("asset not found: %s", rel))
114		return
115	}
116
117	// Determine mime type
118	mt := s.getMimeType(rel)
119
120	// Finish the request with the data
121	logging.Debug(fmt.Sprintf("[scheme] serving %s with mime-type: %s", rel, mt))
122	s.finishRequest(req, mt, data, rel)
123}
124
125// finishRequest completes a URI scheme request with the provided data
126func (s *SchemeHandler) finishRequest(req *webkit.URISchemeRequest, mimeType string, data []byte, filename string) {
127	// Convert byte slice to GLib Bytes
128	gbytes := glib.NewBytes(data)
129
130	// Create an input stream from the bytes
131	stream := gio.NewMemoryInputStreamFromBytes(gbytes)
132	if stream == nil {
133		req.FinishError(fmt.Errorf("failed to create input stream for: %s", filename))
134		logging.Error(fmt.Sprintf("[scheme] failed to create stream for: %s", filename))
135		return
136	}
137
138	// Finish the request with the stream
139	contentLength := int64(len(data))
140	req.Finish(stream, contentLength, mimeType)
141}
142
143// getMimeType determines the MIME type for a given file path
144func (s *SchemeHandler) getMimeType(filename string) string {
145	ext := strings.ToLower(filepath.Ext(filename))
146
147	// Try standard mime type first
148	mt := mime.TypeByExtension(ext)
149	if mt != "" {
150		return mt
151	}
152
153	// Fallbacks for common web assets
154	switch ext {
155	case ".js":
156		return "application/javascript"
157	case ".mjs":
158		return "application/javascript"
159	case ".css":
160		return "text/css"
161	case ".svg":
162		return "image/svg+xml"
163	case ".ico":
164		return "image/x-icon"
165	case ".woff":
166		return "font/woff"
167	case ".woff2":
168		return "font/woff2"
169	case ".ttf":
170		return "font/ttf"
171	case ".otf":
172		return "font/otf"
173	case ".eot":
174		return "application/vnd.ms-fontobject"
175	case ".png":
176		return "image/png"
177	case ".jpg", ".jpeg":
178		return "image/jpeg"
179	case ".gif":
180		return "image/gif"
181	case ".webp":
182		return "image/webp"
183	case ".json":
184		return "application/json"
185	case ".xml":
186		return "application/xml"
187	case ".html", ".htm":
188		return "text/html; charset=utf-8"
189	default:
190		// Default to text/plain for unknown extensions
191		return "text/plain"
192	}
193}
194
195// handleFavicon serves cached favicon files
196func (s *SchemeHandler) handleFavicon(req *webkit.URISchemeRequest, u *neturl.URL) {
197	// Extract the filename from the URL
198	// URL format: dumb://favicon/<hash>.png
199	var filename string
200	if u.Host == "favicon" {
201		filename = strings.TrimPrefix(u.Path, "/")
202	} else {
203		filename = strings.TrimPrefix(u.Path, "/favicon/")
204	}
205
206	if filename == "" {
207		logging.Debug(fmt.Sprintf("[scheme] favicon: empty filename"))
208		req.FinishError(fmt.Errorf("invalid favicon path"))
209		return
210	}
211
212	// Get the favicon cache directory path
213	dataDir, err := config.GetDataDir()
214	if err != nil {
215		logging.Error(fmt.Sprintf("[scheme] favicon: failed to get data directory: %v", err))
216		req.FinishError(fmt.Errorf("failed to get data directory"))
217		return
218	}
219
220	faviconPath := filepath.Join(dataDir, "favicons", filename)
221
222	// Read the favicon file
223	data, err := os.ReadFile(faviconPath)
224	if err != nil {
225		logging.Debug(fmt.Sprintf("[scheme] favicon: file not found: %s", faviconPath))
226		req.FinishError(fmt.Errorf("favicon not found: %s", filename))
227		return
228	}
229
230	logging.Debug(fmt.Sprintf("[scheme] favicon: serving %s (%d bytes)", filename, len(data)))
231	s.finishRequest(req, "image/png", data, filename)
232}

Keyboard shortcuts

Single-key shortcuts are on. ctrl k and esc work whatever this says.

ctrl k :
go to a section, branch, tag, file or repository
?
this list
j k
move the row cursor down / up
enter
open the row under the cursor; on Files, a directory opens or closes in place
↑ ↓ ← → home end
in the file tree (focus on a row): previous / next row, close / open a directory or go to its parent / first entry, first / last row. They never open a file
l h
on Files: open a directory or a file / close a directory or go up to its parent, then to the parent page. Elsewhere: into the side panel (on a merge request, its files) / back to the content; there j k move between its controls
g g G
first / last row
/
filter the list; on Files, find a file anywhere in the repository (esc clears, enter opens the best match)
b
switch branch or tag
g s g f g c g b g m g i g r g a
tabs: summary, files, commits, branches & tags, merge requests, issues, releases, actions (when available)
g h
home
w
wide mode: use the whole width of the screen, or not (remembered)
y
copy a permalink pinned to the commit
d u x
diff: split / unified / collapse or expand all
e E
diff: the whole file in view / every file, with the lines the hunks hide
n
merge request: next note in the diff
esc
close a dialog, leave a field

Branches 8

Tags 42

↑↓ moveenter openesc close