Use the chunk manager properly

This commit is contained in:
Ben Sarmiento
2023-11-18 01:51:25 +01:00
parent 4da9416bec
commit b669f8d673
10 changed files with 30 additions and 31 deletions

View File

@@ -9,6 +9,7 @@ import (
"time"
"github.com/debridmediamanager.com/zurg/internal/torrent"
zurghttp "github.com/debridmediamanager.com/zurg/pkg/http"
"github.com/debridmediamanager.com/zurg/pkg/logutil"
"go.uber.org/zap"
"golang.org/x/sys/unix"
@@ -23,12 +24,13 @@ type Downloader struct {
storage *Storage
torMgr *torrent.TorrentManager
log *zap.SugaredLogger
client *zurghttp.HTTPClient
}
type DownloadCallback func(error, []byte)
// NewDownloader creates a new download manager
func NewDownloader(threads int, storage *Storage, bufferSize int64, torMgr *torrent.TorrentManager) (*Downloader, error) {
func NewDownloader(threads int, storage *Storage, bufferSize int64, torMgr *torrent.TorrentManager, client *zurghttp.HTTPClient) (*Downloader, error) {
rlog := logutil.NewLogger()
log := rlog.Named("downloader")
@@ -38,6 +40,7 @@ func NewDownloader(threads int, storage *Storage, bufferSize int64, torMgr *torr
callbacks: make(map[RequestID][]DownloadCallback, 100),
storage: storage,
torMgr: torMgr,
client: client,
log: log,
}
@@ -76,7 +79,6 @@ func (d *Downloader) thread(n int) {
}
func (d *Downloader) download(req *Request, buffer []byte) {
d.log.Debugf("Starting download %v (preload: %v)", req.id, req.preload)
err := d.downloadFromAPI(req, buffer, 0)
d.lock.Lock()
@@ -137,7 +139,7 @@ func (d *Downloader) downloadFromAPI(request *Request, buffer []byte, delay int6
d.log.Debugf("response read error: %v", err)
return fmt.Errorf("could not read objects %s %s API response", request.file.Path, request.file.Link)
}
d.log.Debugf("Downloaded %v bytes of %s %s", n, request.file.Path, request.file.Link)
d.log.Debugf("Downloaded %v bytes of %s", n, request.file.Path)
return nil
}