mirror of
https://github.com/aljazceru/kata-containers.git
synced 2025-12-26 18:44:47 +01:00
Add kata-pkgsync as the OBS to Packagecloud sync tool. Fixes: #506 Signed-off-by: Marco Vedovati <mvedovati@suse.com>
46 lines
1001 B
Go
46 lines
1001 B
Go
package upload
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"mime/multipart"
|
|
"net/http"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// NewRequest creates a new file upload HTTP request with optional extra params.
|
|
// Based on https://gist.github.com/mattetti/5914158
|
|
func NewRequest(url string, params map[string]string, paramName, path string) (*http.Request, error) {
|
|
file, err := os.Open(path)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer file.Close()
|
|
|
|
var body bytes.Buffer
|
|
writer := multipart.NewWriter(&body)
|
|
part, err := writer.CreateFormFile(paramName, filepath.Base(path))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if _, err = io.Copy(part, file); err != nil {
|
|
return nil, err
|
|
}
|
|
for k, v := range params {
|
|
if err := writer.WriteField(k, v); err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
if err := writer.Close(); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
request, err := http.NewRequest("POST", url, &body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
request.Header.Add("Content-Type", writer.FormDataContentType())
|
|
return request, nil
|
|
}
|