Selaa lähdekoodia

saving file data in redis is working pretty good

AETH-erial 7 kuukautta sitten
vanhempi
commit
a9f2f71fe5
3 muutettua tiedostoa jossa 21 lisäystä ja 7 poistoa
  1. 1 1
      html/templates/upload.html
  2. 11 3
      pkg/controller/content_handlers.go
  3. 9 3
      pkg/helpers/storage.go

+ 1 - 1
html/templates/upload.html

@@ -18,7 +18,7 @@
                         <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>
+                        <textarea name="title" 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>

+ 11 - 3
pkg/controller/content_handlers.go

@@ -1,7 +1,6 @@
 package controller
 
 import (
-	"fmt"
 	"os"
 	"time"
 
@@ -121,11 +120,20 @@ func (c *Controller) SaveFile(ctx *gin.Context) {
 		ctx.HTML(400, "upload_status", gin.H{"UpdateMessage": err, "Color": "red"})
 		return
 	}
-	//rds := helpers.NewRedisClient(c.RedisConfig)
+	var img helpers.ImageStoreItem
+	err = ctx.ShouldBind(&img); if err != nil {
+		ctx.HTML(500, "upload_status", gin.H{"UpdateMessage": err, "Color": "red"})
+		return
+	}
+	savedImg := helpers.NewImageStoreItem(file.Filename, img.Title, img.Desc)
+	err = c.SaveImage(savedImg); if err != nil {
+		ctx.HTML(500, "upload_status", gin.H{"UpdateMessage": err, "Color": "red"})
+		return
+	}
 
 
 	// Upload the file to specific dst.
-	err = ctx.SaveUploadedFile(file, fmt.Sprintf("%s/%s", helpers.GetImageStore(), file.Filename))
+	err = ctx.SaveUploadedFile(file, savedImg.AbsolutePath)
 	if err != nil {
 		ctx.HTML(400, "upload_status", gin.H{"UpdateMessage": err, "Color": "red"})
 		return

+ 9 - 3
pkg/helpers/storage.go

@@ -20,12 +20,18 @@ type ImageStoreItem struct {
 	Identifier		string	`json:"identifier"`
 	Filename		string	`json:"filename"`
 	AbsolutePath	string	`json:"absolute_path"`
-	Title			string	`json:"title"`
+	Title			string	`json:"title" form:"title"`
 	Created			string	`json:"created"`
-	Desc			string	`json:"description"`
+	Desc			string	`json:"description" form:"description"`
 	Category		string	`json:"category"`
 }
 
+/*
+Create a new ImageStoreItem
+	:param fname: the name of the file to be saved
+	:param title: the canonical title to give the image
+	:param desc: the description to associate to the image
+*/
 func NewImageStoreItem(fname string, title string, desc string) *ImageStoreItem {
 	id := uuid.New()
 	img := ImageStoreItem{
@@ -33,7 +39,7 @@ func NewImageStoreItem(fname string, title string, desc string) *ImageStoreItem
 		Filename: fname,
 		Title: title,
 		Category: DIGITAL_ART,
-		AbsolutePath: fmt.Sprintf("%s/%s", env.IMAGE_STORE, fname),
+		AbsolutePath: fmt.Sprintf("%s/%s", GetImageStore(), fname),
 		Created: time.Now().UTC().String(),
 		Desc: desc,
 	}