mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-20 05:52:25 +03:00
55335c4061
Squashed commit of the following: commit 874e847fc9bbfaeb8af1c02eb0ba1dbb98bd008f Merge: 4becdd809a79deda66
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jul 12 16:01:45 2023 +0300 Merge branch 'master' into AG-23497-scripts-download-languages commit 4becdd8092558b15d783674f5b9d1e9c151e3a8c Merge: 1e5385c3340884624c
Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jul 12 13:34:34 2023 +0300 Merge branch 'master' into AG-23497-scripts-download-languages commit 1e5385c33a298b0b8563fee6704f6bb3ded12d60 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Tue Jul 11 19:56:29 2023 +0300 all: upd golibs, imp code commit 0498960b00be21b1294f8b71108b234554e5847f Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Fri Jul 7 19:05:58 2023 +0300 scripts: imp naming commit 6e36ed83c6bec2fe6159442a9e6805c0720e27f5 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Thu Jul 6 16:37:13 2023 +0300 scripts: separate files commit 55027cfa1c04b0a36e5267b024b53a45f26dd974 Author: Stanislav Chzhen <s.chzhen@adguard.com> Date: Wed Jul 5 13:51:40 2023 +0300 scripts: add download languages
120 lines
2.6 KiB
Go
120 lines
2.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"net/textproto"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghhttp"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/httphdr"
|
|
"github.com/AdguardTeam/golibs/mapsutil"
|
|
)
|
|
|
|
// upload base translation.
|
|
func (c *twoskyClient) upload() (err error) {
|
|
defer func() { err = errors.Annotate(err, "upload: %w") }()
|
|
|
|
uploadURI := c.uri.JoinPath("upload")
|
|
basePath := filepath.Join(localesDir, defaultBaseFile)
|
|
|
|
formData := map[string]string{
|
|
"format": "json",
|
|
"language": string(c.baseLang),
|
|
"filename": defaultBaseFile,
|
|
"project": c.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
|
|
|
|
err = mapsutil.OrderedRangeError(formData, w.WriteField)
|
|
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(httphdr.ContentType, aghhttp.HdrValApplicationJSON)
|
|
|
|
d := fmt.Sprintf("form-data; name=%q; filename=%q", "file", defaultBaseFile)
|
|
h.Set(httphdr.ContentDisposition, 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 nil, "", fmt.Errorf("copying: %w", err)
|
|
}
|
|
|
|
err = w.Close()
|
|
if err != nil {
|
|
return nil, "", fmt.Errorf("closing writer: %w", err)
|
|
}
|
|
|
|
return buf, w.FormDataContentType(), nil
|
|
}
|
|
|
|
// send POST request to uriStr.
|
|
func send(uriStr, cType string, buf *bytes.Buffer) (err error) {
|
|
client := http.Client{
|
|
Timeout: uploadTimeout,
|
|
}
|
|
|
|
req, err := http.NewRequest(http.MethodPost, uriStr, buf)
|
|
if err != nil {
|
|
return fmt.Errorf("bad request: %w", err)
|
|
}
|
|
|
|
req.Header.Set(httphdr.ContentType, cType)
|
|
|
|
resp, err := client.Do(req)
|
|
if err != nil {
|
|
return fmt.Errorf("client post form: %w", err)
|
|
}
|
|
|
|
defer func() {
|
|
err = errors.WithDeferred(err, resp.Body.Close())
|
|
}()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("status code is not ok: %q", http.StatusText(resp.StatusCode))
|
|
}
|
|
|
|
return nil
|
|
}
|