Skip to content

Commit

Permalink
Added a Raw File Upload method (#3)
Browse files Browse the repository at this point in the history
* feat: added a rawfileuplaod method

* refactor: moved into one method

* Set `MallocNanoZone=0` env var to fix actions

GitHub actions due to a SIGABRT received on OSX jobs, caused by: golang/go#49138

---------

Co-authored-by: Wayback Archiver <[email protected]>
  • Loading branch information
Lioncat2002 and waybackarchiver authored May 31, 2023
1 parent c8fb98a commit 7b0cf5b
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ on:
- "go.sum"
- ".github/workflows/testing.yml"

env:
MallocNanoZone: 0

jobs:
test:
name: Testing
Expand Down
74 changes: 61 additions & 13 deletions catbox.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,71 @@ func New(client *http.Client) *Catbox {
}

// Upload file or URI to the Catbox. It returns an URL string and error.
func (cat *Catbox) Upload(path string) (string, error) {
parse := func(s string, _ error) (string, error) {
uri, err := url.Parse(s)
func (cat *Catbox) Upload(v ...interface{}) (string, error) {
if len(v) == 0 {
return "", fmt.Errorf(`must specify file path or byte slice`)
}

switch t := v[0].(type) {
case string:
path := t
parse := func(s string, _ error) (string, error) {
uri, err := url.Parse(s)
if err != nil {
return "", err
}
return uri.String(), nil
}
switch {
case helper.IsURL(path):
return parse(cat.urlUpload(path))
case helper.Exists(path):
return parse(cat.fileUpload(path))
default:
return "", errors.New(`path invalid`)
}
case []byte:
if len(v) != 2 {
return "", fmt.Errorf(`must specify file name`)
}
return cat.rawUpload(t, v[1].(string))
}
return "", fmt.Errorf(`unhandled`)
}

func (cat *Catbox) rawUpload(b []byte, name string) (string, error) {
r, w := io.Pipe()
m := multipart.NewWriter(w)

go func() {
defer w.Close()
defer m.Close()

m.WriteField("reqtype", "fileupload")
m.WriteField("userhash", cat.Userhash)
part, err := m.CreateFormFile("fileToUpload", filepath.Base(name))
if err != nil {
return "", err
return
}
if _, err = io.Copy(part, bytes.NewBuffer(b)); err != nil {
return
}
return uri.String(), nil
}()
req, _ := http.NewRequest(http.MethodPost, ENDPOINT, r)
req.Header.Add("Content-Type", m.FormDataContentType())

resp, err := cat.Client.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()

switch {
case helper.IsURL(path):
return parse(cat.urlUpload(path))
case helper.Exists(path):
return parse(cat.fileUpload(path))
default:
return "", errors.New(`path invalid`)
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}

return string(body), nil
}

func (cat *Catbox) fileUpload(path string) (string, error) {
Expand All @@ -69,7 +117,7 @@ func (cat *Catbox) fileUpload(path string) (string, error) {
defer file.Close()

if size := helper.FileSize(path); size > 209715200 {
return "", fmt.Errorf("File too large, size: %d MB", size/1024/1024)
return "", fmt.Errorf("file too large, size: %d MB", size/1024/1024)
}

r, w := io.Pipe()
Expand Down
22 changes: 22 additions & 0 deletions catbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,28 @@ func TestFileUpload(t *testing.T) {
}
}

func TestRawUpload(t *testing.T) {
content := make([]byte, 5000)
tmpfile, err := ioutil.TempFile("", "go-catbox-")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name())
b, err := ioutil.ReadFile(tmpfile.Name())

if err != nil {
t.Fatal(err)
}

if _, err := tmpfile.Write(content); err != nil {
t.Fatal(err)
}

if _, err := New(nil).Upload(b, "test"); err != nil {
t.Fatal(err)
}
}

func TestURLUpload(t *testing.T) {
url := "https://www.gstatic.com/webp/gallery/1.webp"
if _, err := New(nil).Upload(url); err != nil {
Expand Down

0 comments on commit 7b0cf5b

Please sign in to comment.