Git Product home page Git Product logo

cloudinary's Introduction

cloudinary

GitHub go.mod Go version GoDoc

Cloudinary API wrapper in Go.

Installation

go get -u -v github.com/komfy/cloudinary

Example

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/komfy/cloudinary"
)

type downloadHandler struct {
	cs *cloudinary.Service
}
//Creating a handler to donwload from form.
//I prefer to use handlers, because it's easier to add some external services into it's logic.
func (h downloadHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
	if req.Method != http.MethodPost {
		http.Error(res, "Method must be POST", http.StatusMethodNotAllowed)
		return
	}
	// Parsing multipart form is not necessary, FormFile invokes it if form isn't parsed.
	file, fh, err := req.FormFile("file")
	upResp, err := h.cs.Upload(fh.Filename, file, false)
	if err != nil {
		http.Error(res, err.Error(), 505)
		return
	}
	url := upResp.SecureURL
	res.Write([]byte(url))
}

func main() {
	// Creates new Service from environmental variable
	cloudinaryURL := os.Getenv("CLOUDINARY_URL")
	if cloudinaryURL == "" {
		log.Fatalln("there is no env variable with given name")
	}
	s, err := cloudinary.NewService(cloudinaryURL)
	if err != nil {
		log.Fatalln(err)
	}
	// Sending local file.
	file, err := os.Open("example.jpg")
	if err != nil {
		log.Fatalln(err)
	}
	upResp, err := s.Upload(file.Name(), file, false)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(upResp.URL)
	http.Handle("/download", downloadHandler{cs: s})
	http.ListenAndServe(":3000", nil)
}

Uploading mutiple form files

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"mime/multipart"
	"net/http"
	"os"

	"github.com/komfy/cloudinary"
)

type downloadHandler struct {
	cs *cloudinary.Service
}

//UploadFiles func
func (h downloadHandler) UploadMultipleFiles(files []*multipart.FileHeader) []string {
	urls := []string{}
	for _, file := range files {
		fmt.Println(file.Filename)
		data, err := file.Open()
		if err == nil {
			upResp, err := h.cs.Upload(file.Filename, data, false)
			if err != nil {
				panic(err)
			} else {
				urls = append(urls, upResp.URL)
			}
		}
		data.Close()
	}
	return urls
}

func (h downloadHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
	if req.Method != http.MethodPost {
		http.Error(res, "Method must be POST", http.StatusMethodNotAllowed)
		return
	}

	req.ParseMultipartForm(32 << 20)
	files := req.MultipartForm.File["files"] //name of file input
	urls := h.UploadMultipleFiles(files)
	resBody, _ := json.Marshal(urls)
	res.Write(resBody)
}

func main() {
	// Creates new Service from environmental variable
	cloudinaryURL := os.Getenv("CLOUDINARY_URL")
	if cloudinaryURL == "" {
		log.Fatalln("there is no env variable with given name")
	}
	s, err := cloudinary.NewService(cloudinaryURL)
	if err != nil {
		log.Fatalln(err)
	}

	http.Handle("/download", downloadHandler{cs: s})
	http.ListenAndServe(":3000", nil)
}

License

MIT

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.