this repo has no description
1package knotserver
2
3import (
4 "fmt"
5 "io/fs"
6 "log"
7 "net/http"
8 "os"
9 "path/filepath"
10 "strings"
11
12 "github.com/go-chi/chi/v5"
13 "github.com/icyphox/bild/auth"
14 "github.com/icyphox/bild/git"
15 "github.com/microcosm-cc/bluemonday"
16)
17
18func sanitize(content []byte) []byte {
19 return bluemonday.UGCPolicy().SanitizeBytes([]byte(content))
20}
21
22func displayRepoName(r *http.Request) string {
23 user := r.Context().Value("did").(string)
24 name := chi.URLParam(r, "name")
25
26 handle, err := auth.ResolveIdent(r.Context(), user)
27 if err != nil {
28 log.Printf("failed to resolve ident: %s: %s", user, err)
29 return fmt.Sprintf("%s/%s", user, name)
30 }
31
32 return fmt.Sprintf("@%s/%s", handle.Handle.String(), name)
33}
34
35func didPath(r *http.Request, did string) string {
36 path := filepath.Join(did, chi.URLParam(r, "name"))
37 filepath.Clean(path)
38 return path
39}
40
41func getDescription(path string) (desc string) {
42 db, err := os.ReadFile(filepath.Join(path, "description"))
43 if err == nil {
44 desc = string(db)
45 } else {
46 desc = ""
47 }
48 return
49}
50
51func (h *Handle) isUnlisted(name string) bool {
52 for _, i := range h.c.Repo.Unlisted {
53 if name == i {
54 return true
55 }
56 }
57
58 return false
59}
60
61func (h *Handle) isIgnored(name string) bool {
62 for _, i := range h.c.Repo.Ignore {
63 if name == i {
64 return true
65 }
66 }
67
68 return false
69}
70
71type repoInfo struct {
72 Git *git.GitRepo
73 Path string
74 Category string
75}
76
77func (d *Handle) getAllRepos() ([]repoInfo, error) {
78 repos := []repoInfo{}
79 max := strings.Count(d.c.Repo.ScanPath, string(os.PathSeparator)) + 2
80
81 err := filepath.WalkDir(d.c.Repo.ScanPath, func(path string, de fs.DirEntry, err error) error {
82 if err != nil {
83 return err
84 }
85
86 if de.IsDir() {
87 // Check if we've exceeded our recursion depth
88 if strings.Count(path, string(os.PathSeparator)) > max {
89 return fs.SkipDir
90 }
91
92 if d.isIgnored(path) {
93 return fs.SkipDir
94 }
95
96 // A bare repo should always have at least a HEAD file, if it
97 // doesn't we can continue recursing
98 if _, err := os.Lstat(filepath.Join(path, "HEAD")); err == nil {
99 repo, err := git.Open(path, "")
100 if err != nil {
101 log.Println(err)
102 } else {
103 relpath, _ := filepath.Rel(d.c.Repo.ScanPath, path)
104 repos = append(repos, repoInfo{
105 Git: repo,
106 Path: relpath,
107 Category: d.category(path),
108 })
109 // Since we found a Git repo, we don't want to recurse
110 // further
111 return fs.SkipDir
112 }
113 }
114 }
115 return nil
116 })
117
118 return repos, err
119}
120
121func (d *Handle) category(path string) string {
122 return strings.TrimPrefix(filepath.Dir(strings.TrimPrefix(path, d.c.Repo.ScanPath)), string(os.PathSeparator))
123}
124
125func setContentDisposition(w http.ResponseWriter, name string) {
126 h := "inline; filename=\"" + name + "\""
127 w.Header().Add("Content-Disposition", h)
128}
129
130func setGZipMIME(w http.ResponseWriter) {
131 setMIME(w, "application/gzip")
132}
133
134func setMIME(w http.ResponseWriter, mime string) {
135 w.Header().Add("Content-Type", mime)
136}