feat: add uri generator

This commit is contained in:
log101 2024-07-04 19:52:51 +03:00
parent cc4b3141e9
commit 234a5cff74
4 changed files with 57 additions and 40 deletions

7
go.mod
View File

@ -2,12 +2,15 @@ module log101/konulu-konum-backend
go 1.22.3 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 ( require (
github.com/andybalholm/brotli v1.0.5 // indirect github.com/andybalholm/brotli v1.0.5 // indirect
github.com/google/uuid v1.5.0 // 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/klauspost/compress v1.17.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect

2
go.sum
View File

@ -1,5 +1,7 @@
github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs=
github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= 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 h1:tWoP1MJQjGEe4GB5TUGOi7P2E0ZMMRx5ZTG4rT+yGMo=
github.com/gofiber/fiber/v2 v2.52.5/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= 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= github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU=

10
models/KonuluKonum.go Normal file
View File

@ -0,0 +1,10 @@
package models
type KonuluKonum struct {
URI string
ImageURI string
Loc string
AuthorName string
Description string
UnlockedCounter string
}

View File

@ -6,6 +6,7 @@ import (
"os" "os"
"strings" "strings"
"github.com/dchest/uniuri"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/compress" "github.com/gofiber/fiber/v2/middleware/compress"
"github.com/gofiber/fiber/v2/middleware/logger" "github.com/gofiber/fiber/v2/middleware/logger"
@ -27,52 +28,53 @@ func main() {
app.Post("/upload", func(c *fiber.Ctx) error { app.Post("/upload", func(c *fiber.Ctx) error {
if form, err := c.MultipartForm(); err == nil { if form, err := c.MultipartForm(); err == nil {
// Get string input
if token := form.Value["token"]; len(token) > 0 { if token := form.Value["token"]; len(token) > 0 {
fmt.Println(token[0]) fmt.Println(token[0])
} }
files := form.File["documents"] // Get image
files := form.File["document"]
for _, file := range files { if len(files) != 1 {
fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0]) fmt.Println("bad request")
// 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)
} }
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") app.Listen(":3000")