Skip to content

Commit cf6747d

Browse files
committed
Order and delete funcs
1 parent 145a9a3 commit cf6747d

3 files changed

Lines changed: 40 additions & 2 deletions

File tree

database/mysql.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,18 @@ func AddFile(file models.File) (int64, error) {
7171
return id, nil
7272
}
7373

74+
func DeleteFile(name string) error {
75+
_, err := db.Exec("DELETE FROM rotoplas WHERE name = ?", name)
76+
if err != nil {
77+
return err
78+
}
79+
return nil
80+
}
81+
7482
func ListFiles(count int, page int) ([]File, error) {
7583
var files []File
7684

77-
rows, err := db.Query("SELECT * FROM rotoplas LIMIT ? OFFSET ?", count, (page-1)*count)
85+
rows, err := db.Query("SELECT * FROM rotoplas ORDER BY created_at DESC LIMIT ? OFFSET ? ", count, (page-1)*count)
7886
if err != nil {
7987
return nil, err
8088
}

main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,14 +162,28 @@ func isAudio(mime string) bool {
162162
}
163163

164164
func homePage(c *gin.Context) {
165+
admin := c.Query("admin")
165166
files, err := database.ListFiles(10, 1)
166167
if err != nil {
167168
log.Printf("Error listing files: %v", err)
168169
c.String(http.StatusInternalServerError, "Error retrieving files")
169170
return
170171
}
171172
c.HTML(http.StatusOK, "index.tmpl", gin.H{
172-
"files": files})
173+
"files": files,
174+
"admin": admin == "admin"})
175+
}
176+
177+
func delete(c *gin.Context) {
178+
name := c.Query("name")
179+
180+
err := database.DeleteFile(name)
181+
if err != nil {
182+
c.String(http.StatusInternalServerError, "Error deleting file")
183+
return
184+
}
185+
186+
c.String(http.StatusOK, "File deleted successfully")
173187
}
174188

175189
func main() {
@@ -209,6 +223,7 @@ func main() {
209223
router.GET("/", homePage)
210224
router.GET("/hash", getHash)
211225
router.POST("/upload", postFile)
226+
router.GET("/delete/", delete)
212227
router.Static("/files", "./files")
213228
router.GET("/thumbs/:filename", getThumbnail)
214229
router.StaticFile("favicon.ico", "./favicon.ico")

templates/index.tmpl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<p id="urls"></p>
2424

2525
<div class="file-list">
26+
{{ $admin := .admin }}
2627
{{ range.files }}
2728

2829
<div class="file-item">
@@ -41,12 +42,22 @@
4142
<a href="/files/{{.Name}}">
4243
{{.Name}}
4344
</a>
45+
{{ if $admin}}
46+
<a href="/delete?name={{.Name}}" style="color: red">
47+
<p>Delete</p>
48+
</a>
49+
{{ end }}
4450
</div>
4551

4652
{{ end }}
4753
</div>
4854
</div>
4955
</center>
56+
<footer>
57+
<a href="faq">
58+
<p>FAQ</p>
59+
</a>
60+
</footer>
5061
</body>
5162
<script>
5263
document.querySelector("#uploadForm").addEventListener("submit", (e) => {
@@ -73,6 +84,10 @@
7384
background-image: url("/bg.png");
7485
}
7586

87+
video {
88+
height: 140px;
89+
}
90+
7691
p {
7792
font-size: 10px;
7893
}

0 commit comments

Comments
 (0)