Coder Social home page Coder Social logo

b45-dumbmerch-deploy's Introduction

Cloudinary

Cloudinary is a cloud-based image and video management services. It enables users to upload, store, manage, manipulate, and deliver images and video for websites and apps.

Reference: Offical Cloudinary Website

Prepare

  • Click Media Library

  • Create a folder that will be used to store files

    img-1

  • Go to Settings โ†’ Upload

  • Scroll down to Upload presets, Click Add upload preset โ†’ fill in the form and click save

Server side (backend)

  • Install cloudinary

    go get github.com/cloudinary/cloudinary-go/v2
    
  • On upload_file.go file delete split uploads/ code and change data variable to ctx (on parameter 3)

    File: pkg/middleware/upload_file.go

    data := tempFile.Name()
    
    c.Set("dataFile", data)
    return next(c)
  • On handler product.go file

    File: handlers/product.go

    • Import pakcage

      "context"
      "github.com/cloudinary/cloudinary-go/v2"
      "github.com/cloudinary/cloudinary-go/v2/api/uploader"
    • On CreateProduct method, declare context background, CLOUD_NAME, API_KEY, API_SECRET

      var ctx = context.Background()
      var CLOUD_NAME = os.Getenv("CLOUD_NAME")
      var API_KEY = os.Getenv("API_KEY")
      var API_SECRET = os.Getenv("API_SECRET")
    • On CreateProduct method, Add Cloudinary credentials and Upload file to your Cloudinary folder

      // Add your Cloudinary credentials ...
      cld, _ := cloudinary.NewFromParams(CLOUD_NAME, API_KEY, API_SECRET)
      
      // Upload file to Cloudinary ...
      resp, err := cld.Upload.Upload(ctx, filepath, uploader.UploadParams{Folder: "dumbmerch"});
      
      if err != nil {
        fmt.Println(err.Error())
      }
    • On CreateProduct method, modify store file URL to database from resp.SecureURL

      product := models.Product{
        Name:   request.Name,
        Desc:   request.Desc,
        Price:  request.Price,
        Image:  resp.SecureURL, // Modify store file URL to database from resp.SecureURL ...
        Qty:    request.Qty,
        UserID: userId,
        Category:	category,
      }
  • Make sure modify this below code:

    File: handlers/product.go

    • On FindProducts method, delete pathfile manipulation

      for i, p := range products {
        imagePath := os.Getenv("PATH_FILE") + p.Image
        products[i].Image = imagePath
      }
    • On GetProduct method, delete pathfile manipulation

      product.Image = os.Getenv("PATH_FILE") + product.Image
  • Add CLOUD_NAME, API_KEY, API_SECRET variable and the values to .env

    File: .env

    CLOUD_NAME=your_cloud_name_here...
    API_KEY=your_api_key_here...
    API_SECRET=your_api_secret_here...

b45-dumbmerch-deploy's People

Contributors

jody-septiawan avatar suryaelidanto avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.