💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › eb8e6b9b43a28edd64687509b8… captured on 2024-02-05 at 09:47:57. Gemini links have been rewritten to link to archived content
-=-=-=-=-=-=-
0 package gmi
1
2 import (
3 "strconv"
4 "strings"
5
6 "gemigit/db"
7 "gemigit/repo"
8 "gemigit/csrf"
9 io "gemigit/util"
10
11 "github.com/pitr/gig"
12 "github.com/gabriel-vasile/mimetype"
13 )
14
15 func accountRedirect(c gig.Context, after string) error {
16 return c.NoContent(gig.StatusRedirectTemporary,
17 "/account/" + csrf.Token(c.CertHash()) + "/" + after)
18 }
19
20 func showFileContent(content string) string {
21 lines := strings.Split(content, "\n")
22 file := ""
23 for i, line := range lines {
24 file += strconv.Itoa(i) + "\t" + line + "\n"
25 }
26 return strings.Replace(file, "%", "%%", -1)
27 }
28
29 func serveFile(name string, user string, file string) ([]byte, string, error) {
30 repofile, err := repo.GetFile(name, user, file)
31 if err != nil {
32 return nil, "", err
33 }
34 reader, err := repofile.Reader()
35 if err != nil {
36 return nil, "", err
37 }
38 buf, err := io.ReadAll(reader)
39 if err != nil {
40 return nil, "", err
41 }
42 mtype := mimetype.Detect(buf)
43 return buf, mtype.String(), nil
44 }
45
46 // Private
47
48 func RepoFiles(c gig.Context) error {
49 user, exist := db.GetUser(c.CertHash())
50 if !exist {
51 return c.NoContent(gig.StatusBadRequest, "Invalid username")
52 }
53 query, err := c.QueryString()
54 if err != nil {
55 return c.NoContent(gig.StatusBadRequest, err.Error())
56 }
57 if query == "" {
58 return showRepo(c, pageFiles, true)
59 }
60 repofile, err := repo.GetFile(c.Param("repo"), user.Name, query)
61 if err != nil {
62 return c.NoContent(gig.StatusBadRequest, err.Error())
63 }
64 contents, err := repofile.Contents()
65 if err != nil {
66 return c.NoContent(gig.StatusBadRequest, err.Error())
67 }
68 return c.Gemini(contents)
69 }
70
71 func RepoFileContent(c gig.Context) error {
72 user, exist := db.GetUser(c.CertHash())
73 if !exist {
74 return c.NoContent(gig.StatusBadRequest, "Invalid username")
75 }
76 content, err := repo.GetPrivateFile(c.Param("repo"), user.Name,
77 c.Param("blob"), c.CertHash())
78 if err != nil {
79 return c.NoContent(gig.StatusBadRequest, err.Error())
80 }
81 header := "=>/account/repo/" + c.Param("repo") + "/files Go Back\n\n"
82 return c.Gemini(header + showFileContent(content))
83 }
84
85 func RepoFile(c gig.Context) error {
86 user, exist := db.GetUser(c.CertHash())
87 if !exist {
88 return c.NoContent(gig.StatusBadRequest, "Invalid username")
89 }
90 data, mtype, err := serveFile(c.Param("repo"), user.Name, c.Param("*"))
91 if err != nil {
92 return c.NoContent(gig.StatusBadRequest, err.Error())
93 }
94 return c.Blob(mtype, data)
95 }
96
97 func TogglePublic(c gig.Context) error {
98 user, exist := db.GetUser(c.CertHash())
99 if !exist {
100 return c.NoContent(gig.StatusBadRequest, "Invalid username")
101 }
102 if err := user.TogglePublic(c.Param("repo"), c.CertHash());
103 err != nil {
104 return c.NoContent(gig.StatusBadRequest, err.Error())
105 }
106 return accountRedirect(c, "repo/" + c.Param("repo"))
107 }
108
109 func ChangeRepoName(c gig.Context) error {
110 newname, err := c.QueryString()
111 if err != nil {
112 return c.NoContent(gig.StatusBadRequest,
113 "Invalid input received")
114 }
115 if newname == "" {
116 return c.NoContent(gig.StatusInput, "New repository name")
117 }
118 user, exist := db.GetUser(c.CertHash())
119 if !exist {
120 return c.NoContent(gig.StatusBadRequest, "Invalid username")
121 }
122 // should check if repo exist and if the new name is free
123 if err := repo.ChangeRepoDir(c.Param("repo"), user.Name, newname);
124 err != nil {
125 return c.NoContent(gig.StatusBadRequest, err.Error())
126 }
127 if err := user.ChangeRepoName(c.Param("repo"), newname, c.CertHash());
128
129 err != nil {
130 return c.NoContent(gig.StatusBadRequest, err.Error())
131 }
132 return accountRedirect(c, "repo/" + newname)
133 }
134
135 func ChangeRepoDesc(c gig.Context) error {
136 newdesc, err := c.QueryString()
137 if err != nil {
138 return c.NoContent(gig.StatusBadRequest,
139 "Invalid input received")
140 }
141 if newdesc == "" {
142 return c.NoContent(gig.StatusInput,
143 "New repository description")
144 }
145 user, exist := db.GetUser(c.CertHash())
146 if !exist {
147 return c.NoContent(gig.StatusBadRequest, "Invalid username")
148 }
149 if err := user.ChangeRepoDesc(c.Param("repo"), newdesc);
150 err != nil {
151 return c.NoContent(gig.StatusBadRequest, err.Error())
152 }
153 return accountRedirect(c, "repo/" + c.Param("repo"))
154 }
155
156 func DeleteRepo(c gig.Context) error {
157 name, err := c.QueryString()
158 if err != nil {
159 return c.NoContent(gig.StatusBadRequest,
160 "Invalid input received")
161 }
162 if name == "" {
163 return c.NoContent(gig.StatusInput,
164 "To confirm type the repository name")
165 }
166 if name != c.Param("repo") {
167 return accountRedirect(c, "repo/" + c.Param("repo"))
168 }
169 user, b := db.GetUser(c.CertHash())
170 if !b {
171 return c.NoContent(gig.StatusBadRequest,
172 "Cannot find username")
173 }
174 // check if repo exist
175 if err := repo.RemoveRepo(name, user.Name);
176 err != nil {
177 return c.NoContent(gig.StatusBadRequest, err.Error())
178 }
179 if err := user.DeleteRepo(name, c.CertHash());
180 err != nil {
181 return c.NoContent(gig.StatusBadRequest, err.Error())
182 }
183 return accountRedirect(c, "")
184 }
185
186 func RepoRefs(c gig.Context) error {
187 return showRepo(c, pageRefs, true)
188 }
189
190 func RepoLicense(c gig.Context) error {
191 return showRepo(c, pageLicense, true)
192 }
193
194 func RepoReadme(c gig.Context) error {
195 return showRepo(c, pageReadme, true)
196 }
197
198 func RepoLog(c gig.Context) error {
199 return showRepo(c, pageLog, true)
200 }
201