imgbb

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2023 License: Apache-2.0 Imports: 8 Imported by: 1

README

ImgBB

ImgBB is an imgbb.com api client.

Installation

go get github.com/JohnNON/ImgBB

Example of usage:

package main

import (
    "context"
    "crypto/md5"
    "encoding/hex"
    "fmt"
    "io"
    "log"
    "net/http"
    "os"
    "time"

    imgBB "github.com/JohnNON/ImgBB"
)

const (
    key = "your-imgBB-api-key"
)

func main() {
    f, err := os.Open("example.jpg")
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()

    b, err := io.ReadAll(f)
    if err != nil {
        log.Fatal(err)
    }

    img, err := imgBB.NewImageFromFile(hashSum(b), 60, b)
    if err != nil {
        log.Fatal(err)
    }

    httpClient := &http.Client{
        Timeout: 5 * time.Second,
    }

    imgBBClient := imgBB.NewClient(httpClient, key)

    resp, err := imgBBClient.Upload(context.Background(), img)
    if err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%v\n", resp)
}

func hashSum(b []byte) string {
    sum := md5.Sum(b)

    return hex.EncodeToString(sum[:])
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is an imgbb api client.

func NewClient

func NewClient(client *http.Client, key string) *Client

NewClient create a new ImgBB api client.

func (*Client) Upload

func (i *Client) Upload(ctx context.Context, img *Image) (Response, error)

Upload is a function to upload image to ImgBB.

type Data

type Data struct {
	ID         string `json:"id"`
	Title      string `json:"title"`
	URLViewer  string `json:"url_viewer"`
	URL        string `json:"url"`
	DisplayURL string `json:"display_url"`
	Width      int    `json:"width"`
	Height     int    `json:"height"`
	Size       int    `json:"size"`
	Time       int64  `json:"time"`
	TTL        int64  `json:"expiration"`
	Image      Info   `json:"image"`
	Thumb      Info   `json:"thumb"`
	Medium     Info   `json:"medium"`
	DeleteURL  string `json:"delete_url"`
}

Data is an information about uploaded file.

type Error

type Error struct {
	StatusCode int       `json:"status_code"`
	StatusText string    `json:"status_txt"`
	Info       ErrorInfo `json:"error"`
}

Error is an upload error response.

func (Error) Error

func (e Error) Error() string

type ErrorInfo

type ErrorInfo struct {
	Message string `json:"message"`
	Code    int    `json:"code"`
	Context string `json:"context"`
}

ErrorInfo is an upload error info response.

type Image

type Image struct {
	// contains filtered or unexported fields
}

Image is a struct with image data to upload.

func NewImage

func NewImage(name string, ttl uint64, source string) (*Image, error)

NewImage creates a new Image.

func NewImageFromFile added in v1.0.1

func NewImageFromFile(name string, ttl uint64, file []byte) (*Image, error)

NewImageFromFile creates a new Image from file.

type Info

type Info struct {
	Filename  string `json:"filename"`
	Name      string `json:"name"`
	Mime      string `json:"mime"`
	Extension string `json:"extension"`
	URL       string `json:"url"`
}

Info is an additional info about uploaded file.

type Response

type Response struct {
	Data       Data `json:"data"`
	StatusCode int  `json:"status"`
	Success    bool `json:"success"`
}

Response is an upload success response.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL