feat: add uri generator
This commit is contained in:
parent
cc4b3141e9
commit
234a5cff74
7
go.mod
7
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
|
||||
|
|
2
go.sum
2
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=
|
||||
|
|
10
models/KonuluKonum.go
Normal file
10
models/KonuluKonum.go
Normal file
|
@ -0,0 +1,10 @@
|
|||
package models
|
||||
|
||||
type KonuluKonum struct {
|
||||
URI string
|
||||
ImageURI string
|
||||
Loc string
|
||||
AuthorName string
|
||||
Description string
|
||||
UnlockedCounter string
|
||||
}
|
40
server.go
40
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,26 +28,19 @@ 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)
|
||||
// Get image
|
||||
files := form.File["document"]
|
||||
if len(files) != 1 {
|
||||
fmt.Println("bad request")
|
||||
}
|
||||
*/
|
||||
|
||||
file := form.File["document"][0]
|
||||
fmt.Println(file.Filename, file.Size, file.Header["Content-Type"][0])
|
||||
|
||||
newFile, err := file.Open()
|
||||
if err != nil {
|
||||
|
@ -59,6 +53,7 @@ func main() {
|
|||
fmt.Fprintln(os.Stderr, err)
|
||||
}
|
||||
|
||||
// Compress image
|
||||
newImage, err := bimg.NewImage(data).Convert(bimg.WEBP)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
|
@ -66,13 +61,20 @@ func main() {
|
|||
|
||||
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 err
|
||||
}
|
||||
|
||||
return nil
|
||||
return c.SendStatus(fiber.StatusBadRequest)
|
||||
})
|
||||
|
||||
app.Listen(":3000")
|
||||
|
|
Loading…
Reference in New Issue
Block a user