Add checksum and insecure flag for user to skip checksum generation (#1430)

This commit is contained in:
Ta-Ching Chen
2019-11-24 01:35:01 +08:00
committed by GitHub
parent a66de4c601
commit 5c2f0c5f4a
13 changed files with 388 additions and 204 deletions
+35
View File
@@ -17,12 +17,15 @@ limitations under the License.
package utils
import (
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"golang.org/x/net/context/ctxhttp"
"io"
"io/ioutil"
"net"
"net/http"
"os"
"path/filepath"
"strings"
@@ -198,3 +201,35 @@ func GetChecksum(src io.Reader) (*fv1.Checksum, error) {
func IsURL(str string) bool {
return strings.HasPrefix(str, "http://") || strings.HasPrefix(str, "https://")
}
func DownloadUrl(ctx context.Context, httpClient *http.Client, url string, localPath string) error {
resp, err := ctxhttp.Get(ctx, httpClient, url)
if err != nil {
return err
}
defer resp.Body.Close()
w, err := os.Create(localPath)
if err != nil {
return err
}
defer w.Close()
_, err = io.Copy(w, resp.Body)
if err != nil {
return err
}
// flushing write buffer to file
err = w.Sync()
if err != nil {
return err
}
err = os.Chmod(localPath, 0600)
if err != nil {
return err
}
return nil
}