|
|
|
@ -1,44 +1,81 @@ |
|
|
|
|
package jschan |
|
|
|
|
|
|
|
|
|
import ( |
|
|
|
|
"bytes" |
|
|
|
|
"context" |
|
|
|
|
"fmt" |
|
|
|
|
"io" |
|
|
|
|
"mime/multipart" |
|
|
|
|
"net/http" |
|
|
|
|
"net/url" |
|
|
|
|
"strings" |
|
|
|
|
"net/textproto" |
|
|
|
|
"os" |
|
|
|
|
"path" |
|
|
|
|
"strconv" |
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
type PostMakePostOptions struct { |
|
|
|
|
Board string |
|
|
|
|
Thread int |
|
|
|
|
Name string |
|
|
|
|
Message string |
|
|
|
|
Subject string |
|
|
|
|
Email string |
|
|
|
|
PostPassword string |
|
|
|
|
//TODO: Files
|
|
|
|
|
type MakePostOptions struct { |
|
|
|
|
Board string |
|
|
|
|
Thread int |
|
|
|
|
Name string |
|
|
|
|
Message string |
|
|
|
|
Subject string |
|
|
|
|
Email string |
|
|
|
|
PostPassword string |
|
|
|
|
Files []string //Array of filenames
|
|
|
|
|
Spoiler []string |
|
|
|
|
SpoilerAll bool |
|
|
|
|
StripFilename []string |
|
|
|
|
CustomFlag string |
|
|
|
|
//Array for grid captcha, submitted as single param if len()==1
|
|
|
|
|
Captcha []string |
|
|
|
|
Captcha []string //Array for grid captcha, submitted as single param if len()==1
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (c *Client) MakePost(ctx context.Context, options *PostMakePostOptions) error { |
|
|
|
|
func (c *Client) MakePost(ctx context.Context, options *MakePostOptions) error { |
|
|
|
|
|
|
|
|
|
formData := url.Values{} |
|
|
|
|
//TODO: post params
|
|
|
|
|
|
|
|
|
|
endodedBody := strings.NewReader(formData.Encode()) |
|
|
|
|
body := &bytes.Buffer{} |
|
|
|
|
writer := multipart.NewWriter(body) |
|
|
|
|
if options.Files != nil && len(options.Files) > 0 { |
|
|
|
|
for _, filepath := range options.Files { |
|
|
|
|
dir, fileName := path.Split(filepath) |
|
|
|
|
filePath := path.Join(dir, fileName) |
|
|
|
|
file, _ := os.Open(filePath) |
|
|
|
|
defer file.Close() |
|
|
|
|
h := make(textproto.MIMEHeader) |
|
|
|
|
h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileName)) |
|
|
|
|
h.Set("Content-Type", "image/png") |
|
|
|
|
part, _ := writer.CreatePart(h) |
|
|
|
|
//part, _ := writer.CreateFormFile("file", h)
|
|
|
|
|
io.Copy(part, file) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
_ = writer.WriteField("thread", strconv.Itoa(options.Thread)) |
|
|
|
|
_ = writer.WriteField("name", options.Name) |
|
|
|
|
_ = writer.WriteField("message", options.Message) |
|
|
|
|
_ = writer.WriteField("subject", options.Subject) |
|
|
|
|
_ = writer.WriteField("email", options.Email) |
|
|
|
|
_ = writer.WriteField("postpassword", options.PostPassword) |
|
|
|
|
_ = writer.WriteField("customflag", options.CustomFlag) |
|
|
|
|
if options.SpoilerAll == true { |
|
|
|
|
_ = writer.WriteField("spoiler_all", "true") |
|
|
|
|
} |
|
|
|
|
for _, filename := range options.Spoiler { |
|
|
|
|
_ = writer.WriteField("spoiler", filename) |
|
|
|
|
} |
|
|
|
|
for _, filename := range options.StripFilename { |
|
|
|
|
_ = writer.WriteField("strip_filename", filename) |
|
|
|
|
} |
|
|
|
|
for _, answer := range options.Captcha { |
|
|
|
|
_ = writer.WriteField("captcha", answer) |
|
|
|
|
} |
|
|
|
|
writer.Close() |
|
|
|
|
|
|
|
|
|
url := fmt.Sprintf("%s/forms/%s/post", c.BaseURL, options.Board) |
|
|
|
|
url := fmt.Sprintf("%s/forms/board/%s/post", c.BaseURL, options.Board) |
|
|
|
|
|
|
|
|
|
req, err := http.NewRequest(http.MethodPost, url, endodedBody) |
|
|
|
|
req, err := http.NewRequest(http.MethodPost, url, body) |
|
|
|
|
if err != nil { |
|
|
|
|
return err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// req.Header.Set("content-type", "multipart/form-data")
|
|
|
|
|
req.Header.Add("Content-Type", writer.FormDataContentType()) |
|
|
|
|
req = req.WithContext(ctx) |
|
|
|
|
|
|
|
|
|
if err := c.sendRequest(req, nil, nil); err != nil { |
|
|
|
|