mirror of
https://github.com/michaellindman/discoupload.git
synced 2024-12-18 10:02:30 +00:00
moved upload functionality to its own package
Signed-off-by: Michael <michael.lindman@gmail.com>
This commit is contained in:
parent
f7b0a0a63a
commit
4d57947f83
@ -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 = "<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 {
|
||||
log.Println(err)
|
||||
}
|
||||
|
2
go.mod
2
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
|
||||
)
|
||||
|
4
go.sum
4
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=
|
||||
|
68
main.go
68
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
|
||||
}
|
||||
|
71
upload/upload.go
Normal file
71
upload/upload.go
Normal 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
|
||||
}
|
Loading…
Reference in New Issue
Block a user