feat: add checksum for torrent downloader
All checks were successful
Pre-commit / pre-commit (pull_request) Successful in 6m0s
Update alr-git / changelog (push) Successful in 28s

This commit is contained in:
2025-06-20 20:12:43 +03:00
parent 6bccce1db4
commit 85878f69d3
4 changed files with 72 additions and 49 deletions

View File

@ -20,11 +20,8 @@
package dl
import (
"bytes"
"context"
"encoding/hex"
"errors"
"log/slog"
"net/url"
"path"
"strconv"
@ -127,7 +124,7 @@ func (d *GitDownloader) Download(ctx context.Context, opts Options) (Type, strin
}
}
err = d.verifyHash(opts)
err = VerifyHashFromLocal("", opts)
if err != nil {
return 0, "", err
}
@ -139,30 +136,6 @@ func (d *GitDownloader) Download(ctx context.Context, opts Options) (Type, strin
return TypeDir, name, nil
}
func (GitDownloader) verifyHash(opts Options) error {
if opts.Hash != nil {
h, err := opts.NewHash()
if err != nil {
return err
}
err = HashDir(opts.Destination, h)
if err != nil {
return err
}
sum := h.Sum(nil)
slog.Warn("validate checksum", "real", hex.EncodeToString(sum), "expected", hex.EncodeToString(opts.Hash))
if !bytes.Equal(sum, opts.Hash) {
return ErrChecksumMismatch
}
}
return nil
}
// Update uses git to pull the repository and update it
// to the latest revision. It allows specifying the depth
// and recursion options via query string. It returns
@ -225,7 +198,7 @@ func (d *GitDownloader) Update(opts Options) (bool, error) {
return false, err
}
err = d.verifyHash(opts)
err = VerifyHashFromLocal("", opts)
if err != nil {
return false, err
}