Monorepo for Tangled
1package repo
2
3import (
4 "encoding/base64"
5 "fmt"
6 "io"
7 "net/http"
8 "net/url"
9 "path/filepath"
10 "slices"
11 "strings"
12 "time"
13
14 "tangled.org/core/api/tangled"
15 "tangled.org/core/appview/config"
16 "tangled.org/core/appview/db"
17 "tangled.org/core/appview/models"
18 "tangled.org/core/appview/pages"
19 "tangled.org/core/appview/pages/markup"
20 "tangled.org/core/appview/reporesolver"
21 xrpcclient "tangled.org/core/appview/xrpcclient"
22 "tangled.org/core/types"
23
24 indigoxrpc "github.com/bluesky-social/indigo/xrpc"
25 "github.com/go-chi/chi/v5"
26 "github.com/go-git/go-git/v5/plumbing"
27)
28
29// the content can be one of the following:
30//
31// - code : text | | raw
32// - markup : text | rendered | raw
33// - svg : text | rendered | raw
34// - png : | rendered | raw
35// - video : | rendered | raw
36// - submodule : | rendered |
37// - rest : | |
38func (rp *Repo) Blob(w http.ResponseWriter, r *http.Request) {
39 l := rp.logger.With("handler", "RepoBlob")
40
41 f, err := rp.repoResolver.Resolve(r)
42 if err != nil {
43 l.Error("failed to get repo and knot", "err", err)
44 return
45 }
46
47 ref := chi.URLParam(r, "ref")
48 ref, _ = url.PathUnescape(ref)
49
50 filePath := chi.URLParam(r, "*")
51 filePath, _ = url.PathUnescape(filePath)
52
53 scheme := "http"
54 if !rp.config.Core.Dev {
55 scheme = "https"
56 }
57 host := fmt.Sprintf("%s://%s", scheme, f.Knot)
58 xrpcc := &indigoxrpc.Client{
59 Host: host,
60 }
61 repo := fmt.Sprintf("%s/%s", f.Did, f.Name)
62 resp, err := tangled.RepoBlob(r.Context(), xrpcc, filePath, false, ref, repo)
63 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil {
64 l.Error("failed to call XRPC repo.blob", "err", xrpcerr)
65 rp.pages.Error503(w)
66 return
67 }
68
69 ownerSlashRepo := reporesolver.GetBaseRepoPath(r, f)
70
71 // Use XRPC response directly instead of converting to internal types
72 var breadcrumbs [][]string
73 breadcrumbs = append(breadcrumbs, []string{f.Name, fmt.Sprintf("/%s/tree/%s", ownerSlashRepo, url.PathEscape(ref))})
74 if filePath != "" {
75 for idx, elem := range strings.Split(filePath, "/") {
76 breadcrumbs = append(breadcrumbs, []string{elem, fmt.Sprintf("%s/%s", breadcrumbs[idx][1], url.PathEscape(elem))})
77 }
78 }
79
80 // Create the blob view
81 blobView := NewBlobView(resp, rp.config, f, ref, filePath, r.URL.Query())
82
83 user := rp.oauth.GetMultiAccountUser(r)
84
85 // Get email to DID mapping for commit author
86 var emails []string
87 if resp.LastCommit != nil && resp.LastCommit.Author != nil {
88 emails = append(emails, resp.LastCommit.Author.Email)
89 }
90 emailToDidMap, err := db.GetEmailToDid(rp.db, emails, true)
91 if err != nil {
92 l.Error("failed to get email to did mapping", "err", err)
93 emailToDidMap = make(map[string]string)
94 }
95
96 var lastCommitInfo *types.LastCommitInfo
97 if resp.LastCommit != nil {
98 when, _ := time.Parse(time.RFC3339, resp.LastCommit.When)
99 lastCommitInfo = &types.LastCommitInfo{
100 Hash: plumbing.NewHash(resp.LastCommit.Hash),
101 Message: resp.LastCommit.Message,
102 When: when,
103 }
104 if resp.LastCommit.Author != nil {
105 lastCommitInfo.Author.Name = resp.LastCommit.Author.Name
106 lastCommitInfo.Author.Email = resp.LastCommit.Author.Email
107 lastCommitInfo.Author.When, _ = time.Parse(time.RFC3339, resp.LastCommit.Author.When)
108 }
109 }
110
111 rp.pages.RepoBlob(w, pages.RepoBlobParams{
112 LoggedInUser: user,
113 RepoInfo: rp.repoResolver.GetRepoInfo(r, user),
114 BreadCrumbs: breadcrumbs,
115 BlobView: blobView,
116 EmailToDid: emailToDidMap,
117 LastCommitInfo: lastCommitInfo,
118 RepoBlob_Output: resp,
119 })
120}
121
122func (rp *Repo) RepoBlobRaw(w http.ResponseWriter, r *http.Request) {
123 l := rp.logger.With("handler", "RepoBlobRaw")
124
125 f, err := rp.repoResolver.Resolve(r)
126 if err != nil {
127 l.Error("failed to get repo and knot", "err", err)
128 w.WriteHeader(http.StatusBadRequest)
129 return
130 }
131
132 ref := chi.URLParam(r, "ref")
133 ref, _ = url.PathUnescape(ref)
134
135 filePath := chi.URLParam(r, "*")
136 filePath, _ = url.PathUnescape(filePath)
137
138 scheme := "http"
139 if !rp.config.Core.Dev {
140 scheme = "https"
141 }
142 repo := f.DidSlashRepo()
143 baseURL := &url.URL{
144 Scheme: scheme,
145 Host: f.Knot,
146 Path: "/xrpc/sh.tangled.repo.blob",
147 }
148 query := baseURL.Query()
149 query.Set("repo", repo)
150 query.Set("ref", ref)
151 query.Set("path", filePath)
152 query.Set("raw", "true")
153 baseURL.RawQuery = query.Encode()
154 blobURL := baseURL.String()
155 req, err := http.NewRequest("GET", blobURL, nil)
156 if err != nil {
157 l.Error("failed to create request", "err", err)
158 return
159 }
160
161 // forward the If-None-Match header
162 if clientETag := r.Header.Get("If-None-Match"); clientETag != "" {
163 req.Header.Set("If-None-Match", clientETag)
164 }
165 client := &http.Client{}
166
167 resp, err := client.Do(req)
168 if err != nil {
169 l.Error("failed to reach knotserver", "err", err)
170 rp.pages.Error503(w)
171 return
172 }
173
174 defer resp.Body.Close()
175
176 // forward 304 not modified
177 if resp.StatusCode == http.StatusNotModified {
178 w.WriteHeader(http.StatusNotModified)
179 return
180 }
181
182 if resp.StatusCode != http.StatusOK {
183 l.Error("knotserver returned non-OK status for raw blob", "url", blobURL, "statuscode", resp.StatusCode)
184 w.WriteHeader(resp.StatusCode)
185 _, _ = io.Copy(w, resp.Body)
186 return
187 }
188
189 contentType := resp.Header.Get("Content-Type")
190 body, err := io.ReadAll(resp.Body)
191 if err != nil {
192 l.Error("error reading response body from knotserver", "err", err)
193 w.WriteHeader(http.StatusInternalServerError)
194 return
195 }
196
197 if strings.HasPrefix(contentType, "text/") || isTextualMimeType(contentType) {
198 // serve all textual content as text/plain
199 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
200 w.Write(body)
201 } else if strings.HasPrefix(contentType, "image/") || strings.HasPrefix(contentType, "video/") {
202 // serve images and videos with their original content type
203 w.Header().Set("Content-Type", contentType)
204 w.Write(body)
205 } else {
206 w.WriteHeader(http.StatusUnsupportedMediaType)
207 w.Write([]byte("unsupported content type"))
208 return
209 }
210}
211
212// NewBlobView creates a BlobView from the XRPC response
213func NewBlobView(resp *tangled.RepoBlob_Output, config *config.Config, repo *models.Repo, ref, filePath string, queryParams url.Values) models.BlobView {
214 view := models.BlobView{
215 Contents: "",
216 Lines: 0,
217 }
218
219 // Set size
220 if resp.Size != nil {
221 view.SizeHint = uint64(*resp.Size)
222 } else if resp.Content != nil {
223 view.SizeHint = uint64(len(*resp.Content))
224 }
225
226 if resp.Submodule != nil {
227 view.ContentType = models.BlobContentTypeSubmodule
228 view.HasRenderedView = true
229 view.ContentSrc = resp.Submodule.Url
230 return view
231 }
232
233 // Determine if binary
234 if resp.IsBinary != nil && *resp.IsBinary {
235 view.ContentSrc = generateBlobURL(config, repo, ref, filePath)
236 ext := strings.ToLower(filepath.Ext(resp.Path))
237
238 switch ext {
239 case ".jpg", ".jpeg", ".png", ".gif", ".webp":
240 view.ContentType = models.BlobContentTypeImage
241 view.HasRawView = true
242 view.HasRenderedView = true
243 view.ShowingRendered = true
244
245 case ".svg":
246 view.ContentType = models.BlobContentTypeSvg
247 view.HasRawView = true
248 view.HasTextView = true
249 view.HasRenderedView = true
250 view.ShowingRendered = queryParams.Get("code") != "true"
251 if resp.Content != nil {
252 bytes, _ := base64.StdEncoding.DecodeString(*resp.Content)
253 view.Contents = string(bytes)
254 view.Lines = countLines(view.Contents)
255 }
256
257 case ".mp4", ".webm", ".ogg", ".mov", ".avi":
258 view.ContentType = models.BlobContentTypeVideo
259 view.HasRawView = true
260 view.HasRenderedView = true
261 view.ShowingRendered = true
262 }
263
264 return view
265 }
266
267 // otherwise, we are dealing with text content
268 view.HasRawView = true
269 view.HasTextView = true
270
271 if resp.Content != nil {
272 view.Contents = *resp.Content
273 view.Lines = countLines(view.Contents)
274 }
275
276 // with text, we may be dealing with markdown
277 format := markup.GetFormat(resp.Path)
278 if format == markup.FormatMarkdown {
279 view.ContentType = models.BlobContentTypeMarkup
280 view.HasRenderedView = true
281 view.ShowingRendered = queryParams.Get("code") != "true"
282 }
283
284 return view
285}
286
287func generateBlobURL(config *config.Config, repo *models.Repo, ref, filePath string) string {
288 scheme := "http"
289 if !config.Core.Dev {
290 scheme = "https"
291 }
292
293 repoName := fmt.Sprintf("%s/%s", repo.Did, repo.Name)
294 baseURL := &url.URL{
295 Scheme: scheme,
296 Host: repo.Knot,
297 Path: "/xrpc/sh.tangled.repo.blob",
298 }
299 query := baseURL.Query()
300 query.Set("repo", repoName)
301 query.Set("ref", ref)
302 query.Set("path", filePath)
303 query.Set("raw", "true")
304 baseURL.RawQuery = query.Encode()
305 blobURL := baseURL.String()
306
307 if !config.Core.Dev {
308 return markup.GenerateCamoURL(config.Camo.Host, config.Camo.SharedSecret, blobURL)
309 }
310 return blobURL
311}
312
313func isTextualMimeType(mimeType string) bool {
314 textualTypes := []string{
315 "application/json",
316 "application/xml",
317 "application/yaml",
318 "application/x-yaml",
319 "application/toml",
320 "application/javascript",
321 "application/ecmascript",
322 "message/",
323 }
324 return slices.Contains(textualTypes, mimeType)
325}
326
327// TODO: dedup with strings
328func countLines(content string) int {
329 if content == "" {
330 return 0
331 }
332
333 count := strings.Count(content, "\n")
334
335 if !strings.HasSuffix(content, "\n") {
336 count++
337 }
338
339 return count
340}