A vibe coded tangled fork which supports pijul.
1package xrpc
2
3import (
4 "compress/gzip"
5 "fmt"
6 "net/http"
7 "net/url"
8 "strings"
9
10 "github.com/bluesky-social/indigo/atproto/atclient"
11 "github.com/bluesky-social/indigo/atproto/syntax"
12 "github.com/go-git/go-git/v5/plumbing"
13 "tangled.org/core/api/tangled"
14 "tangled.org/core/knotmirror/db"
15 "tangled.org/core/knotserver/git"
16)
17
18func (x *Xrpc) GetArchive(w http.ResponseWriter, r *http.Request) {
19 var (
20 repoQuery = r.URL.Query().Get("repo")
21 ref = r.URL.Query().Get("ref")
22 format = r.URL.Query().Get("format")
23 prefix = r.URL.Query().Get("prefix")
24 )
25
26 repo, err := syntax.ParseATURI(repoQuery)
27 if err != nil || repo.RecordKey() == "" {
28 writeJson(w, http.StatusBadRequest, atclient.ErrorBody{Name: "BadRequest", Message: fmt.Sprintf("repo parameter invalid: %s", repoQuery)})
29 return
30 }
31
32 if format != "tar.gz" {
33 writeJson(w, http.StatusBadRequest, atclient.ErrorBody{Name: "BadRequest", Message: "only tar.gz format is supported"})
34 return
35 }
36 if format == "" {
37 format = "tar.gz"
38 }
39
40 l := x.logger.With("repo", repo, "ref", ref, "format", format, "prefix", prefix)
41 ctx := r.Context()
42
43 repoPath, err := x.makeRepoPath(ctx, repo)
44 if err != nil {
45 l.Error("failed to resolve repo at-uri", "err", err)
46 writeJson(w, http.StatusInternalServerError, atclient.ErrorBody{Name: "InternalServerError", Message: "failed to resolve repo"})
47 return
48 }
49
50 gr, err := git.Open(repoPath, ref)
51 if err != nil {
52 l.Error("failed to open git repo", "err", err)
53 writeJson(w, http.StatusInternalServerError, atclient.ErrorBody{Name: "InternalServerError", Message: "failed to open git repo"})
54 return
55 }
56
57 repoName, err := func() (string, error) {
58 r, err := db.GetRepoByAtUri(ctx, x.db, repo)
59 if err != nil {
60 return "", err
61 }
62 if r == nil {
63 return "", fmt.Errorf("repo not found: %s", repo)
64 }
65 return r.Name, nil
66 }()
67 if err != nil {
68 l.Error("failed to get repo name", "err", err)
69 writeJson(w, http.StatusInternalServerError, atclient.ErrorBody{Name: "InternalServerError", Message: "failed to retrieve repo name"})
70 return
71 }
72
73 safeRefFilename := strings.ReplaceAll(plumbing.ReferenceName(ref).Short(), "/", "-")
74 immutableLink := func() string {
75 params := url.Values{}
76 params.Set("repo", repo.String())
77 params.Set("ref", gr.Hash().String())
78 params.Set("format", format)
79 params.Set("prefix", prefix)
80 return fmt.Sprintf("%s/xrpc/%s?%s", x.cfg.BaseUrl(), tangled.GitTempGetArchiveNSID, params.Encode())
81 }()
82
83 filename := fmt.Sprintf("%s-%s.tar.gz", repoName, safeRefFilename)
84 w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename))
85 w.Header().Set("Content-Type", "application/gzip")
86 w.Header().Set("Link", fmt.Sprintf("<%s>; rel=\"immutable\"", immutableLink))
87
88 gw := gzip.NewWriter(w)
89 defer gw.Close()
90
91 if err := gr.WriteTar(gw, prefix); err != nil {
92 // once we start writing to the body we can't report error anymore
93 // so we are only left with logging the error
94 l.Error("writing tar file", "err", err.Error())
95 w.WriteHeader(http.StatusInternalServerError)
96 return
97 }
98
99 if err := gw.Flush(); err != nil {
100 // once we start writing to the body we can't report error anymore
101 // so we are only left with logging the error
102 l.Error("flushing", "err", err.Error())
103 w.WriteHeader(http.StatusInternalServerError)
104 return
105 }
106}