From 3dd584b2cd7551140ab734b09aaa7979a4de6182 Mon Sep 17 00:00:00 2001 From: Ivan Nepomniashchikh Date: Sun, 20 Aug 2023 12:21:03 +0700 Subject: [PATCH] add NewImageFromFile --- README.md | 2 +- imgbb.go | 55 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 3598467..23dbe8a 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ func main() { log.Fatal(err) } - img, err := imgBB.NewImage(hashSum(b), 60, b) + img, err := imgBB.NewImageFromFile(hashSum(b), 60, b) if err != nil { log.Fatal(err) } diff --git a/imgbb.go b/imgbb.go index 77ce85e..59e6c33 100644 --- a/imgbb.go +++ b/imgbb.go @@ -31,14 +31,31 @@ var ( // Image is a struct with image data to upload. type Image struct { - name string - size int - ttl uint64 - file []byte + name string + size int + ttl uint64 + source string + file []byte } // NewImage creates a new Image. -func NewImage(name string, ttl uint64, file []byte) (*Image, error) { +func NewImage(name string, ttl uint64, source string) (*Image, error) { + size := len(source) + + if size <= 0 { + return nil, ErrFileEmpty + } + + return &Image{ + name: name, + size: size, + ttl: ttl, + source: source, + }, nil +} + +// NewImageFromFile creates a new Image from file. +func NewImageFromFile(name string, ttl uint64, file []byte) (*Image, error) { size := len(file) if size <= 0 { @@ -156,16 +173,6 @@ func (i *Client) prepareRequest(ctx context.Context, img *Image) (*http.Request, return } - err = mpWriter.WriteField("type", "file") - if err != nil { - return - } - - err = mpWriter.WriteField("action", "upload") - if err != nil { - return - } - if img.ttl > 0 { err = mpWriter.WriteField("expiration", strconv.FormatUint(img.ttl, 10)) if err != nil { @@ -173,12 +180,26 @@ func (i *Client) prepareRequest(ctx context.Context, img *Image) (*http.Request, } } - part, err := mpWriter.CreateFormFile("image", img.name) + if img.file != nil { + part, err := mpWriter.CreateFormFile("image", img.name) + if err != nil { + return + } + + if _, err = io.Copy(part, bytes.NewReader(img.file)); err != nil { + return + } + + return + } + + err = mpWriter.WriteField("name", img.name) if err != nil { return } - if _, err = io.Copy(part, bytes.NewReader(img.file)); err != nil { + err = mpWriter.WriteField("image", img.source) + if err != nil { return } }()