From 4d57947f836f0787d146a5e03a0168d8a625dfd9 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 3 Aug 2020 19:21:22 +0100 Subject: [PATCH] moved upload functionality to its own package Signed-off-by: Michael --- README.md | 6 ++-- go.mod | 2 +- go.sum | 4 +-- main.go | 68 ++-------------------------------------------- upload/upload.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 72 deletions(-) create mode 100644 upload/upload.go diff --git a/README.md b/README.md index bab9abe..229c5fe 100644 --- a/README.md +++ b/README.md @@ -39,10 +39,10 @@ and can be ran with: ### As a go module -The upload function has been exported so it can be used by other go applications. With a correctly configured golang toolchain run: +The upload function has been exported in its own package so it can be used by other go applications. With a correctly configured golang toolchain run: ```sh -go get github.com/michaellindman/discoupload +go get github.com/michaellindman/discoupload/upload ``` #### Example @@ -55,7 +55,7 @@ var ( url = "" ) -upload, err := Upload(key, username, url, "path/to/upload/file") +upload, err := upload.Upload(key, username, url, "path/to/upload/file") if err != nil { log.Println(err) } diff --git a/go.mod b/go.mod index a707011..088bb39 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ module github.com/michaellindman/discoupload go 1.14 require ( - github.com/michaellindman/request v0.0.5 + git.0cd.xyz/michael/request v0.0.5 github.com/pkg/errors v0.9.1 gopkg.in/yaml.v2 v2.3.0 ) diff --git a/go.sum b/go.sum index e68c795..3bfc724 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,5 @@ -github.com/michaellindman/request v0.0.5 h1:X+Ir1rZ1QieG8E2jj05aey6QSrqC5N1+pqDSxMEkMA0= -github.com/michaellindman/request v0.0.5/go.mod h1:h4Iti/hHTMj52wDqq6y24u8ROiTZkcc6Hy2tnM0mq8o= +git.0cd.xyz/michael/request v0.0.5 h1:Vbyyb/9PwIeIES6hpfbDd2uuWGpTBxH7M1UOqTeBPRg= +git.0cd.xyz/michael/request v0.0.5/go.mod h1:oyVCWzVMlGOfQal6KsDwvWIsKeszVsOTT6JTRK9qAqo= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/main.go b/main.go index 8631d7d..c794db4 100644 --- a/main.go +++ b/main.go @@ -1,18 +1,13 @@ package main import ( - "bytes" - "encoding/json" "flag" "fmt" - "io/ioutil" "log" - "mime/multipart" - "net/http" "os" "path/filepath" - "github.com/michaellindman/request" + "github.com/michaellindman/discoupload/upload" "github.com/pkg/errors" ) @@ -38,7 +33,7 @@ func run() error { file := flag.String("file", "", "path for file to be uploaded") flag.Parse() if *file != "" { - upload, err := Upload(cfg.API.Key, cfg.API.Username, cfg.API.URL, *file) + upload, err := upload.Upload(cfg.API.Key, cfg.API.Username, cfg.API.URL, *file) if err != nil { return errors.Wrap(err, "upload") } @@ -48,62 +43,3 @@ func run() error { flag.PrintDefaults() return nil } - -// Upload file to discourse server -func Upload(key, username, url, filepath string) (response map[string]interface{}, err error) { - params := map[string]string{ - "type": "upload", - "synchronous": "true", - } - - file, err := os.Open(filepath) - if err != nil { - return nil, err - } - contents, err := ioutil.ReadAll(file) - if err != nil { - file.Close() - return nil, err - } - stat, err := file.Stat() - if err != nil { - file.Close() - return nil, err - } - err = file.Close() - if err != nil { - return nil, err - } - - body := &bytes.Buffer{} - writer := multipart.NewWriter(body) - part, err := writer.CreateFormFile("file", stat.Name()) - part.Write(contents) - - for key, val := range params { - writer.WriteField(key, val) - } - - err = writer.Close() - if err != nil { - return nil, err - } - - headers := map[string]string{ - "Api-Key": key, - "Api-Username": username, - "Content-Type": writer.FormDataContentType(), - } - - resp, err := request.API(http.MethodPost, url+"/uploads", headers, body) - if err != nil { - return nil, err - } - - err = json.Unmarshal(resp.Body, &response) - if err != nil { - return nil, err - } - - return response, nil -} diff --git a/upload/upload.go b/upload/upload.go new file mode 100644 index 0000000..776c9cd --- /dev/null +++ b/upload/upload.go @@ -0,0 +1,71 @@ +package upload + +import ( + "bytes" + "encoding/json" + "io/ioutil" + "mime/multipart" + "net/http" + "os" + + "git.0cd.xyz/michael/request" +) + +// Upload file to discourse server +func Upload(key, username, url, filepath string) (response map[string]interface{}, err error) { + params := map[string]string{ + "type": "upload", + "synchronous": "true", + } + + file, err := os.Open(filepath) + if err != nil { + return nil, err + } + contents, err := ioutil.ReadAll(file) + if err != nil { + file.Close() + return nil, err + } + stat, err := file.Stat() + if err != nil { + file.Close() + return nil, err + } + err = file.Close() + if err != nil { + return nil, err + } + + body := &bytes.Buffer{} + writer := multipart.NewWriter(body) + part, err := writer.CreateFormFile("file", stat.Name()) + part.Write(contents) + + for key, val := range params { + writer.WriteField(key, val) + } + + err = writer.Close() + if err != nil { + return nil, err + } + + headers := map[string]string{ + "Api-Key": key, + "Api-Username": username, + "Content-Type": writer.FormDataContentType(), + } + + resp, err := request.API(http.MethodPost, url+"/uploads", headers, body) + if err != nil { + return nil, err + } + + err = json.Unmarshal(resp.Body, &response) + if err != nil { + return nil, err + } + + return response, nil +}