💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › c44f9c54422fca19505ef99e04… captured on 2023-09-08 at 16:29:19. 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 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, "Invalid username")
46 }
47 query, err := c.QueryString()
48 if err != nil {
49 return c.NoContent(gig.StatusBadRequest, err.Error())
50 }
51 if query == "" {
52 return showRepo(c, pageFiles, true)
53 }
54 repofile, err := repo.GetFile(c.Param("repo"), user.Name, query)
55 if err != nil {
56 return c.NoContent(gig.StatusBadRequest, err.Error())
57 }
58 contents, err := repofile.Contents()
59 if err != nil {
60 return c.NoContent(gig.StatusBadRequest, err.Error())
61 }
62 return c.Gemini(contents)
63 }
64
65 func RepoFileContent(c gig.Context) error {
66 user, exist := db.GetUser(c.CertHash())
67 if !exist {
68 return c.NoContent(gig.StatusBadRequest, "Invalid username")
69 }
70 content, err := repo.GetPrivateFile(c.Param("repo"), user.Name,
71 c.Param("blob"), c.CertHash())
72 if err != nil {
73 return c.NoContent(gig.StatusBadRequest, err.Error())
74 }
75 header := "=>/account/repo/" + c.Param("repo") + "/files Go Back\n\n"
76 return c.Gemini(header + showFileContent(content))
77 }
78
79 func RepoFile(c gig.Context) error {
80 user, exist := db.GetUser(c.CertHash())
81 if !exist {
82 return c.NoContent(gig.StatusBadRequest, "Invalid username")
83 }
84 data, mtype, err := serveFile(c.Param("repo"), user.Name, c.Param("*"))
85 if err != nil {
86 return c.NoContent(gig.StatusBadRequest, err.Error())
87 }
88 return c.Blob(mtype, data)
89 }
90
91 func TogglePublic(c gig.Context) error {
92 user, exist := db.GetUser(c.CertHash())
93 if !exist {
94 return c.NoContent(gig.StatusBadRequest, "Invalid username")
95 }
96 if err := user.TogglePublic(c.Param("repo"), c.CertHash());
97 err != nil {
98 return c.NoContent(gig.StatusBadRequest, err.Error())
99 }
100 return c.NoContent(gig.StatusRedirectTemporary,
101 "/account/repo/" + c.Param("repo"))
102 }
103
104 func ChangeRepoName(c gig.Context) error {
105 newname, err := c.QueryString()
106 if err != nil {
107 return c.NoContent(gig.StatusBadRequest,
108 "Invalid input received")
109 }
110 if newname == "" {
111 return c.NoContent(gig.StatusInput, "New repository name")
112 }
113 user, exist := db.GetUser(c.CertHash())
114 if !exist {
115 return c.NoContent(gig.StatusBadRequest, "Invalid username")
116 }
117 // should check if repo exist and if the new name is free
118 if err := repo.ChangeRepoDir(c.Param("repo"), user.Name, newname);
119 err != nil {
120 return c.NoContent(gig.StatusBadRequest, err.Error())
121 }
122 if err := user.ChangeRepoName(c.Param("repo"), newname, c.CertHash());
123
124 err != nil {
125 return c.NoContent(gig.StatusBadRequest, err.Error())
126 }
127 return c.NoContent(gig.StatusRedirectTemporary,
128 "/account/repo/" + newname)
129 }
130
131 func ChangeRepoDesc(c gig.Context) error {
132 newdesc, err := c.QueryString()
133 if err != nil {
134 return c.NoContent(gig.StatusBadRequest,
135 "Invalid input received")
136 }
137 if newdesc == "" {
138 return c.NoContent(gig.StatusInput,
139 "New repository description")
140 }
141 user, exist := db.GetUser(c.CertHash())
142 if !exist {
143 return c.NoContent(gig.StatusBadRequest, "Invalid username")
144 }
145 if err := user.ChangeRepoDesc(c.Param("repo"), newdesc);
146 err != nil {
147 return c.NoContent(gig.StatusBadRequest, err.Error())
148 }
149 return c.NoContent(gig.StatusRedirectTemporary,
150 "/account/repo/" + c.Param("repo"))
151 }
152
153 func DeleteRepo(c gig.Context) error {
154 name, err := c.QueryString()
155 if err != nil {
156 return c.NoContent(gig.StatusBadRequest,
157 "Invalid input received")
158 }
159 if name == "" {
160 return c.NoContent(gig.StatusInput,
161 "To confirm type the repository name")
162 }
163 if name != c.Param("repo") {
164 return c.NoContent(gig.StatusRedirectTemporary,
165 "/account/repo/" + c.Param("repo"))
166 }
167 user, b := db.GetUser(c.CertHash())
168 if !b {
169 return c.NoContent(gig.StatusBadRequest,
170 "Cannot find username")
171 }
172 // check if repo exist
173 if err := repo.RemoveRepo(name, user.Name);
174 err != nil {
175 return c.NoContent(gig.StatusBadRequest, err.Error())
176 }
177 if err := user.DeleteRepo(name, c.CertHash());
178 err != nil {
179 return c.NoContent(gig.StatusBadRequest, err.Error())
180 }
181 return c.NoContent(gig.StatusRedirectTemporary, "/account")
182 }
183
184 func RepoRefs(c gig.Context) error {
185 return showRepo(c, pageRefs, true)
186 }
187
188 func RepoLicense(c gig.Context) error {
189 return showRepo(c, pageLicense, true)
190 }
191
192 func RepoReadme(c gig.Context) error {
193 return showRepo(c, pageReadme, true)
194 }
195
196 func RepoLog(c gig.Context) error {
197 return showRepo(c, pageLog, true)
198 }
199