فهرست منبع

removed images from the image store and added upload image endpoint with html

AETH-erial 7 ماه پیش
والد
کامیت
c0821d3fb8

+ 4 - 1
.gitignore

@@ -8,4 +8,7 @@ build/linux/seed/*
 html/assets/images/*
 
 # db seed data
-db_seed/*
+db_seed/*
+
+# ignoring nohup.out file
+nohup.out

+ 16 - 0
cmd/webserver/webserver.go

@@ -84,6 +84,22 @@ func main() {
 		"upload_status",
 		fmt.Sprintf("%s/templates/upload_status.html", WEB_ROOT),
 	)
+	renderer.AddFromFiles(
+		"unhandled_error",
+		fmt.Sprintf("%s/templates/unhandled_error.html", WEB_ROOT),
+		fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
+		fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
+		fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
+		fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
+	)
+	renderer.AddFromFiles(
+		"upload",
+		fmt.Sprintf("%s/templates/upload.html", WEB_ROOT),
+		fmt.Sprintf("%s/templates/menu.html", WEB_ROOT),
+		fmt.Sprintf("%s/templates/link.html", WEB_ROOT),
+		fmt.Sprintf("%s/templates/navigation.html", WEB_ROOT),
+		fmt.Sprintf("%s/templates/listing.html", WEB_ROOT),
+	)
 	e := gin.Default()
 	e.HTMLRender = renderer
 	routes.Register(e, WEB_ROOT, DOMAIN_NAME, REDIS_PORT, REDIS_ADDR)

BIN
html/assets/images/blackandwhitedesign.jpg


BIN
html/assets/images/keepoff_small.jpg


BIN
html/assets/images/someoneatthedoor_small.jpg


BIN
html/assets/images/whyhere.gif


+ 1 - 1
html/templates/login.html

@@ -39,7 +39,7 @@
             </div>
         </div>
     </body>
-    <script src="https://unpkg.com/htmx.org@1.9.4"></script> 
+    <script src="https://unpkg.com/htmx.org@1.9.4"></script>
     <script src="https://unpkg.com/htmx.org/dist/ext/json-enc.js"></script>
 </html>
 {{ end }}

+ 19 - 0
html/templates/unhandled_error.html

@@ -0,0 +1,19 @@
+{{ define "unhandled_error.html" }}
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
+        <meta name="viewport" content="width=device-width, initial-scale=1">
+        <link rel="stylesheet" href="/api/v1/style/bootstrap.min.css">
+        <link rel="stylesheet" href="/api/v1/style/mdb/mdb.min.css">
+    </head>
+    <body style="background-color: rgb(56, 56, 56);">
+        {{ template "navigation.html" .navigation }}
+        <div class="col container-fluid" style="max-width: 80vw; background-color: rgb(22, 22, 22);">
+            <a style="color: red; height: fit-content; font-size: larger; font-family: monospace;">
+                STATUS: {{ .StatusCode }}. UNHANDLED EXCEPTION RAISED: {{ .Reason }}
+            </a>
+        </div>
+    </body>
+</html>
+{{ end }}

+ 36 - 18
html/templates/upload.html

@@ -1,24 +1,42 @@
 {{ define "upload.html" }}
 <!DOCTYPE html>
 <html lang="en">
-        <form hx-encoding='multipart/form-data' hx-post='/admin/images/upload'
-             _='on htmx:xhr:progress(loaded, total) set #progress.value to (loaded/total)*100'>
-                <div class="row container p-2 m-2">
-                    <input type='file' name='file'>
-                </div>
-                <div class="row container p-2 m-2">
-                    <textarea name="description" wrap="soft" required="required" placeholder="Name of the piece?"></textarea>
-                </div>
-                <div class="row container p-2 m-2">
-                    <textarea name="description" wrap="soft" required="required" placeholder="What would you like to say about it?"></textarea>
-                </div>
-                <div class="row container p-2 m-2">
-                    <button>
-                       Upload
-                    </button>
-                    <progress id='progress' value='0' max='100'></progress>
-                </div>
-        </form>
+<head>
+    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
+    <meta name="viewport" content="width=device-width, initial-scale=1">
+    <link rel="stylesheet" href="/api/v1/style/bootstrap.min.css">
+    <link rel="stylesheet" href="/api/v1/style/mdb/mdb.min.css">
+</head>
+<body style="background-color: rgb(56, 56, 56);">
+    {{ template "navigation.html" .navigation }}
+    <div class="container-fluid row">
+        <div class="col"></div>
+        <div class="col" style="min-width: 80vw; background-color: rgb(22, 22, 22); font-family: monospace;">
+            <form hx-encoding='multipart/form-data' hx-post='/admin/images/upload'
+                 _='on htmx:xhr:progress(loaded, total) set #progress.value to (loaded/total)*100'>
+                    <div class="row container p-2 m-2">
+                        <input type='file' name='file'>
+                    </div>
+                    <div class="row container p-2 m-2">
+                        <textarea name="description" wrap="soft" required="required" placeholder="Name of the piece?"></textarea>
+                    </div>
+                    <div class="row container p-2 m-2">
+                        <textarea name="description" wrap="soft" required="required" placeholder="What would you like to say about it?"></textarea>
+                    </div>
+                    <div class="row container p-2 m-2">
+                        <button>
+                           Upload
+                        </button>
+                        <progress id='progress' value='0' max='100'></progress>
+                    </div>
+            </form>
+        </div>
+        <div class="col"></div>
+    </div>
+    <script src="https://unpkg.com/htmx.org@1.9.4"></script>
+    <script src="https://unpkg.com/htmx.org/dist/ext/json-enc.js"></script>
+    <script type="text/javascript" src="/api/v1/js/bootstrap.bundle.min.js" crossorigin="anonymous"></script>
+    </body>
 </html>
 
 {{ end }}

+ 14 - 4
pkg/controller/content_handlers.go

@@ -103,17 +103,27 @@ func (c *Controller) MakeBlogPost(ctx *gin.Context) {
 
 }
 
+
+func (c *Controller) ServeFileUpload(ctx *gin.Context) {
+	ctx.HTML(200, "upload", gin.H{
+		"navigation": gin.H{
+			"menu": c.Menu(),
+			"headers": c.Headers().Elements,
+		},
+	})
+}
+
+
+
 func (c *Controller) SaveFile(ctx *gin.Context) {
 	file, _ := ctx.FormFile("file")
 
 	// Upload the file to specific dst.
 	err := ctx.SaveUploadedFile(file, fmt.Sprintf("%s/%s", helpers.GetImageStore(), file.Filename))
 	if err != nil {
-		ctx.JSON(400, map[string]string{
-			"Error": err.Error(),
-		})
+		ctx.HTML(400, "upload_status", gin.H{"UpdateMessage": "Update Failed!", "Color": "red"})
 		return
 	}
 
-	ctx.HTML(200, "upload", nil)
+	ctx.HTML(200, "upload_status", gin.H{"UpdateMessage": "Update Successful!", "Color": "green"})
 }

+ 11 - 6
pkg/controller/html_handlers.go

@@ -1,7 +1,6 @@
 package controller
 
 import (
-	"fmt"
 	"net/http"
 
 	"git.aetherial.dev/aeth/keiji/pkg/helpers"
@@ -138,15 +137,21 @@ func (c *Controller) ServeTechnicalWriteups(ctx *gin.Context) {
 // @Tags webpages
 // @Router /digital [get]
 func (c *Controller) ServeDigitalArt(ctx *gin.Context) {
+	fnames, err := helpers.GetImagePaths(4, 0)
+	if err != nil {
+		ctx.HTML(http.StatusInternalServerError, "unhandled_error",
+		gin.H{
+			"StatusCode": http.StatusInternalServerError,
+			"Reason": err.Error(),
+		},
+	)
+	return
+	}
 	ctx.HTML(http.StatusOK, "digital_art", gin.H{
 		"navigation": gin.H{
 			"menu": c.Menu(),
 			"headers": c.Headers().Elements,
 		},
-		"images": []string{
-			fmt.Sprintf("/api/v1/images/%s", "keepoff_small.jpg"),
-			fmt.Sprintf("/api/v1/images/%s", "someoneatthedoor_small.jpg"),
-			fmt.Sprintf("/api/v1/images/%s", "whyhere.gif"),
-		},
+		"images": fnames,
 	})
 }

+ 32 - 1
pkg/helpers/storage.go

@@ -1,15 +1,46 @@
 package helpers
 
 import (
+	"fmt"
 	"os"
 
 	"git.aetherial.dev/aeth/keiji/pkg/env"
 )
 
+type InvalidSkipArg struct {Skip int}
 
+func (i *InvalidSkipArg) Error() string {
+	return fmt.Sprintf("Invalid skip amount was passed: %s", i.Skip)
+}
 
-
+/*
+Function to return the location of the image store. Wrapping the env call in
+a function so that refactoring is easier
+*/
 func GetImageStore() string {
 	return os.Getenv(env.IMAGE_STORE)
 }
 
