...

Source file src/jschan/app/post.go

Documentation: jschan/app

     1  package jschan
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"io"
     8  	"mime/multipart"
     9  	"net/http"
    10  	"net/textproto"
    11  	"os"
    12  	"path"
    13  	"strconv"
    14  )
    15  
    16  type MakePostOptions struct {
    17  	Board         string
    18  	Thread        int
    19  	Name          string
    20  	Message       string
    21  	Subject       string
    22  	Email         string
    23  	PostPassword  string
    24  	Files         []string
    25  	Spoiler       []string
    26  	SpoilerAll    bool
    27  	StripFilename []string
    28  	CustomFlag    string
    29  	Captcha       []string
    30  	Mod           bool
    31  }
    32  
    33  func (c *Client) MakePost(ctx context.Context, options *MakePostOptions) error {
    34  
    35  	body := &bytes.Buffer{}
    36  	writer := multipart.NewWriter(body)
    37  	if options.Files != nil && len(options.Files) > 0 {
    38  		for _, filepath := range options.Files {
    39  			dir, fileName := path.Split(filepath)
    40  			filePath := path.Join(dir, fileName)
    41  			file, _ := os.Open(filePath)
    42  			defer file.Close()
    43  			h := make(textproto.MIMEHeader)
    44  			h.Set("Content-Disposition", fmt.Sprintf(`form-data; name="file"; filename="%s"`, fileName))
    45  			h.Set("Content-Type", "image/png")
    46  			part, _ := writer.CreatePart(h)
    47  			//part, _ := writer.CreateFormFile("file", h)
    48  			io.Copy(part, file)
    49  		}
    50  	}
    51  	_ = writer.WriteField("thread", strconv.Itoa(options.Thread))
    52  	_ = writer.WriteField("name", options.Name)
    53  	_ = writer.WriteField("message", options.Message)
    54  	_ = writer.WriteField("subject", options.Subject)
    55  	_ = writer.WriteField("email", options.Email)
    56  	_ = writer.WriteField("postpassword", options.PostPassword)
    57  	_ = writer.WriteField("customflag", options.CustomFlag)
    58  	if options.SpoilerAll == true {
    59  		_ = writer.WriteField("spoiler_all", "true")
    60  	}
    61  	for _, filename := range options.Spoiler {
    62  		_ = writer.WriteField("spoiler", filename)
    63  	}
    64  	for _, filename := range options.StripFilename {
    65  		_ = writer.WriteField("strip_filename", filename)
    66  	}
    67  	for _, answer := range options.Captcha {
    68  		_ = writer.WriteField("captcha", answer)
    69  	}
    70  	writer.Close()
    71  
    72  	suffix := "post"
    73  	if options.Mod {
    74  		suffix = "modpost"
    75  	}
    76  	url := fmt.Sprintf("%s/forms/board/%s/%s", c.BaseURL, options.Board, suffix)
    77  
    78  	req, err := http.NewRequest(http.MethodPost, url, body)
    79  	if err != nil {
    80  		return err
    81  	}
    82  	//	req.Header.Set("content-type", "multipart/form-data")
    83  	req.Header.Add("Content-Type", writer.FormDataContentType())
    84  	req = req.WithContext(ctx)
    85  
    86  	if err := c.sendRequest(req, nil, nil); err != nil {
    87  		return err
    88  	}
    89  
    90  	return nil
    91  
    92  }
    93  
    94  type SubmitPostActionsOptions struct {
    95  	CheckedPosts      []string
    96  	CheckedReports    []string
    97  	Board             string
    98  	Captcha           []string
    99  	PostPassword      string
   100  	Ban               bool
   101  	BanGlobal         bool
   102  	BanHalfRange      bool
   103  	BanQuarterRance   bool
   104  	BanReporter       bool
   105  	BanReporterGlobal bool
   106  	BanReason         string
   107  	BanDuration       string
   108  	Move              bool
   109  	MoveToThread      int
   110  	Sticky            int
   111  	ToggleCyclic      bool
   112  	ToggleBumplock    bool
   113  	ToggleLock        bool
   114  	Spoiler           bool
   115  	Delete            bool
   116  	DeleteFiles       bool
   117  	UnlinkFiles       bool
   118  	DeleteIPThread    bool
   119  	DeleteIPBoard     bool
   120  	DeleteIPGlobal    bool
   121  	Dismiss           bool
   122  	DismissGlobal     bool
   123  	Report            bool
   124  	ReportGlobal      bool
   125  	ReportReason      string
   126  	HideName          bool
   127  	NoAppeal          bool
   128  	PreservePost      bool
   129  	LogMessage        string
   130  }
   131  
   132  func (c *Client) SubmitPostActions(ctx context.Context, options *MakePostOptions) error {
   133  
   134  	return nil
   135  
   136  }
   137  

View as plain text