Skip to content

Commit

Permalink
Merge pull request #19 from l0wl3vel/io-rework
Browse files Browse the repository at this point in the history
Revert "feat: Add UploadFromReader function to allow uploading from a…
  • Loading branch information
l0wl3vel authored Jan 26, 2024
2 parents 8f34b40 + 6b71eed commit 7a6652b
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions client.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package bunnystorage

import (
"bytes"
"encoding/hex"
"errors"
"fmt"
Expand Down Expand Up @@ -48,29 +47,18 @@ func (c *Client) WithLogger(l resty.Logger) *Client {

// Uploads a file to the relative path. generateChecksum controls if a checksum gets generated and attached to the upload request. Returns an error.
func (c *Client) Upload(path string, content []byte, generateChecksum bool) error {
return c.UploadFromReader(path, bytes.NewReader(content), generateChecksum)
}

// Uploads a file to the relative path. generateChecksum controls if a checksum gets generated and attached to the upload request. Returns an error. Allows passing a reader instead of a []byte
func (c *Client) UploadFromReader(path string, content io.Reader, generateChecksum bool) error {
req := c.R().
SetHeader("Content-Type", "application/octet-stream")
SetHeader("Content-Type", "application/octet-stream").
SetBody(content)

if generateChecksum {
checksum := sha256.New()
content_slice, readErr := io.ReadAll(content)
if readErr != nil {
return readErr
}
_, err := checksum.Write(content_slice)
_, err := checksum.Write(content)
if err != nil {
return err
}
hex_checksum := hex.EncodeToString(checksum.Sum(nil))
req = req.SetHeader("Checksum", hex_checksum).SetBody(content_slice)
} else {
// Use Resty bufferless codepath if checksum is disabled
req = req.SetBody(content)
req = req.SetHeader("Checksum", hex_checksum)
}

resp, err := req.Put(path)
Expand Down

0 comments on commit 7a6652b

Please sign in to comment.