NullpointerW / go-qbittorrent-apiv2

qBittorrent web API(v2.8.3) wrapper for go
MIT License
3 stars 4 forks source link

multipart: can't write to finished part (tested on qB4.6.4) #5

Open ludoux opened 2 weeks ago

ludoux commented 2 weeks ago

It seems that it should copy just after createFormFile in the postMultipartFile function, otherwise It will raise multipart: can't write to finished part error.

https://github.com/NullpointerW/go-qbittorrent-apiv2/blob/9b515d3fcfaf4f8e951a48d100d055cbda590f6c/cli.go#L144-L155

I change the code to the below to make it work.

    formWriter, err := writer.CreateFormFile("torrents", path.Base(fileName))
    if err != nil {
        return nil, fmt.Errorf("error adding file: %w", err)
    }

    // copy the file first or there will be "multipart: can't write to finished part" error
    // copy the file contents into the form
    if _, err = io.Copy(formWriter, file); err != nil {
        return nil, fmt.Errorf("error copying file: %w", err)
    }

    // write the options to the buffer
    writeOptions(writer, opts)
func (c *Client) AddNewTorrentWithFile(filePath string, opt Optional) error {
    resp, err := c.postMultipartFile("torrents/add", filePath, opt)
    err = RespOk(resp, err)
    if err != nil {
        return err
    }
    if err = RespBodyOk(resp.Body, ErrAddTorrnetfailed); err != nil {
        return err
    }
    return nil
}