Skip to content

Commit

Permalink
scripts: add multipart
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Mar 27, 2023
1 parent 39d352e commit a788e5e
Showing 1 changed file with 52 additions and 11 deletions.
63 changes: 52 additions & 11 deletions scripts/translations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"flag"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/url"
"os"
Expand Down Expand Up @@ -438,23 +439,63 @@ func upload(uri *url.URL, projectID string, baseLang langCode) (err error) {
}

basePath := filepath.Join(localesDir, defaultBaseFile)
b, err := os.ReadFile(basePath)

formData := map[string]string{
"format": "json",
"language": string(lang),
"filename": defaultBaseFile,
"project": projectID,
}

return uploadMultiPart(uploadURI, formData, basePath)
}

func uploadMultiPart(uri *url.URL, formData map[string]string, basePath string) (err error) {
defer func() { err = errors.Annotate(err, "upload multipart: %w") }()

buf := &bytes.Buffer{}
w := multipart.NewWriter(buf)
var fw io.Writer

for k, v := range formData {
fw, err = w.CreateFormField(k)
if err != nil {
return fmt.Errorf("creating form field: %w", err)
}

_, err = fw.Write([]byte(v))
if err != nil {
return fmt.Errorf("multipart writing: %w", err)
}
}

file, err := os.Open(basePath)
if err != nil {
// Don't wrap the error since it's informative enough as is and there
// is an annotation deferred already.
return err
return fmt.Errorf("opening file: %w", err)
}

fw, err = w.CreateFormFile("file", basePath)
_, err = io.Copy(fw, file)
if err != nil {
return fmt.Errorf("copying: %w", err)
}

formData := url.Values{
"format": {"json"},
"language": {string(lang)},
"filename": {defaultBaseFile},
"project": {projectID},
"file": {string(b)},
err = w.Close()
if err != nil {
return fmt.Errorf("closing multipart writer: %w", err)
}

var client http.Client
resp, err := client.PostForm(uploadURI.String(), formData)

req, err := http.NewRequest(http.MethodPost, uri.String(), buf)
if err != nil {
return fmt.Errorf("bad request: %w", err)
}

req.Header.Add("Content-Type", w.FormDataContentType())
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

resp, err := client.Do(req)
if err != nil {
return fmt.Errorf("client post form: %w", err)
}
Expand Down

0 comments on commit a788e5e

Please sign in to comment.