linsk/storage/download.go

156 lines
3.6 KiB
Go
Raw Normal View History

2023-09-02 20:03:44 +01:00
// Linsk - A utility to access Linux-native file systems on non-Linux operating systems.
// Copyright (c) 2023 The Linsk Authors.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
2023-08-30 12:39:38 +01:00
package storage
import (
"bytes"
"context"
2023-08-30 12:39:38 +01:00
"crypto/sha256"
"encoding/hex"
"fmt"
"hash"
"io"
"math"
"net/http"
"os"
2023-09-02 12:14:02 +01:00
"path/filepath"
2023-08-30 12:39:38 +01:00
"github.com/dustin/go-humanize"
"github.com/pkg/errors"
)
func (s *Storage) download(ctx context.Context, url string, hash []byte, out string, applyReaderMiddleware func(io.Reader) io.Reader) error {
2023-09-02 12:14:02 +01:00
outClean := filepath.Clean(out)
2023-08-30 12:39:38 +01:00
var created, success bool
defer func() {
if created && !success {
2023-09-02 12:14:02 +01:00
_ = os.Remove(outClean)
2023-08-30 12:39:38 +01:00
}
}()
2023-09-02 12:14:02 +01:00
_, err := os.Stat(outClean)
2023-08-30 12:39:38 +01:00
if err == nil {
return errors.Wrap(err, "file already exists")
2023-09-02 11:47:58 +01:00
} else if !errors.Is(err, os.ErrNotExist) {
return errors.Wrap(err, "stat out path")
2023-08-30 12:39:38 +01:00
}
2023-09-02 12:14:02 +01:00
f, err := os.OpenFile(outClean, os.O_CREATE|os.O_WRONLY, 0400)
2023-08-30 12:39:38 +01:00
if err != nil {
return errors.Wrap(err, "open file")
}
created = true
defer func() { _ = f.Close() }()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return errors.Wrap(err, "create new http get request")
}
2023-09-02 12:14:02 +01:00
s.logger.Info("Starting to download file", "from", url, "to", outClean)
2023-08-30 12:39:38 +01:00
resp, err := http.DefaultClient.Do(req)
2023-08-30 12:39:38 +01:00
if err != nil {
return errors.Wrap(err, "http get")
}
defer func() { _ = resp.Body.Close() }()
2023-08-30 13:13:08 +01:00
knownSize := resp.ContentLength
var readFrom io.Reader
if applyReaderMiddleware != nil {
readFrom = applyReaderMiddleware(resp.Body)
knownSize = 0
} else {
readFrom = resp.Body
}
n, err := copyWithProgressAndHash(f, readFrom, 1024, hash, func(downloaded int) {
var percent float64
if knownSize != 0 {
percent = float64(downloaded) / float64(knownSize)
}
2023-09-02 12:14:02 +01:00
lg := s.logger.With("out", outClean, "done", humanize.Bytes(uint64(downloaded)))
2023-08-30 13:13:08 +01:00
if percent != 0 {
lg.Info("Downloading file", "percent", math.Round(percent*100*100)/100)
} else {
lg.Info("Downloading compressed file", "percent", "N/A")
}
2023-08-30 12:39:38 +01:00
})
if err != nil {
return errors.Wrap(err, "copy resp to file")
}
2023-09-02 12:14:02 +01:00
s.logger.Info("Successfully downloaded file", "from", url, "to", outClean, "out-size", humanize.Bytes(uint64(n)))
2023-08-30 12:39:38 +01:00
success = true
return nil
}
2023-08-30 13:13:08 +01:00
func copyWithProgressAndHash(dst io.Writer, src io.Reader, blockSize int, wantHash []byte, report func(int)) (int, error) {
2023-08-30 12:39:38 +01:00
block := make([]byte, blockSize)
var h hash.Hash
if wantHash != nil {
h = sha256.New()
}
var progress int
for {
read, err := src.Read(block)
if read > 0 {
written, err := dst.Write(block[:read])
if err != nil {
return progress, errors.Wrap(err, "write")
}
if h != nil {
h.Write(block[:read])
}
progress += written
}
if err != nil {
if errors.Is(err, io.EOF) {
break
}
return progress, errors.Wrap(err, "read")
}
if progress%1000000 == 0 {
2023-08-30 13:13:08 +01:00
report(progress)
2023-08-30 12:39:38 +01:00
}
}
if h != nil {
sum := h.Sum(nil)
if !bytes.Equal(sum, wantHash) {
return progress, fmt.Errorf("hash mismach: want '%v', have '%v'", hex.EncodeToString(wantHash), hex.EncodeToString(sum))
}
}
return progress, nil
}