download.go
func ToFile(src, dest string, options FileOptions) error {
	u, err := url.Parse(src)
	if err != nil {
		return errors.Wrap(err, "invalid src URL")
	}

	targetDir := filepath.Dir(dest)
	if err = createDir(targetDir, options.Mkdirs == nil || *options.Mkdirs); err != nil {
		return err
	}

	targetName := filepath.Base(dest)
	f, err := ioutil.TempFile(targetDir, ".tmp-"+targetName)
	if err != nil {
		return errors.Wrap(err, "failed to create temp file")
	}

	err = downloadFile(u, f, options.Options)
	if err != nil {
		_ = f.Close()           // #nosec
		_ = os.Remove(f.Name()) // #nosec
		return errors.Wrap(err, "failed to download")
	}
	err = f.Close()
	if err != nil {
		_ = os.Remove(f.Name()) // #nosec
		return errors.Wrap(err, "failed to close temp file")
	}

	if err = renameFile(f.Name(), dest); err != nil {
		return err
	}

	return nil
}
func ToFile(src, dest string, options FileOptions) error {
	u, err := url.Parse(src)
	if err != nil {
		return errors.Wrap(err, "invalid src URL")
	}

	targetDir := filepath.Dir(dest)
	if err = createDir(targetDir, options.Mkdirs == nil || *options.Mkdirs); err != nil {
		return err
	}

	targetName := filepath.Base(dest)
	f, err := ioutil.TempFile(targetDir, ".tmp-"+targetName)
	if err != nil {
		return errors.Wrap(err, "failed to create temp file")
	}

	err = downloadFile(u, f, options.Options)
	if err != nil {
		_ = f.Close()           // #nosec
		_ = os.Remove(f.Name()) // #nosec
		return errors.Wrap(err, "failed to download")
	}
	err = f.Close()
	if err != nil {
		_ = os.Remove(f.Name()) // #nosec
		return errors.Wrap(err, "failed to close temp file")
	}

	if err = renameFile(f.Name(), dest); err != nil {
		return err
	}

	return nil
}
// ToFile downloads the specified `src` URL to `dest` file using
// the specified `FileOptions`.