diff --git a/lib/objects/objects_post.go b/lib/objects/objects_post.go index eb8a5a9..bd9f571 100644 --- a/lib/objects/objects_post.go +++ b/lib/objects/objects_post.go @@ -26,6 +26,13 @@ type Post struct { Views uint32 `json:"views"` } +func (p *Post) Delete() error { + filePath := path.Join(getStorageDirectory(), p.Name) + + // Remove the file and return the result + return os.Remove(filePath) +} + // Get the path to the views JSON func getViewsFilePath() (string, error) { // Get the path to the storage then append the const name for the storage file @@ -94,7 +101,7 @@ func LoadViewsCache() error { return nil } -func AddViewToPost(postName string) uint32 { +func AddViewToPost(postName string, incrementViews bool) uint32 { // Lock the viewers mapping viewersLock.Lock() @@ -104,8 +111,10 @@ func AddViewToPost(postName string) uint32 { ViewsCache[postName] = 0 } - // Add to the counter - ViewsCache[postName]++ + if incrementViews { + // Add to the counter + ViewsCache[postName]++ + } // Unlock viewersLock.Unlock() @@ -175,7 +184,7 @@ func getStorageDirectory() string { } // Get a post from the file system -func GetPost(fileName string) Post { +func GetPost(fileName string, incrementViews bool) Post { // Get the base storage directory and make sure it exists storageDir := getStorageDirectory() @@ -183,7 +192,7 @@ func GetPost(fileName string) Post { filePath := fmt.Sprintf("%s%s", storageDir, fileName) // Get the post views and add 1 to them - postViews := AddViewToPost(fileName) + postViews := AddViewToPost(fileName, incrementViews) p := Post{ Name: fileName, @@ -316,7 +325,7 @@ func GetAllPosts() []Post { // Go through all of the files for _, v := range files { // Process the file into a pad - postList = append(postList, GetPost(v.Name())) + postList = append(postList, GetPost(v.Name(), false)) } // Return the post list diff --git a/lib/routes/routes_admin.go b/lib/routes/routes_admin.go index 5162728..e12aca3 100644 --- a/lib/routes/routes_admin.go +++ b/lib/routes/routes_admin.go @@ -64,7 +64,18 @@ func AdminRoutes(router *gin.RouterGroup) { }) router.GET("/delete/:padname", func(ctx *gin.Context) { + // Get the pad name that we bout' to delete + padName := ctx.Param("padname") + // Try and get the pad, check if valid + pad := objects.GetPost(padName, false) + + // Delete the pad + err := pad.Delete() + fmt.Println(err) + + // Redirect the user to the admin page + ctx.Redirect(http.StatusFound, "/admin/view") }) // Admin view route @@ -73,8 +84,6 @@ func AdminRoutes(router *gin.RouterGroup) { // Get all of the pads as a listing padList := objects.GetAllPosts() - fmt.Println(padList) - ctx.HTML(200, "admin_view.html", gin.H{ "title": "Admin", "padList": padList, diff --git a/lib/routes/routes_home.go b/lib/routes/routes_home.go index d512015..e7d8be0 100644 --- a/lib/routes/routes_home.go +++ b/lib/routes/routes_home.go @@ -41,7 +41,7 @@ func HomeRoutes(router *gin.Engine) { } postName = sanitize.XSS(sanitize.SingleLine(postName)) - post := objects.GetPost(postName) + post := objects.GetPost(postName, true) c.HTML(200, "page.html", gin.H{ "title": postName, diff --git a/templates/pages/admin_view.html b/templates/pages/admin_view.html index 9a092e5..18df451 100644 --- a/templates/pages/admin_view.html +++ b/templates/pages/admin_view.html @@ -37,7 +37,10 @@
Pad Name
-
+
+ Views +
+
Create Date
@@ -54,11 +57,14 @@ {{ $element.Name }}
-
+
+ {{ $element.Views }} +
+
{{ $element.LastModified }}
-
+
Delete