1
0
mirror of https://github.com/michaellindman/discoupload.git synced 2024-12-18 19:04:29 +00:00

moved upload functionality to its own package

Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
Michael 2020-08-03 19:21:22 +01:00
parent f7b0a0a63a
commit 4d57947f83
5 changed files with 79 additions and 72 deletions

View File

@ -39,10 +39,10 @@ and can be ran with:
### As a go module ### 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 ```sh
go get github.com/michaellindman/discoupload go get github.com/michaellindman/discoupload/upload
``` ```
#### Example #### Example
@ -55,7 +55,7 @@ var (
url = "<forum url>" url = "<forum 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 { if err != nil {
log.Println(err) log.Println(err)
} }

2
go.mod
View File

@ -3,7 +3,7 @@ module github.com/michaellindman/discoupload
go 1.14 go 1.14
require ( require (
github.com/michaellindman/request v0.0.5 git.0cd.xyz/michael/request v0.0.5
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
gopkg.in/yaml.v2 v2.3.0 gopkg.in/yaml.v2 v2.3.0
) )

4
go.sum
View File

@ -1,5 +1,5 @@
github.com/michaellindman/request v0.0.5 h1:X+Ir1rZ1QieG8E2jj05aey6QSrqC5N1+pqDSxMEkMA0= git.0cd.xyz/michael/request v0.0.5 h1:Vbyyb/9PwIeIES6hpfbDd2uuWGpTBxH7M1UOqTeBPRg=
github.com/michaellindman/request v0.0.5/go.mod h1:h4Iti/hHTMj52wDqq6y24u8ROiTZkcc6Hy2tnM0mq8o= 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 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

68
main.go
View File

@ -1,18 +1,13 @@
package main package main
import ( import (
"bytes"
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"mime/multipart"
"net/http"
"os" "os"
"path/filepath" "path/filepath"
"github.com/michaellindman/request" "github.com/michaellindman/discoupload/upload"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -38,7 +33,7 @@ func run() error {
file := flag.String("file", "", "path for file to be uploaded") file := flag.String("file", "", "path for file to be uploaded")
flag.Parse() flag.Parse()
if *file != "" { 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 { if err != nil {
return errors.Wrap(err, "upload") return errors.Wrap(err, "upload")
} }
@ -48,62 +43,3 @@ func run() error {
flag.PrintDefaults() flag.PrintDefaults()
return nil 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
}

71
upload/upload.go Normal file
View File

@ -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
}