Skip to content

Commit

Permalink
Move to Cloudinary from imgbb
Browse files Browse the repository at this point in the history
  • Loading branch information
lil5 committed Jun 3, 2024
1 parent 114ee51 commit 9f5e9ff
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 25 deletions.
4 changes: 4 additions & 0 deletions server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ require (
github.com/antihax/optional v1.0.0 // indirect
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/cloudinary/cloudinary-go v1.7.0
github.com/creasty/defaults v1.5.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
Expand All @@ -42,6 +44,8 @@ require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/gorilla/schema v1.2.0 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/json-iterator/go v1.1.12 // indirect
Expand Down
12 changes: 12 additions & 0 deletions server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cloudinary/cloudinary-go v1.7.0 h1:KI+1C5JM1TsWi3NNSVitshnQEc5n27firfWIEPDsoWQ=
github.com/cloudinary/cloudinary-go v1.7.0/go.mod h1:V1AhCEPFlSN2FN3OosHgu4iX1SkusvDCgfSE7eU79Vo=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creasty/defaults v1.5.1 h1:j8WexcS3d/t4ZmllX4GEkl4wIB/trOr035ajcLHCISM=
github.com/creasty/defaults v1.5.1/go.mod h1:FPZ+Y0WNrbqOVw+c6av63eyHUAl6pMHZwqLPvXUZGfY=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -89,6 +93,7 @@ github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QX
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
Expand Down Expand Up @@ -145,10 +150,16 @@ github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20200430221834-fc25d7d30c6d/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200708004538-1a94d8640e99/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.2.0 h1:qJYtXnJRWmpe7m/3XlyhrsLrEURqHRM2kxzoxXqyUDs=
github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gorilla/schema v1.2.0 h1:YufUaxZYCKGFuAq3c96BOhjgd5nmXiOY9NGzF247Tsc=
github.com/gorilla/schema v1.2.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/heimdalr/dag v1.0.1/go.mod h1:t+ZkR+sjKL4xhlE1B9rwpvwfo+x+2R0363efS+Oghns=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/jaswdr/faker v1.18.0 h1:sJ8HQLxvNRH+Ond1pTLR01BAxMN0iuYe+6aD30H0cRE=
github.com/jaswdr/faker v1.18.0/go.mod h1:x7ZlyB1AZqwqKZgyQlnqEG8FDptmHlncA5u2zY/yi6w=
Expand Down Expand Up @@ -207,6 +218,7 @@ github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSS
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"fmt"
"net/http"

"github.com/cloudinary/cloudinary-go"
"github.com/cloudinary/cloudinary-go/api/uploader"
"github.com/disintegration/imaging"
"github.com/gin-gonic/gin"
"github.com/the-clothing-loop/website/server/internal/app"
Expand All @@ -15,8 +17,13 @@ import (
"github.com/the-clothing-loop/website/server/pkg/imgbb"
)

// @param expiration (optional): Enable this if you want to force uploads to be auto deleted after certain time (in seconds 60-15552000)
func imgBBupload(c *gin.Context, size int, expiration int) (imgRes *imgbb.ImgbbResponse, err error) {
type ImageUploadResponse struct {
Delete string `json:"delete"`
Thumbnail string `json:"thumbnail"`
Image string `json:"image"`
}

func imgBBupload(c *gin.Context, size int, expiration int) (imgRes *ImageUploadResponse, err error) {
if app.Config.IMGBB_KEY == "" {
return nil, errors.New("Api key to found")
}
Expand All @@ -35,20 +42,47 @@ func imgBBupload(c *gin.Context, size int, expiration int) (imgRes *imgbb.ImgbbR
return nil, fmt.Errorf("Unable to encode to jpeg: %v", err)
}

bStr := base64.StdEncoding.EncodeToString(buf.Bytes())
// bStr = fmt.Sprintf("data:image/jpeg;base64,%s", bStr)
// os.WriteFile("test.jpg", buf.Bytes(), 775)
// os.WriteFile("test.jpg.base64", []byte(bStr), 0775)

// c.String(http.StatusTeapot, "image", bStr)
// fmt.Print(bStr)
// return nil, fmt.Errorf("test")

image, err := imgbb.Upload(app.Config.IMGBB_KEY, bStr, expiration, "")
cld, err := cloudinary.NewFromParams("dr5iag9id", "532794798513218", app.Config.IMGBB_KEY)
if err != nil {
return nil, fmt.Errorf("Unable to upload to ImgBB: %v", err)
return nil, fmt.Errorf("Unable to setup Cloudinary: %v", err)
}
resUpload, err := cld.Upload.Upload(c.Request.Context(), buf, uploader.UploadParams{
DiscardOriginalFilename: true,
UniqueFilename: true,
Folder: "upload",
})
if err != nil {
return nil, fmt.Errorf("Unable to upload to Cloudinary: %v", err)
}

res := &ImageUploadResponse{
Delete: "",
Thumbnail: "",
Image: "",
}
return image, nil
{
my_image, err := cld.Image(resUpload.PublicID)
if err != nil {
return nil, fmt.Errorf("Unable to create Cloudinary image from public id: %v", err)
}
res.Image, err = my_image.String()
if err != nil {
return nil, fmt.Errorf("Unable to create Cloudinary image url: %v", err)
}
}
{
my_image, err := cld.Image(resUpload.PublicID)
if err != nil {
return nil, fmt.Errorf("Unable to create Cloudinary image from public id: %v", err)
}
my_image.Transformation = "c_fill,h_180,w_180"
res.Thumbnail, err = my_image.String()
if err != nil {
return nil, fmt.Errorf("Unable to create Cloudinary image url: %v", err)
}
}

return res, nil
}
func ImageUpload(c *gin.Context) {
db := getDB(c)
Expand All @@ -72,17 +106,7 @@ func ImageUpload(c *gin.Context) {
return
}

if !res.Success {
goscope.Log.Warningf("Unable to upload image: %+v", res)
c.String(http.StatusBadRequest, "Unable to upload image")
return
}

c.JSON(http.StatusOK, gin.H{
"delete": res.Data.DeleteURL,
"image": res.Data.Image.Url,
"thumb": res.Data.Thumb.Url,
})
c.JSON(http.StatusOK, res)
}

func ImageDelete(c *gin.Context) {
Expand Down

0 comments on commit 9f5e9ff

Please sign in to comment.