mirror of
https://github.com/drone-plugins/drone-download.git
synced 2026-06-04 18:24:21 +08:00
Copy the target file first and fix checksum checks
This commit is contained in:
@@ -107,39 +107,45 @@ func (p Plugin) Exec() error {
|
||||
|
||||
defer target.Close()
|
||||
|
||||
if p.Config.MD5 != "" {
|
||||
h := md5.New()
|
||||
|
||||
if _, err := io.Copy(h, resp.Body); err != nil {
|
||||
return errors.Wrap(err, "failed to compare checksum")
|
||||
}
|
||||
|
||||
check := fmt.Sprintf("%x", h.Sum(nil))
|
||||
|
||||
if p.Config.MD5 != check {
|
||||
return fmt.Errorf("checksum doesn't match, got %s and expected %s", check, p.Config.MD5)
|
||||
}
|
||||
}
|
||||
|
||||
if p.Config.SHA256 != "" {
|
||||
h := sha256.New()
|
||||
|
||||
if _, err := io.Copy(h, resp.Body); err != nil {
|
||||
return errors.Wrap(err, "failed to compare checksum")
|
||||
}
|
||||
|
||||
check := fmt.Sprintf("%x", h.Sum(nil))
|
||||
|
||||
if p.Config.SHA256 != check {
|
||||
return fmt.Errorf("checksum doesn't match, got %s and expected %s", check, p.Config.SHA256)
|
||||
}
|
||||
}
|
||||
|
||||
_, err = io.Copy(target, resp.Body)
|
||||
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "copying destination failed")
|
||||
}
|
||||
|
||||
if p.Config.MD5 != "" {
|
||||
h := md5.New()
|
||||
target.Seek(0, 0)
|
||||
|
||||
if _, err := io.Copy(h, target); err != nil {
|
||||
defer os.Remove(target.Name())
|
||||
return errors.Wrap(err, "failed to compare checksum")
|
||||
}
|
||||
|
||||
check := fmt.Sprintf("%x", h.Sum(nil))
|
||||
|
||||
if p.Config.MD5 != check {
|
||||
defer os.Remove(target.Name())
|
||||
return fmt.Errorf("checksum doesn't match, got %s and expected %s", check, p.Config.MD5)
|
||||
}
|
||||
}
|
||||
|
||||
if p.Config.SHA256 != "" {
|
||||
h := sha256.New()
|
||||
target.Seek(0, 0)
|
||||
|
||||
if _, err := io.Copy(h, target); err != nil {
|
||||
defer os.Remove(target.Name())
|
||||
return errors.Wrap(err, "failed to compare checksum")
|
||||
}
|
||||
|
||||
check := fmt.Sprintf("%x", h.Sum(nil))
|
||||
|
||||
if p.Config.SHA256 != check {
|
||||
defer os.Remove(target.Name())
|
||||
return fmt.Errorf("checksum doesn't match, got %s and expected %s", check, p.Config.SHA256)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user