From 234a5cff749e769c5a11fa3058119d5e397fbc19 Mon Sep 17 00:00:00 2001 From: log101 Date: Thu, 4 Jul 2024 19:52:51 +0300 Subject: [PATCH] feat: add uri generator --- go.mod | 7 ++-- go.sum | 2 ++ models/KonuluKonum.go | 10 ++++++ server.go | 78 ++++++++++++++++++++++--------------------- 4 files changed, 57 insertions(+), 40 deletions(-) create mode 100644 models/KonuluKonum.go diff --git a/go.mod b/go.mod index 62ed3ac..d536d07 100644 --- a/go.mod +++ b/go.mod @@ -2,12 +2,15 @@ module log101/konulu-konum-backend go 1.22.3 -require github.com/gofiber/fiber/v2 v2.52.5 +require ( + github.com/dchest/uniuri v1.2.0 + github.com/gofiber/fiber/v2 v2.52.5 + github.com/h2non/bimg v1.1.9 +) require ( github.com/andybalholm/brotli v1.0.5 // indirect github.com/google/uuid v1.5.0 // indirect - github.com/h2non/bimg v1.1.9 // indirect github.com/klauspost/compress v1.17.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect diff --git a/go.sum b/go.sum index 3dae570..8fa18bd 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,7 @@ github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/dchest/uniuri v1.2.0 h1:koIcOUdrTIivZgSLhHQvKgqdWZq5d7KdMEWF1Ud6+5g= +github.com/dchest/uniuri v1.2.0/go.mod h1:fSzm4SLHzNZvWLvWJew423PhAzkpNQYq+uNLq4kxhkY= github.com/gofiber/fiber/v2 v2.52.5 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo= github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= diff --git a/models/KonuluKonum.go b/models/KonuluKonum.go new file mode 100644 index 0000000..070e0de --- /dev/null +++ b/models/KonuluKonum.go @@ -0,0 +1,10 @@ +package models + +type KonuluKonum struct { + URI string + ImageURI string + Loc string + AuthorName string + Description string + UnlockedCounter string +} diff --git a/server.go b/server.go index 1d1c7d5..9d22dbb 100644 --- a/server.go +++ b/server.go @@ -6,6 +6,7 @@ import ( "os" "strings" + "github.com/dchest/uniuri" "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/compress" "github.com/gofiber/fiber/v2/middleware/logger" @@ -27,52 +28,53 @@ func main() { app.Post("/upload", func(c *fiber.Ctx) error { if form, err := c.MultipartForm(); err == nil { + // Get string input if token := form.Value["token"]; len(token) > 0 { fmt.Println(token[0]) } - files := form.File["documents"] - - for _, file := range files { - fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0]) - - // Save the files to disk: - // if err := c.SaveFile(file, fmt.Sprintf("./public/%s", file.Filename)); err != nil { - // return err - // } - - /* - buffer, err := bimg.Read(file.Filename) - if err != nil { - fmt.Fprintln(os.Stderr, err) - } - */ - - newFile, err := file.Open() - if err != nil { - fmt.Fprintln(os.Stderr, err) - } - defer newFile.Close() - - data, err := io.ReadAll(newFile) - if err != nil { - fmt.Fprintln(os.Stderr, err) - } - - newImage, err := bimg.NewImage(data).Convert(bimg.WEBP) - if err != nil { - fmt.Fprintln(os.Stderr, err) - } - - imageName := strings.Split(file.Filename, ".")[0] - - bimg.Write(fmt.Sprintf("./public/%s.webp", imageName), newImage) + // Get image + files := form.File["document"] + if len(files) != 1 { + fmt.Println("bad request") } - return err + file := form.File["document"][0] + fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0]) + + newFile, err := file.Open() + if err != nil { + fmt.Fprintln(os.Stderr, err) + } + defer newFile.Close() + + data, err := io.ReadAll(newFile) + if err != nil { + fmt.Fprintln(os.Stderr, err) + } + + // Compress image + newImage, err := bimg.NewImage(data).Convert(bimg.WEBP) + if err != nil { + fmt.Fprintln(os.Stderr, err) + } + + imageName := strings.Split(file.Filename, ".")[0] + + // Save image + bimg.Write(fmt.Sprintf("./public/%s.webp", imageName), newImage) + + // Generate public uri for the image + chars := uniuri.StdChars[26:52] + randomUri := uniuri.NewLenChars(10, chars) + imageUri := fmt.Sprintf("%s-%s-%s", randomUri[0:3], randomUri[3:7], randomUri[7:]) + + fmt.Println(imageUri) + + return c.SendStatus(fiber.StatusOK) } - return nil + return c.SendStatus(fiber.StatusBadRequest) }) app.Listen(":3000")