2020-12-17 14:44:04 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2020-12-17 14:44:04 +01:00
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2020-12-17 14:44:04 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2020-12-17 14:44:04 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2020-12-17 14:44:04 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package migration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"net/http"
|
2020-12-18 12:12:05 +01:00
|
|
|
"net/url"
|
|
|
|
"strings"
|
2020-12-17 14:44:04 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// DownloadFile downloads a file and returns its contents
|
|
|
|
func DownloadFile(url string) (buf *bytes.Buffer, err error) {
|
2021-11-14 21:47:51 +01:00
|
|
|
return DownloadFileWithHeaders(url, nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DownloadFileWithHeaders downloads a file and allows you to pass in headers
|
|
|
|
func DownloadFileWithHeaders(url string, headers http.Header) (buf *bytes.Buffer, err error) {
|
2020-12-17 14:44:04 +01:00
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, url, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-14 21:47:51 +01:00
|
|
|
|
2021-11-16 20:37:37 +01:00
|
|
|
for key, h := range headers {
|
|
|
|
for _, hh := range h {
|
|
|
|
req.Header.Add(key, hh)
|
2021-11-14 21:47:51 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-17 14:44:04 +01:00
|
|
|
hc := http.Client{}
|
|
|
|
resp, err := hc.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
buf = &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(resp.Body)
|
2021-11-14 21:47:51 +01:00
|
|
|
|
2020-12-17 14:44:04 +01:00
|
|
|
return
|
|
|
|
}
|
2020-12-18 12:12:05 +01:00
|
|
|
|
|
|
|
// DoPost makes a form encoded post request
|
|
|
|
func DoPost(url string, form url.Values) (resp *http.Response, err error) {
|
|
|
|
req, err := http.NewRequestWithContext(context.Background(), http.MethodPost, url, strings.NewReader(form.Encode()))
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
hc := http.Client{}
|
|
|
|
return hc.Do(req)
|
|
|
|
}
|