Skip to content

Commit f8f43de

Browse files
committed
Rudimentary admin tools
1 parent 0cbe021 commit f8f43de

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
**/files/*
22
mysql_data/*
3+
.idea/*

database/mysql.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,30 @@ func DeleteFile(name string) error {
8080
return nil
8181
}
8282

83+
func ListGodMode(count int, page int) ([]File, error) {
84+
var files []File
85+
86+
rows, err := db.Query("SELECT * FROM rotoplas ORDER BY created_at DESC LIMIT ? OFFSET ? ", count, (page-1)*count)
87+
if err != nil {
88+
return nil, err
89+
}
90+
defer rows.Close()
91+
92+
for rows.Next() {
93+
var file File
94+
if err := rows.Scan(&file.ID, &file.Name, &file.Size, &file.UploadIp, &file.CreatedAt, &file.MimeType, &file.Hidden); err != nil {
95+
return nil, err
96+
}
97+
files = append(files, file)
98+
}
99+
100+
if err := rows.Err(); err != nil {
101+
return nil, err
102+
}
103+
104+
return files, nil
105+
}
106+
83107
func ListFiles(count int, page int) ([]File, error) {
84108
var files []File
85109

main.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ func postFile(c *gin.Context) {
8181
c.String(http.StatusInternalServerError, "Error saving file metadata")
8282
return
8383
}
84+
8485
c.JSON(http.StatusOK, gin.H{
8586
"message": "File uploaded successfully",
8687
"filename": encoded,
@@ -167,7 +168,6 @@ func isAudio(mime string) bool {
167168
}
168169

169170
func homePage(c *gin.Context) {
170-
admin := c.Query("admin")
171171
pageStr := c.Query("page")
172172

173173
page, err := strconv.Atoi(pageStr)
@@ -185,13 +185,40 @@ func homePage(c *gin.Context) {
185185
}
186186
c.HTML(http.StatusOK, "index.tmpl", gin.H{
187187
"files": files,
188-
"admin": admin == "admin",
188+
"admin": false,
189189
"next": int(count) > page*10,
190190
"prev": page > 1,
191191
"nextPage": page + 1,
192192
"prevPage": page - 1})
193193
}
194194

195+
func goGodMode(c *gin.Context) {
196+
pageStr := c.Query("page")
197+
198+
page, err := strconv.Atoi(pageStr)
199+
if err != nil {
200+
page = 1 // Default to page 1 if parsing fails
201+
}
202+
203+
files, err := database.ListGodMode(10, page)
204+
count, _ := database.Count()
205+
206+
if err != nil {
207+
log.Printf("Error listing files: %v", err)
208+
c.String(http.StatusInternalServerError, "Error retrieving files")
209+
return
210+
}
211+
212+
c.HTML(http.StatusOK, "index.tmpl", gin.H{
213+
"files": files,
214+
"admin": true,
215+
"next": int(count) > page*10,
216+
"prev": page > 1,
217+
"nextPage": page + 1,
218+
"prevPage": page - 1})
219+
220+
}
221+
195222
func deleteFile(c *gin.Context) {
196223
name := c.Query("name")
197224

@@ -201,7 +228,7 @@ func deleteFile(c *gin.Context) {
201228
return
202229
}
203230

204-
c.String(http.StatusOK, "File deleted successfully")
231+
c.Redirect(http.StatusSeeOther, "/")
205232
}
206233

207234
func main() {
@@ -239,6 +266,7 @@ func main() {
239266
})
240267

241268
router.GET("/", homePage)
269+
router.GET("/godmode", goGodMode)
242270
router.GET("/hash", getHash)
243271
router.POST("/upload", postFile)
244272
router.GET("/delete/", deleteFile)

0 commit comments

Comments
 (0)