💾 Archived View for gemini.rmf-dev.com › repo › Vaati › Gemigit › files › 5dce3ede4009079b30fe9685e8… captured on 2023-03-20 at 18:03:26. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2023-01-29)

🚧 View Differences

-=-=-=-=-=-=-

0 package gmi

1

2 import (

3 "io"

4 "strconv"

5 "strings"

6

7 "gemigit/db"

8 "gemigit/repo"

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 number := strconv.Itoa(i)

19 space := 6 - len(number)

20 if space < 1 {

21 space = 1

22 }

23 file += number + strings.Repeat(" ", space)

24 file += 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,

52 "Invalid username")

53 }

54 query, err := c.QueryString()

55 if err != nil {

56 return c.NoContent(gig.StatusBadRequest, err.Error())

57 }

58 if query == "" {

59 return showRepo(c, pageFiles, true)

60 }

61 repofile, err := repo.GetFile(c.Param("repo"),

62 user.Name, query)

63 if err != nil {

64 return c.NoContent(gig.StatusBadRequest, err.Error())

65 }

66 contents, err := repofile.Contents()

67 if err != nil {

68 return c.NoContent(gig.StatusBadRequest, err.Error())

69 }

70 return c.Gemini(contents)

71 }

72

73 func RepoFileContent(c gig.Context) error {

74 user, exist := db.GetUser(c.CertHash())

75 if !exist {

76 return c.NoContent(gig.StatusBadRequest,

77 "Invalid username")

78 }

79 content, err := repo.GetPrivateFile(c.Param("repo"), user.Name,

80 c.Param("blob"), c.CertHash())

81 if err != nil {

82 return c.NoContent(gig.StatusBadRequest,

83 err.Error())

84 }

85 return c.Gemini(showFileContent(content))

86 }

87

88 func RepoFile(c gig.Context) error {

89 user, exist := db.GetUser(c.CertHash())

90 if !exist {

91 return c.NoContent(gig.StatusBadRequest,

92 "Invalid username")

93 }

94 data, mtype, err := serveFile(c.Param("repo"), user.Name, c.Param("*"))

95 if err != nil {

96 return c.NoContent(gig.StatusBadRequest, err.Error())

97 }

98 return c.Blob(mtype, data)

99 }

100

101 func TogglePublic(c gig.Context) error {

102 user, exist := db.GetUser(c.CertHash())

103 if !exist {

104 return c.NoContent(gig.StatusBadRequest,

105 "Invalid username")

106 }

107 if err := user.TogglePublic(c.Param("repo"), c.CertHash());

108 err != nil {

109 return c.NoContent(gig.StatusBadRequest, err.Error())

110 }

111 return c.NoContent(gig.StatusRedirectTemporary,

112 "/account/repo/" + c.Param("repo"))

113 }

114

115 func ChangeRepoName(c gig.Context) error {

116 newname, err := c.QueryString()

117 if err != nil {

118 return c.NoContent(gig.StatusBadRequest,

119 "Invalid input received")

120 }

121 if newname == "" {

122 return c.NoContent(gig.StatusInput,

123 "New repository name")

124 }

125 user, exist := db.GetUser(c.CertHash())

126 if !exist {

127 return c.NoContent(gig.StatusBadRequest,

128 "Invalid username")

129 }

130 // should check if repo exist and if the new name is free

131 if err := repo.ChangeRepoDir(c.Param("repo"), user.Name, newname);

132 err != nil {

133 return c.NoContent(gig.StatusBadRequest, err.Error())

134 }

135 if err := user.ChangeRepoName(c.Param("repo"), newname, c.CertHash());

136

137 err != nil {

138 return c.NoContent(gig.StatusBadRequest, err.Error())

139 }

140 return c.NoContent(gig.StatusRedirectTemporary,

141 "/account/repo/" + newname)

142 }

143

144 func ChangeRepoDesc(c gig.Context) error {

145 newdesc, err := c.QueryString()

146 if err != nil {

147 return c.NoContent(gig.StatusBadRequest,

148 "Invalid input received")

149 }

150 if newdesc == "" {

151 return c.NoContent(gig.StatusInput,

152 "New repository description")

153 }

154 user, exist := db.GetUser(c.CertHash())

155 if !exist {

156 return c.NoContent(gig.StatusBadRequest,

157 "Invalid username")

158 }

159 if err := user.ChangeRepoDesc(c.Param("repo"), newdesc);

160 err != nil {

161 return c.NoContent(gig.StatusBadRequest, err.Error())

162 }

163 return c.NoContent(gig.StatusRedirectTemporary,

164 "/account/repo/" + c.Param("repo"))

165 }

166

167 func DeleteRepo(c gig.Context) error {

168 name, err := c.QueryString()

169 if err != nil {

170 return c.NoContent(gig.StatusBadRequest,

171 "Invalid input received")

172 }

173 if name == "" {

174 return c.NoContent(gig.StatusInput,

175 "To confirm type the repository name")

176 }

177 if name != c.Param("repo") {

178 return c.NoContent(gig.StatusRedirectTemporary,

179 "/account/repo/" +

180 c.Param("repo"))

181 }

182 user, b := db.GetUser(c.CertHash())

183 if !b {

184 return c.NoContent(gig.StatusBadRequest,

185 "Cannot find username")

186 }

187 // check if repo exist

188 if err := repo.RemoveRepo(name, user.Name);

189 err != nil {

190 return c.NoContent(gig.StatusBadRequest,

191 err.Error())

192 }

193 if err := user.DeleteRepo(name, c.CertHash());

194 err != nil {

195 return c.NoContent(gig.StatusBadRequest,

196 err.Error())

197 }

198 return c.NoContent(gig.StatusRedirectTemporary,

199 "/account")

200 }

201

202 func RepoRefs(c gig.Context) error {

203 return showRepo(c, pageRefs, true)

204 }

205

206 func RepoLicense(c gig.Context) error {

207 return showRepo(c, pageLicense, true)

208 }

209

210 func RepoReadme(c gig.Context) error {

211 return showRepo(c, pageReadme, true)

212 }

213

214 func RepoLog(c gig.Context) error {

215 return showRepo(c, pageLog, true)

216 }

217