this repo has no description
at master 1.6 kB view raw
1package repo 2 3import ( 4 "fmt" 5 "net/http" 6 "net/url" 7 "strings" 8 9 "tangled.org/core/api/tangled" 10 xrpcclient "tangled.org/core/appview/xrpcclient" 11 12 indigoxrpc "github.com/bluesky-social/indigo/xrpc" 13 "github.com/go-chi/chi/v5" 14 "github.com/go-git/go-git/v5/plumbing" 15) 16 17func (rp *Repo) DownloadArchive(w http.ResponseWriter, r *http.Request) { 18 l := rp.logger.With("handler", "DownloadArchive") 19 ref := chi.URLParam(r, "ref") 20 ref, _ = url.PathUnescape(ref) 21 ref = strings.TrimSuffix(ref, ".tar.gz") 22 f, err := rp.repoResolver.Resolve(r) 23 if err != nil { 24 l.Error("failed to get repo and knot", "err", err) 25 return 26 } 27 scheme := "http" 28 if !rp.config.Core.Dev { 29 scheme = "https" 30 } 31 host := fmt.Sprintf("%s://%s", scheme, f.Knot) 32 xrpcc := &indigoxrpc.Client{ 33 Host: host, 34 } 35 didSlashRepo := f.DidSlashRepo() 36 archiveBytes, err := tangled.RepoArchive(r.Context(), xrpcc, "tar.gz", "", ref, didSlashRepo) 37 if xrpcerr := xrpcclient.HandleXrpcErr(err); xrpcerr != nil { 38 l.Error("failed to call XRPC repo.archive", "err", xrpcerr) 39 rp.pages.Error503(w) 40 return 41 } 42 // Set headers for file download, just pass along whatever the knot specifies 43 safeRefFilename := strings.ReplaceAll(plumbing.ReferenceName(ref).Short(), "/", "-") 44 filename := fmt.Sprintf("%s-%s.tar.gz", f.Name, safeRefFilename) 45 w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s\"", filename)) 46 w.Header().Set("Content-Type", "application/gzip") 47 w.Header().Set("Content-Length", fmt.Sprintf("%d", len(archiveBytes))) 48 // Write the archive data directly 49 w.Write(archiveBytes) 50}