Skip to content

Commit

Permalink
Pull request 1789: AG-20200-translation-script-fix-upload
Browse files Browse the repository at this point in the history
Merge in DNS/adguard-home from AG-20200-translation-script-fix-upload to master

Squashed commit of the following:

commit 4d89892
Merge: 41ad204 487675b
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Mar 28 13:30:36 2023 +0300

    Merge branch 'master' into AG-20200-translation-script-fix-upload

commit 41ad204
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Mar 28 13:29:26 2023 +0300

    scripts: imp more

commit 5ea4821
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Mar 28 12:38:22 2023 +0300

    scripts: imp code

commit 253a72f
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Mar 28 12:04:16 2023 +0300

    scripts: add docs

commit bfd70e3
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Mar 28 10:56:05 2023 +0300

    scripts: imp more

commit 547b827
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Tue Mar 28 10:08:16 2023 +0300

    scripts: imp code

commit a788e5e
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Mar 27 20:14:55 2023 +0300

    scripts: add multipart

commit 39d352e
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Mar 27 18:35:08 2023 +0300

    scripts: fix more

commit bcbf155
Author: Stanislav Chzhen <s.chzhen@adguard.com>
Date:   Mon Mar 27 17:47:40 2023 +0300

    scripts: fix upload
  • Loading branch information
schzhn committed Mar 28, 2023
1 parent 487675b commit c576d50
Showing 1 changed file with 81 additions and 7 deletions.
88 changes: 81 additions & 7 deletions scripts/translations/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import (
"flag"
"fmt"
"io"
"mime/multipart"
"net/http"
"net/textproto"
"net/url"
"os"
"path/filepath"
Expand Down Expand Up @@ -426,6 +428,8 @@ func printUnused(loc locales) {
// upload base translation. uri is the base URL. projectID is the name of the
// project. baseLang is the base language code.
func upload(uri *url.URL, projectID string, baseLang langCode) (err error) {
defer func() { err = errors.Annotate(err, "upload: %w") }()

uploadURI := uri.JoinPath("upload")

lang := baseLang
Expand All @@ -436,20 +440,90 @@ 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,
}

buf, cType, err := prepareMultipartMsg(formData, basePath)
if err != nil {
return fmt.Errorf("preparing multipart msg: %w", err)
}

err = send(uploadURI.String(), cType, buf)
if err != nil {
return fmt.Errorf("sending multipart msg: %w", err)
}

return nil
}

// prepareMultipartMsg prepares translation data for upload.
func prepareMultipartMsg(
formData map[string]string,
basePath string,
) (buf *bytes.Buffer, cType string, err error) {
buf = &bytes.Buffer{}
w := multipart.NewWriter(buf)
var fw io.Writer

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

file, err := os.Open(basePath)
if err != nil {
return nil, "", fmt.Errorf("opening file: %w", err)
}

defer func() {
err = errors.WithDeferred(err, file.Close())
}()

h := make(textproto.MIMEHeader)
h.Set("Content-Type", "application/json")

d := fmt.Sprintf("form-data; name=%q; filename=%q", "file", defaultBaseFile)
h.Set("Content-Disposition", d)

fw, err = w.CreatePart(h)
if err != nil {
return nil, "", fmt.Errorf("creating part: %w", err)
}

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

var buf bytes.Buffer
buf.Write(b)
err = w.Close()
if err != nil {
return nil, "", fmt.Errorf("closing writer: %w", err)
}

uri = translationURL(uploadURI, defaultBaseFile, projectID, lang)
return buf, w.FormDataContentType(), nil
}

// send POST request to uriStr.
func send(uriStr, cType string, buf *bytes.Buffer) (err error) {
var client http.Client
resp, err := client.Post(uri.String(), "application/json", &buf)

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

req.Header.Set("Content-Type", cType)

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

defer func() {
Expand Down

0 comments on commit c576d50

Please sign in to comment.