github.com/filecoin-project/bacalhau@v0.3.23-0.20230228154132-45c989550ace/pkg/downloader/estuary/downloader.go (about)

     1  package estuary
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/filecoin-project/bacalhau/pkg/downloader/ipfs"
     7  	"go.uber.org/multierr"
     8  
     9  	"github.com/filecoin-project/bacalhau/pkg/downloader/http"
    10  	"github.com/filecoin-project/bacalhau/pkg/model"
    11  	"github.com/filecoin-project/bacalhau/pkg/system"
    12  )
    13  
    14  // Estuary downloader uses HTTP downloader to download result published to Estuary
    15  // by combining Estuary gateway URL and CID returned by Estuary publisher and passing it for download.
    16  type Downloader struct {
    17  	httpDownloader *http.Downloader
    18  	ipfsDownloader *ipfs.Downloader
    19  }
    20  
    21  func NewEstuaryDownloader(cm *system.CleanupManager, settings *model.DownloaderSettings) *Downloader {
    22  	return &Downloader{
    23  		ipfsDownloader: ipfs.NewIPFSDownloader(cm, settings),
    24  		httpDownloader: http.NewHTTPDownloader(settings),
    25  	}
    26  }
    27  
    28  func (downloader *Downloader) IsInstalled(ctx context.Context) (bool, error) {
    29  	ipfsInstalled, ipfsErr := downloader.ipfsDownloader.IsInstalled(ctx)
    30  	httpInstalled, httpErr := downloader.httpDownloader.IsInstalled(ctx)
    31  	return ipfsInstalled && httpInstalled, multierr.Combine(ipfsErr, httpErr)
    32  }
    33  
    34  func (downloader *Downloader) FetchResult(ctx context.Context, result model.PublishedResult, downloadPath string) error {
    35  	ctx, span := system.NewSpan(ctx, system.GetTracer(), "pkg/downloader.estuary.FetchResult")
    36  	defer span.End()
    37  
    38  	// fallback to ipfs download for old results without URL
    39  	if result.Data.URL == "" {
    40  		return downloader.ipfsDownloader.FetchResult(ctx, result, downloadPath)
    41  	}
    42  
    43  	return downloader.httpDownloader.FetchResult(ctx, result, downloadPath)
    44  }