1
0
mirror of https://github.com/JustKato/FreePad.git synced 2026-02-23 15:50:46 +02:00

+ Reading from files

+ Working Page access
This commit is contained in:
2022-05-20 00:46:14 +03:00
parent a3de00aabd
commit a76b9d02ba
10 changed files with 177 additions and 39 deletions

View File

@@ -0,0 +1,69 @@
package objects
import (
"fmt"
"os"
)
type Post struct {
Name string `json:"name"`
Content string `json:"content"`
}
func getStorageDirectory() string {
baseStoragePath, exists := os.LookupEnv("PAD_STORAGE_PATH")
if !exists {
baseStoragePath = "/tmp/"
}
// Check if the base storage path exists
if _, err := os.Stat(baseStoragePath); os.IsNotExist(err) {
// Looks like the base storage path was NOT set, create the dir
err = os.Mkdir(baseStoragePath, 0777)
// Check for errors
if err != nil {
// No way this sends an error unless it goes horribly wrong.
panic(err)
}
}
// Return the base storage path
return baseStoragePath
}
func GetPost(fileName string) Post {
// Get the base storage directory and make sure it exists
storageDir := getStorageDirectory()
// Generate the file path
filePath := fmt.Sprintf("%s%s", storageDir, fileName)
fmt.Println("Reading: ", filePath)
p := Post{
Name: fileName,
Content: "",
}
// Check if the file exits
if _, err := os.Stat(filePath); !os.IsNotExist(err) {
fmt.Println("Found ", filePath)
// File does exist, read it and set the content
data, err := os.ReadFile(filePath)
if err != nil {
fmt.Println("Error:", err)
}
// Get the content of the file and put it in the response
p.Content = string(data)
fmt.Println("Loaded content for ", filePath)
}
return p
}
func WritePost(p Post) error {
return nil
}

View File

@@ -1,8 +1,13 @@
package routes
import (
"fmt"
"net/url"
"github.com/JustKato/FreePad/lib/helper"
"github.com/JustKato/FreePad/lib/objects"
"github.com/gin-gonic/gin"
"github.com/mrz1836/go-sanitize"
)
func HomeRoutes(router *gin.Engine) {
@@ -18,9 +23,22 @@ func HomeRoutes(router *gin.Engine) {
// Get the post we are looking for.
postName := c.Param("post")
fmt.Println("Sanitizing ", postName)
// Sanitize the postName
newPostName, err := url.QueryUnescape(postName)
if err == nil {
postName = newPostName
}
postName = sanitize.AlphaNumeric(postName, true)
fmt.Println("Fetching ", postName)
post := objects.GetPost(postName)
c.HTML(200, "page.html", gin.H{
"title": postName,
"post_content": "",
"post_content": post.Content,
"domain_base": helper.GetDomainBase(),
})
})

View File

@@ -1,6 +0,0 @@
package types
type Post struct {
Name string `json:"name"`
Content string `json:"content"`
}