+/*
+Return all of the filenames of the images that exist in the imagestore location
+	:param limit: the limit of filenames to return
+	:param skip: the index to start getting images from
+*/
+func GetImagePaths(limit int, skip int) ([]string, error) {
+	f, err := os.ReadDir(GetImageStore())
+	if err != nil {
+		return nil, err
+	}
+	if len(f) < skip {
+		return nil, &InvalidSkipArg{Skip: skip}
+	}
+	if len(f) < limit {
+		return nil, &InvalidSkipArg{Skip: limit}
+	}
+	fnames := []string{}
+	for i := skip; i < (skip + limit); i++ {
+		fnames = append(fnames, fmt.Sprintf("/api/v1/images/%s", f[i].Name()))
+	}
+	return fnames, err
+}
+

+ 2 - 0
pkg/routes/register.go

@@ -30,6 +30,8 @@ func Register(e *gin.Engine, root string, domain string, redisPort string, redis
 
 	priv := e.Group("/admin")
 	priv.Use(c.IsAuthenticated)
+	priv.GET("/upload", c.ServeFileUpload)
+	priv.POST("/upload", c.SaveFile)
 	priv.GET("/panel", c.AdminPanel)
 	priv.POST("/add-document", c.AddDocument)
 	priv.POST("/images/upload", c.SaveFile)