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